I have created a delete method. My method has an index as argument that comes from a jlist position. The method will get the position from the jlist that the user have clicked and look up that element in the xml and delete it and all children.
So if user click TrainCategorySpeed it has index 122 and will look for that type in the XML
<type>
<OBJECT_TYPE>TrainCategorySpeed</OBJECT_TYPE>
- <prop>
<DESCRIPTION>Differential speed limit</DESCRIPTION>
<PARENT>NULL</PARENT>
<VIRTUAL>0</VIRTUAL>
<VISIBLE>0</VISIBLE>
<PICTURE>NULL</PICTURE>
<HELP>NULL</HELP>
<MIN_NO>NULL</MIN_NO>
<MAX_NO>NULL</MAX_NO>
<NAME_FORMAT>NULL</NAME_FORMAT>
</prop>
- <param>
<PARAMETER>trainCategory</PARAMETER>
<DATA_TYPE>INTEGER</DATA_TYPE>
<DESCRIPTION>Train category</DESCRIPTION>
<MIN_NO>1</MIN_NO>
<MAX_NO>1</MAX_NO>
<ORDER1>1</ORDER1>
<NESTED>1</NESTED>
<DEFAULT1>NULL</DEFAULT1>
<FORMAT>0:15</FORMAT>
</param>
- <param>
<PARAMETER>speed</PARAMETER>
<DATA_TYPE>INTEGER</DATA_TYPE>
<DESCRIPTION>Speed (km/h (V_DIFF))</DESCRIPTION>
<MIN_NO>1</MIN_NO>
<MAX_NO>1</MAX_NO>
<ORDER1>2</ORDER1>
<NESTED>1</NESTED>
<DEFAULT1>NULL</DEFAULT1>
<FORMAT>0:600:5</FORMAT>
</param>
</type>
But when I am trying to achieve this it gives me a nullpointer exception
on the follwoing line : Node type = doc.getElementsByTagName("type").item(x);
x here is really giving correct position and type is the element in the xml.
but the type is null.
This is the rest of the method :
public void deleteObjType(int x) {
System.out.println("index : " + x); // This one will return index : 122 in the console and it does
File file = new File("xmlFiles/CoreDatamodel.xml");
DocumentBuilderFactory builderFactory = DocumentBuilderFactory
.newInstance();
try {
DocumentBuilder builder = builderFactory.newDocumentBuilder();
builder = builderFactory.newDocumentBuilder();
Document doc = builder.parse(file);
Node type = doc.getElementsByTagName("type").item(x);
System.out.println("type : " + type);
NodeList nodes = type.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
Node element = nodes.item(x);
System.out.println("element : " + element);
// remove node
if ("OBJECT_TYPE".equals(element.getNodeName())) {
type.removeChild(element);
}
}
// Save the new update
save(file, doc);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XPathExpressionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Full stacktrace:
java.lang.NullPointerException
at cxmleditor.service.XMLEditorService.deleteObjType(XMLEditorService.java:106)
at xmleditor.gui.XmlEditorMain$deleteObjType.actionPerformed(XmlEditorMain.java:383)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)