-1

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)
Sembrano
  • 1,127
  • 4
  • 18
  • 28
  • Your question is very unclear. The title says you're having trouble deleting, but your question says the problem is in _finding_ the node to be deleted. Please clarify. – Jim Garrison Jan 27 '14 at 16:47
  • Ok I edited the title. but this line of code is giving me a null : Node type = doc.getElementsByTagName("type").item(x); And it shouldndt since ther eis a in my xml file. – Sembrano Jan 27 '14 at 16:49
  • First rule of SO: If you have an exception, POST THE ENTIRE STACK TRACE. – Jim Garrison Jan 27 '14 at 17:02
  • Ok I have done that now. – Sembrano Jan 28 '14 at 08:17

2 Answers2

0

I think your problem is the following line:

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

Note that i should not increment when you delete a node.

lance-java
  • 25,497
  • 4
  • 59
  • 101
0

The problem is not necessarily that your type object is null. When deleteObjType(0); is called on the simple xml you provided,

System.out.println("type : " + type); 

only uses the toString()-method of the type object instance and prints out:

type : [type: null]

if you instead do

System.out.println("type : " +type);
if(type == null) {
   System.out.println("type is null!");
}else {
  System.out.println("type is not null but an instance of ["+type.getClass().getCanonicalName()+"]");
 }

it prints in my case:

type is not null but an instance of [com.sun.org.apache.xerces.internal.dom.DeferredElementImpl]

DOM is also a bit more complex than element nodes having direct element nodes as children as you can see here . I've learned through practice that simply printing out the nodes is usually not enough. Which part of the xml do you actually want to remove?

teekoo
  • 26
  • 4