私は Android アプリを作成しており、そのために XML 解析を行う必要があります。動作する例に従いましたが、例を自分の使用法に合わせようとすると、理解できないヌルポインター例外が発生します。これは、関連するコードのすべてのビットを保持する必要があります。問題は、このスニピットの下部にあるLog.e("5", "5")
と対応する の"6"
間の行にありますElement docEle = dom.getDocumentElement();
何か案は?
public class Parser {
//Constructor
ArrayList<Response> responseList;
ArrayList<ResourceType> resourceTypeList;
List myEmpls;
Document dom;
public Parser()
{
responseList = new ArrayList<Response>();
resourceTypeList = new ArrayList<ResourceType>();
myEmpls = new ArrayList();
}
public void addRequest(int reportId)
{
Request request = new Request(reportId);
Response response = new Response(request);
//Form XML in LoadReport Request
//Send XML
//Receive and parse
parseReportResponse();
//Input information into response
//Form XML in GetRoomData request
//Send XML
//Receive and parse
//Input information into response
responseList.add(response);
Log.e("5", "5");
}
public void parseReportResponse()
{
parseReportXML();
parseReportDocument();
}
public void parseReportXML()
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
dom = db.parse("LoadReportResponse.xml");
}catch(ParserConfigurationException pce) {
pce.printStackTrace();
}catch(SAXException se) {
se.printStackTrace();
}catch(IOException ioe) {
ioe.printStackTrace();
}
}
public void parseReportDocument()
{
Log.e("5", "5");
Element docEle = dom.getDocumentElement();
Log.e("6", "6");
NodeList nl = docEle.getElementsByTagName("Room");
if(nl != null && nl.getLength() > 0)
{
for(int i=0; i<nl.getLength(); i++)
{
Element el = (Element)nl.item(i);
//Employee e = getEmployee(el);
Room room = getNewRoom(el);
myEmpls.add(room);
}
}
}
ログキャットはこちら
06-24 15:16:08.209: E/AndroidRuntime(6017): FATAL EXCEPTION: main
06-24 15:16:08.209: E/AndroidRuntime(6017): java.lang.RuntimeException: Unable to start activity ComponentInfo{[Stuff I have to hide].MainActivity}: java.lang.NullPointerException
06-24 15:16:08.209: E/AndroidRuntime(6017): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-24 15:16:08.209: E/AndroidRuntime(6017): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-24 15:16:08.209: E/AndroidRuntime(6017): at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-24 15:16:08.209: E/AndroidRuntime(6017): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-24 15:16:08.209: E/AndroidRuntime(6017): at android.os.Handler.dispatchMessage(Handler.java:99)
06-24 15:16:08.209: E/AndroidRuntime(6017): at android.os.Looper.loop(Looper.java:137)
06-24 15:16:08.209: E/AndroidRuntime(6017): at android.app.ActivityThread.main(ActivityThread.java:5039)
06-24 15:16:08.209: E/AndroidRuntime(6017): at java.lang.reflect.Method.invokeNative(Native Method)
06-24 15:16:08.209: E/AndroidRuntime(6017): at java.lang.reflect.Method.invoke(Method.java:511)
06-24 15:16:08.209: E/AndroidRuntime(6017): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-24 15:16:08.209: E/AndroidRuntime(6017): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-24 15:16:08.209: E/AndroidRuntime(6017): at dalvik.system.NativeStart.main(Native Method)
06-24 15:16:08.209: E/AndroidRuntime(6017): Caused by: java.lang.NullPointerException
06-24 15:16:08.209: E/AndroidRuntime(6017): at com.example.[I have to hide].Parser.parseReportDocument(Parser.java:81)
06-24 15:16:08.209: E/AndroidRuntime(6017): at com.example.[I have to hide].Parser.parseReportResponse(Parser.java:59)
06-24 15:16:08.209: E/AndroidRuntime(6017): at com.example.[I have to hide].Parser.addRequest(Parser.java:41)
06-24 15:16:08.209: E/AndroidRuntime(6017): at com.example.[I have to hide].MainActivity.onCreate(MainActivity.java:79)
06-24 15:16:08.209: E/AndroidRuntime(6017): at android.app.Activity.performCreate(Activity.java:5104)
06-24 15:16:08.209: E/AndroidRuntime(6017): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-24 15:16:08.209: E/AndroidRuntime(6017): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-24 15:16:08.209: E/AndroidRuntime(6017): ... 11 more