I'm trying to read some XML nodes and store them into a Java object. I can read some of the elements of the object while I can't read others and I don't know why. Here is how a part of the XML looks like:
<problems>
<problem id = "1">
<quest>22-16:4</quest>
<result>18</result>
<chapter_id>1</chapter_id>
<type>text</type>
</problem>
<problem id = "2">
<quest>16+2*12</quest>
<result>30</result>
<chapter_id>1</chapter_id>
<type>text</type>
</problem>
<problem id = "3">
<quest>72:2-18</quest>
<result>18</result>
<chapter_id>1</chapter_id>
<type>text</type>
</problem>
<problem id = "4">
<quest>12*4-15:5</quest>
<result>45</result>
<chapter_id>1</chapter_id>
<type>text</type>
</problem>
</problems>
And here is the XMLHandler:
try {
Configuration configuration = new Configuration().configure();
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
File fXmlFile = new File("C:\\Users\\buciu\\workspace\\teachApp\\xml\\problems.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("problem");
for (int temp = 0; temp < nList.getLength(); temp++) {
Transaction transaction = session.beginTransaction();
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
Problem p = new Problem();
ChapterDAO chdao=new ChapterDAO();
p.setQuest(eElement.getElementsByTagName("quest").item(0).getTextContent());
p.setResult(eElement.getElementsByTagName("result").item(0).getTextContent());
Chapter c = chdao.findTChapter(eElement.getElementsByTagName("chapter_id").item(0).getTextContent());
p.setType(eElement.getElementsByTagName("type").item(0).getTextContent());
p.setChapter(c);
session.save(p);
transaction.commit();
}
}
} catch (Exception e) {
e.printStackTrace();
}
I can read the quest, result and chapter_id elements, but I cannot read the type element. I used my application without it so far, I just added it as a new element and now I want to create another object, updated.
The error it gives me is simple(pointing at the line with setting the type element):
java.lang.NullPointerException
at testsManagement.XMLHandler.readXMLProblems(XMLHandler.java:57)
at Main.main(Main.java:30)
So do you see where is the problem? Thanks!