2016-10-04 65 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 { 

而且只要內部類不需要他們周圍的類的引用,它可能是將它們標記爲一個好主意作爲靜態。如果你對初始化字段更靈活的方法感興趣,你可以看看構造器和初始化器。