2016-10-04 40 views
0

我解析在ShenzhenDecoder类的消息,以获得它的数据 - decode()方法。在解析它之后,我在ShenzhenRecoder类中构建了xml文件。但是在深圳解码器类中设置数据--setStatusFlagMaptoRecorder()后,我遇到了一个问题,要求将数据从statusFlag类中取出。我在哪里以及如何初始化ShenzhenDecoder的内部StatusFlag类以获取其侧面数据createXMLFromRecord()?因为getStatusflag()变为null,getStausFlag()。getLowBattery()变得异常,因为getStatusflag()为null。的Java:初始化一个内部类在正确的位置

ShenzhenDecoder

公共类ShenzhenDecoder {

ShenzhenRecord record = null; 

LinkedHashMap<String, String> statusFlagMap = new LinkedHashMap<String, String>(); 


public ShenzhenRecord decode(final byte[] data) { 
     this.record = new ShenzhenRecord(); 
     byte[] imeiArray = Arrays.copyOfRange(data, 1, 16); 
     String imei = new String(imeiArray, "UTF-8"); 
     System.out.println("IMEI: " + imei); 
     this.record.setImei(imei); 

} 


private void assignStatusFlagMaptoRecorder() { 
    StatusFlag statusFlagRecord = this.record.new StatusFlag(); 
    for (Entry<String, String> entry : this.statusFlagMap.entrySet()) { 
     String key = entry.getKey(); 
     switch (key) { 
     case "lowBattery": 
      statusFlagRecord.setLowBattery(entry.getValue()); 
      break; 
     case "spare1BitFirstByte": 
      statusFlagRecord.setSpare1BitFirstByte(entry.getValue()); 
      break; 

     case "lbs": 
      statusFlagRecord.setLbs(entry.getValue()); 
      break; 
     case "gmsBlindArea": 
      statusFlagRecord.setGmsBlindArea(entry.getValue()); 
      break; 
     } 

    } 

} 

}

Shenzhenrecord

public class ShenzhenRecord { 

    private String imei; 
    private StatusFlag statusFlag; 


    public String getImei() { 
     return this.imei; 
    } 


    public void setImei(String imei) { 
     this.imei = imei; 
    } 

    public class StatusFlag { 

     private String lowBattery; 


     public String getLowBattery() { 
      return this.lowBattery; 
     } 


     public void setLowBattery(String lowBattery) { 
      this.lowBattery = lowBattery; 
     } 

    } 


    public Document createXMLFromRecord(String code) throws ApplicationException { 

     Document document = DocumentHelper.createDocument(); 

     Element shenzhenElement = document.addElement("shenzhen"); 

     shenzhenElement.addAttribute("imei", getImei()); 

     Element statusFlagElement = shenzhenElement.addElement("statusFlag"); 

     StatusFlag statusFlagtest = this.getStatusFlag(); 
     //Here I am getting an error because getStatusFlag() has null. 
     String lowBatteryTest = this.getStatusFlag().getLowBattery(); 

     statusFlagElement.addAttribute("lowBattery", this.getStatusFlag().getLowBattery()); 

     } 

    return document; 
} 

} 

回答

0
StatusFlag statusFlagRecord = this.record.new StatusFlag(); 

意味着新创建StatusFlag具有对记录的引用,而不是该字段“ShenzhenRecord.statusFlag”被初始化。因此,只要删除了这一行,让ShenzhenRecord初始化它的字段本身,因为它的预期在OOP:

public class ShenzhenRecord { 
    private String imei; 
    private StatusFlag statusFlag = new StatusFlag(); 

    public String getImei() { 
     return this.imei; 
    } 

    public void setImei(String imei) { 
     this.imei = imei; 
    } 

    public static class StatusFlag { 

而且只要内部类不需要他们周围的类的引用,它可能是将它们标记为一个好主意作为静态。如果你对初始化字段更灵活的方法感兴趣,你可以看看构造器和初始化器。