我解析在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;
}
}