我为我的xml页面编写了一个解析器,我得到一个奇怪的错误,我完全不明白为什么。首先,我想向您展示我的xml页面:
<?xml version="1.0" encoding="UTF-8"?>
<drugs xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://drugbank.ca" xs:schemaLocation="http://www.drugbank.ca/docs/drugbank.xsd" schemaVersion="1.4">
<drug type="biotech" created="2005-06-13 07:24:05 -0600" updated="2012-02-13 17:42:20 -0700" version="3.0">
<drugbank-id>DB00001</drugbank-id>
<name>Lepirudin</name>
<drug-interactions>
<drug-interaction>
<drug>DB01381</drug>
<name>Ginkgo biloba</name>
<description>Additive anticoagulant/antiplatelet effects may increase bleed risk. Concomitant therapy should be avoided.</description>
</drug-interaction>
<drug-interaction>
<drug>DB00374</drug>
<name>Treprostinil</name>
<description>The prostacyclin analogue, Treprostinil, increases the risk of bleeding when combined with the anticoagulant, Lepirudin. Monitor for increased bleeding during concomitant thearpy. </description>
</drug-interaction>
</drug-interactions>
<targets>
<target partner="54">
<actions>
<action>inhibitor</action>
</actions>
</target>
</targets>
</drug>
<partners>
<partner id="54">
<name>Prothrombin</name>
<general-function>Involved in blood clotting cascade</general-function>
</partner>
</partners>
在这里,您可以看到药物相互作用标签,其中有关于相互作用药物的信息。因此,当我删除药物相互作用标签时,我的解析器工作得很好,但是使用药物交互标签,我得到了NullPointerException。
错误行:
if(nameElement.getChildNodes().item(0).getNodeValue().equals(str))
我在这里放了整个解析器代码。你也可以自己尝试一下。为什么我会收到此错误,如何解决?抱歉很长的帖子。感谢
import java.io.File;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class xmlParser {
public static void main(String argv[]) {
getDrug("Lepirudin");
}
public static void getDrug(String str){
ArrayList<String> arr = new ArrayList();
String trgts = "";
try {
File file = new File("test.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
doc.getDocumentElement().normalize();
System.out.println("Root element " + doc.getDocumentElement().getNodeName());
NodeList drugList = doc.getElementsByTagName("drug");
for (int s = 0; s < drugList.getLength(); s++) {
Node drugNode = drugList.item(s);
if (drugNode.getNodeType() == Node.ELEMENT_NODE) {
Element drugElement = (Element) drugNode;
Element nameElement = (Element) drugElement.getElementsByTagName("name").item(0);
if(nameElement.getChildNodes().item(0).getNodeValue().equals(str)){
System.out.println("Name : " + nameElement.getChildNodes().item(0).getNodeValue());
Element drugNodeElement = (Element) drugElement.getElementsByTagName("drugbank-id").item(0);
System.out.println("ID : " + drugNodeElement.getChildNodes().item(0).getNodeValue());
/////////////////////////////////////////////////
NodeList targetList = (NodeList) drugElement.getElementsByTagName("target");
for(int t=0; t<targetList.getLength(); t++)
{
Node targetNode = targetList.item(t);
if(targetNode.getNodeType() == Node.ELEMENT_NODE)
{
Element targetElement = (Element) targetNode;
if(targetElement.getAttribute("position").equals(""))
{
System.out.println("target partner: " + targetElement.getAttribute("partner"));
}
else
{
System.out.println("target position: "+ targetElement.getAttribute("position")+ "target partner: "+ targetElement.getAttribute("partner"));
}
arr.add(targetElement.getAttribute("partner"));
}
}
System.out.println("-------------------------");
}
}
}
NodeList partnersList = doc.getElementsByTagName("partners");
for (int q = 0; q < partnersList.getLength(); q++)
{
Node partnersNode = partnersList.item(q);
if (partnersNode.getNodeType() == Node.ELEMENT_NODE) {
Element partnersElement = (Element) partnersNode;
NodeList partnerList = (NodeList) partnersElement.getElementsByTagName("partner");
for(int w=0; w<partnerList.getLength(); w++)
{
Node partnerNode = partnerList.item(w);
if(partnerNode.getNodeType() == Node.ELEMENT_NODE)
{
Element partnerElement = (Element) partnerNode;
if(arr.contains(partnerElement.getAttribute("id"))){
Element partnernameElement = (Element) partnerElement.getElementsByTagName("name").item(0);
trgts += partnernameElement.getChildNodes().item(0).getNodeValue() + "," ;
}
}
}
}
}
trgts = trgts.substring(0, trgts.length()-1);
System.out.println(trgts);
} catch (Exception e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
Null Pointer Exception意味着您尝试在空指针上执行函数。我建议你做的是弄清楚你得到的音符有多远。一次删除一个函数调用,直到找出null的发生位置,并查看是否可以找出该值返回null的原因。
nameElement.getChildNodes().item(0).getNodeValue().equals(str)