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 Error
Page Index Toggle Pages: 1
NullPointerException Error (Read 4982 times)
NullPointerException Error
Dec 23rd, 2007, 4:06am
 
Hey everyone. I'm getting this error after running my code

Quote:


void setup()
{
 
String[] lines = loadStrings("database_clean.txt");

Record[] drivers = new Record[lines.length];
int driverCount = 0;

for (int i = 0; i < lines.length; i++)
{
 if (lines[i].startsWith("Card:"))
 {
   drivers[driverCount].cardName = lines[i];
   driverCount++;
 }
}

for (int i = 0; i < driverCount; i++)
println(i + "-->" + drivers[driverCount].cardName);
}

class Record {
 String cardName;
}


Here's the link to the database_clean.txt
http://sparragus.freehostia.com/database_clean.txt

Error is ...
Quote:
java.lang.NullPointerException

at Temporary_774_8919.setup(Temporary_774_8919.java:13)

at processing.core.PApplet.handleDisplay(PApplet.java:1390)

at processing.core.PGraphics.requestDisplay(PGraphics.java:690)

at processing.core.PApplet.run(PApplet.java:1562)

at java.lang.Thread.run(Thread.java:613)


I'm running Processing 135 in Mac OS 10.5
Thanks in advance.
Re: NullPointerException Error
Reply #1 - Dec 23rd, 2007, 8:51am
 
this:

new Record[lines.length];

just creates the array but it is still empty (filled with "null"s).
that's why this:

drivers[driverCount]

raises the error.
you have to create every object before you can use it:

if (lines[i].startsWith("Card:"))
 {  
drivers[driverCount] = new Record();
drivers[driverCount].cardName = lines[i];
...


F
Re: NullPointerException Error
Reply #2 - Dec 24th, 2007, 6:12am
 
Now I'm getting this...

/tmp/build2333.tmp/Temporary_5429_4545.java:13:39:13:39: Syntax Error: ArrayInitializer expected after this token

because of this...
Quote:
void setup()
{
 
String[] lines = loadStrings("database_clean.txt");

Record[] drivers;
int driverCount = 0;

for (int i = 0; i < lines.length; i++)
{
 if (lines[i].startsWith("Card:"))
 {
   drivers[driverCount] = new Record[];
   
   drivers[driverCount].cardName = lines[i];
   driverCount++;
 }
}

for (int i = 0; i < driverCount; i++)
println(i + "-->" + drivers[driverCount].cardName);
}

class Record {
 String cardName;
}


Re: NullPointerException Error
Reply #3 - Dec 24th, 2007, 8:32am
 
try this

Code:

void setup()
{

String[] lines = loadStrings("database_clean.txt");

Record[] drivers = new Record[lines.length];
int driverCount = 0;

for (int i = 0; i < lines.length; i++) {
if (lines[i].startsWith("Card:")){
drivers[driverCount] = new Record();

drivers[driverCount].cardName = lines[i];
driverCount++;
}
}

for (int i = 0; i < driverCount; i++)
println(i + "-->" + drivers[i].cardName);
}

class Record {
String cardName;
}


There was 3 things that didn't work.
1) You needed to specify an array size (like you did in your first post)

2) you need to write
Quote:
drivers[driverCount] = new Record();
and not
Quote:
drivers[driverCount] = new Record[];
() is not the same as []

3) Change "driverCount" by "i" in this line to avoid an ArrayOutOfBound Error
Quote:
println(i + "-->" + drivers[driverCount].cardName);
Re: NullPointerException Error
Reply #4 - Dec 24th, 2007, 11:33am
 
Damn. Thank you Steven and fjen. I love this forum. Thank to you guys one learns a lot. Dont be surprised if I bring up some more of my buggy code. Hehe, I always try to do my best, but there are times I have no idea what the hell is messing the code.

Thank you once again.
Page Index Toggle Pages: 1