We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › NullPointerException
Page Index Toggle Pages: 1
NullPointerException (Read 738 times)
NullPointerException
Feb 22nd, 2010, 10:44am
 
Can anyone explain why i am getting the error message "NullPointerException" please. From looking at previous posts i figure it is to do with calling an array of a different length? Can anyone explain this problem to me simply please?

Also, could help me identify where this problem might be occuring in my code (pasted below please). It doesn't happen every time - but as i add the other classes into the sketch to represent other information the error happens more often. I'm confused why it should happen some times and not others. Thanks for any help.

//loads data from an xml file
//data types are different classes, represented graphically
//feb 2010

import proxml.*;
import proxml.XMLElement;

XMLElement branches;
XMLInOut xmlInOut;

int nbrNodes = 2;
bookNode[] bookParticles;

/*..................................................................*/
void setup() {
 size(200,200);
 background(0);
 smooth();

 //load nodes from file
 xmlInOut = new XMLInOut(this);
 xmlInOut.loadElement("testData4.xml");

 for (int i = 0; i < nbrNodes; i++) {
   bookParticles = new bookNode[nbrNodes];
 }
}
/*..................................................................*/
void xmlEvent(XMLElement element){
 branches = element;

 XMLElement node;
 XMLElement title;
 XMLElement source;
 XMLElement subject1;
 XMLElement subject2;
 XMLElement subject3;
 // parse through branches: gets each row of data in xml file
 for(int i = 0; i < branches.countChildren();i++){
   //and get the elements of each row
   node = branches.getChild(i);

   //get the attributes of each element in a row - and add attibutes to kin class
   title = node.getChild(0);
   title.getAttribute("name");
   String labelName = title.getAttribute("name");
   bookParticles[i] = new bookNode(new PVector(random(width), random(height)));
   bookParticles[i].name = labelName;

   source = node.getChild(1);
   source.getAttribute("origin");

   subject1 = node.getChild(2);
   subject1.getAttribute("tag1");

   subject2 = node.getChild(3);
   subject2.getAttribute("tag2");

   subject3 = node.getChild(4);
   subject3.getAttribute("tag3");    
 }  
}
/*..................................................................*/
void draw() {
 for (int i = 0; i < bookParticles.length; i++) {
   bookParticles[i].display();
 }
}
/*..................................................................*/
void mousePressed() {
 bookNode bP;
 for(int i = 0; i < bookParticles.length; i++) {
   bP = bookParticles[i];
   if(dist(bP.position.x,bP.position.y,pmouseX,pmouseY) < 5) {
     String label = bP.name;
     println(label);
     PVector pos = bP.position;
     println(pos);
   }
 }

}
Re: NullPointerException
Reply #1 - Feb 22nd, 2010, 10:55am
 
sorry, should have mentioned that on getting the error the line of code
   bookParticles[i].display();
in draw is highlighted.

thanks for any help.
Re: NullPointerException
Reply #2 - Feb 22nd, 2010, 1:57pm
 
There seems to be some missing code (i.e. bookNode class) and it's not clear whether the xmlEvent function is actually called (unless I'm missing something obvious)...  In fact that may well be the issue as that appears to be what populates bookParticles with bookNode objects:  If it doesn't get called, bookParticles doesn't contain any bookNode objects and you're then not able to call the display() method of those objects.
Re: NullPointerException
Reply #3 - Feb 22nd, 2010, 11:15pm
 
The part: Code:
for (int i = 0; i < nbrNodes; i++) {
bookParticles = new bookNode[nbrNodes];
}
is strange, you need to initialize this array only once...
Also you should do this init in xmlEvent, with branches.countChildren() instead of an arbitrary number.
I think the error is there, if you find & create less bookNode's than nbrNodes, ie. than bookParticles.length.
Re: NullPointerException
Reply #4 - Feb 23rd, 2010, 5:16am
 
Thankyou! That worked, i put the init in XMLEvent, getting rid of draw but there seems to be some bug still.

I have put further classes in for different data types and links betwen related types of data. And re-organising as suggested it runs without an error but sometimes it doesn't seem to draw everything?? for example not all nodes are drawn, or half links appear, which are only lines drawn between nodes.

I saved it as an applet and when run in the applet it seems fine - is there an explanation for this??? Is there any reason why it should run ok in the applet and only sometimes in processing?
Re: NullPointerException
Reply #5 - Feb 23rd, 2010, 6:56am
 
You use random coordinates, so perhaps sometime they go out of the screen?
Page Index Toggle Pages: 1