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 & HelpPrograms › NullPointerException with no line number
Page Index Toggle Pages: 1
NullPointerException with no line number (Read 673 times)
NullPointerException with no line number
May 14th, 2009, 6:03pm
 
Working on a fairly simple probgram... the portions that I think are relevant are below.

bauercalc.pde
Code:

int nProb = 0;
Problem[] allProblems;

int i=0;
int j=0;
int k=0;
int l=0;


void setup(){
//load problems file
String rawProbList[] = loadStrings("problems.txt");
String thisLine;
for(i=0;i < rawProbList.length; i++){
//above reads each line. logic follows..
thisLine = rawProbList[i];

//is it a comment?
if((thisLine.length() < 2) || (thisLine.substring(0,2).equals("//") == false)){
//it's not a comment
//Is it a header?
if(thisLine.equals("HEADER") == true){
j=0;
//First "line" of the problem
//Also update the count of valid lines...
nProb++;
}else{
//it's a content line, not header
switch(j){
case 0:
//need to make a new problem; this line gives the filename
allProblems = (Problem[])append(allProblems, new Problem(thisLine));
break;
case 1:
//update topic of this problem...
allProblems[nProb-1].topic = int(thisLine);
break;
case 2:
//update DC this problem...
allProblems[nProb-1].level = int(thisLine);
break;
case 3:
//update answer of this problem...
allProblems[nProb-1].answer = thisLine.charAt(0);
break;
}
//increment which one we're on
j++;
}
}//end of dealing with a valid line

}//end of reading problems.txt


problemObj.pde
Code:
class Problem
{
//class representing the current problem
char answer;
int topic;
int level;
String fileName;
PImage visual;

void display(int ecks, int why){
//draw it
image(visual, ecks, why);
}

Problem(String ifileName){
fileName = ifileName;
//load it
visual = loadImage(fileName);
}
}


I'm getting a NullPointerException, with output as follows:

Code:
Exception in thread "Animation Thread" java.lang.NullPointerException
at java.lang.reflect.Array.getLength(Native Method)
at processing.core.PApplet.append(PApplet.java:4691)
at bauercalc.setup(bauercalc.java:92)
at processing.core.PApplet.handleDraw(PApplet.java:1383)
at processing.core.PApplet.run(PApplet.java:1311)
at java.lang.Thread.run(Unknown Source)


I've had to deal with several NullPointerExceptions from syntax issues, but they all highlighted a specific line.  This has no line reference... and the error still occurs when I replace thisLine.length() and rawProbList.length with constants.

Any thoughts?  I'm afraid I'm new at both Processing and Java... any advice or aid would be appreciated!
Re: NullPointerException with no line number
Reply #1 - May 15th, 2009, 2:03am
 
Classical issue: you declare the array but don't initialize it.
You must add

allProblems = new Problem[INITIAL_SIZE];

where INITIAL_SIZE is an appropriately dimensioned number, to your setup().

Also I advise to look into ArrayList for such functionality.
Re: NullPointerException with no line number
Reply #2 - May 15th, 2009, 6:28am
 
Thanks for the prompt help!

BenHem: I'm pretty sure the nProb-1 isn't an issue.  I tried initialising it as Code:
int nProb =1; 

and still got the error.  It should never be a problem anyway- the file that I'm reading from has a very strict syntax, and any line that would call that switch(j) statement will come after a HEADER line- that is, nProb++ will always be executed before thy switch statement.

PhiLho: Aha!  I've kind of been confused by the array/object initialization styntax- I'm used to PHP, which of course has none of that.  I added Code:
allProblems = new Problem[0]; 

to the toppish section of setup(), and the program at least doesn't crash.  Just to confirm- that will give it an initial length of 0 elements, so it will only contain elements I append()...right?
Re: NullPointerException with no line number
Reply #3 - May 15th, 2009, 6:30am
 
OK, confirmed: allProblems.length is the expected value.  Thanks for the help!
Page Index Toggle Pages: 1