-1

I want to insert nodes which are fetched from other part of the program into a NodeList.

static void NodeCheck(Node node)
{
    NodeList NodeArray = null;
    for(int i=0;NodeArray.getLength()<=i;i++)
    {
        NodeArray.item(i)=node;
    }
} 

here it goes, the NetBeans gives me the error that I need a variable here not value. what would be the solution?

EDIT: Ok let change to this

NodeList NodeArray = null;

        NodeArray.item(0)=node;

there is no iteration and still same error,,,,

EDIT NO.2: let me make my question more clear, how can I insert nodes (single nodes) into something like an array? ant idea? the reason to do this, is to compare them with each other to see weather they are same or different.

Cœur
  • 37,241
  • 25
  • 195
  • 267
lonesome
  • 2,503
  • 6
  • 35
  • 61

6 Answers6

2

This line will set the NodeArray to null.

  NodeList `NodeArray` = null;

In the next line you're trying to call a method on this NodeArray which is null, this can't work.

  NodeArray.getLength()

You have to initialize the NodeArray properly to a non null value.

flash
  • 6,730
  • 7
  • 46
  • 70
0

You cannot insert into org.w3c.dom.NodeList. Use appropriate element methods e.g. org.w3c.dom.Node#appendChild.

kan
  • 28,279
  • 7
  • 71
  • 101
0

It's possibly nullPointerException because NodeArray is null when you call length() method for it.

shift66
  • 11,760
  • 13
  • 50
  • 83
0

i think that the first problem you have is that the NodeArray is null, and you cant acces to a property of a null element... try doing something like this:

NodeList NodeArray = new NodeList();
melli-182
  • 1,216
  • 3
  • 16
  • 29
  • org.w3c.dom.NodeList is an interface. You're code won't work. – flash Nov 29 '11 at 10:19
  • Oh i thought that he created her own Class called NodeList (there was none import statement)... If he is using the interface... the problem is different... – melli-182 Nov 29 '11 at 10:22
0

You have to assign "nodeArray", also you get a NullPointerException on the line "NodeArray.getLength()". I also recommends to start variables and functions names by lowercase (by convention).

static Node rootNode; 
static void nodeCheck(Node node)
{
    NodeList nodeArray = rootNode.getChildNodes(); // if rootNode==null -> NullPointerException
    for(int i=0;nodeArray.getLength()<=i;i++) // if nodeArray==null -> NullPointerException
    {
        //replace child of rootNode by node
        rootNode.replaceChild(node,nodeArray.item(i));           
    }
    //add child to rootNode
    rootNode.appendChild(node); 
}

Best regards, Julien

Julien DAUPHANT
  • 250
  • 2
  • 10
0

When you have nothing there and you are iterating it, it will result null pointer.

The NodeList should be initialized before you iterate.

Mxyk
  • 10,678
  • 16
  • 57
  • 76
Tushar Agarwal
  • 521
  • 1
  • 16
  • 39