gpt4 book ai didi

java - 在相等标签处解析 xml 时出现 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 11:28:53 25 4
gpt4 key购买 nike

我的 XML 文件:

<job_id>000273E060E87000C0001323FA21427120150602153306</job_id>
<job_id>000273E060E87000C0001323FA21427120150602153342</job_id>
<job_id>000273E060E87000C0001323FA21427120150602153419</job_id>

我在这里得到一个 NullPointerException:

protected ArrayList<HashMap<String, String>> doInBackground(URL... urls) {
if (cd.isConnectingToInternet()) {
XMLParser xmlParser = new XMLParser();
String xml = xmlParser.getXMLFromUrl("http://www.example.de/android.php?f=1200");
Document doc = xmlParser.getDomElement(xml);
writeToFile(xml, "xmlfilesnames");

NodeList noteListFileNames = doc.getElementsByTagName("tasklist");

for (int i = 0; i <= noteListFileNames.getLength(); i++) {

HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) noteListFileNames.item(i);

map.put("job_id", xmlParser.getValue(e, "job_id"));
xmlFileNames.add(map);

}

at: map.put("job_id", xmlParser.getValue(e, "job_id")); 当 for 循环第二次运行时。

堆栈跟踪:

java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.NullPointerException
at xmlParsing.XMLParser.getValue(XMLParser.java:88)
at de.example.app.ListViewActivity$DownloadXMLFilesName.doInBackground(ListViewActivity.java:298)
at de.example.app.ListViewActivity$DownloadXMLFilesName.doInBackground(ListViewActivity.java:281)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)

            位于 android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)在 java.lang.Thread.run(Thread.java:841)

XML 文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<tasklist>
<job_id>000273E060E87000C0001323FA21427120150602153306</job_id>
<job_id>000273E060E87000C0001323FA21427120150602153342</job_id>
<job_id>000273E060E87000C0001323FA21427120150602153419</job_id>
</tasklist>

第一次循环

xml文件名:

000273E060E87000C0001323FA21427120150602153419

map :

000273E060E87000C0001323FA21427120150602153419

第二个循环xml文件名:

000273E060E87000C0001323FA21427120150602153342
000273E060E87000C0001323FA21427120150602153342

map :

000273E060E87000C0001323FA21427120150602153342

第三个循环

xml文件名

000273E060E87000C0001323FA21427120150602153419
000273E060E87000C0001323FA21427120150602153419
000273E060E87000C0001323FA21427120150602153419

map :

000273E060E87000C0001323FA21427120150602153419

最佳答案

在for循环中应小于不小于或等于

for (int i = 0; i <= noteListFileNames.getLength(); i++)

应该是

for (int i = 0; i < noteListFileNames.getLength(); i++)

除了您的进一步问题:

尝试改变:

Element e = (Element) noteListFileNames.item(i);

map.put("job_id", xmlParser.getValue(e, "job_id"));
xmlFileNames.add(map);

至:

Element e = (Element) noteListFileNames.item(i);

NodeList children = e.getChildNodes();

for (int j = 0; j < children.getLength(); j++) {
if (children.item(j).getNodeName().equals("job_id")) {
map.put(children.item(j).getNodeName(),
children.item(j).getTextContent());
xmlFileNames.add(map);
}
}

或者类似的东西。不确定您使用的是哪个解析器。

关于java - 在相等标签处解析 xml 时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30598273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com