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 847 times)
nullPointerException
Apr 1st, 2010, 9:35am
 
I got a nullPointerExceptio on this line:

 println("records[0].goals.length "+records[0].goals.length);//NullPointerException

goals does get created but is the data not stored or something?

the data can be vieuwed/ downloaded here:
http://doekewartena.nl/files/temp/data_voetbal3.txt

Code:

Record[] records;
int recordCount;

void setup() {
size(200, 200);

String[] lines = loadStrings("data_voetbal3.txt");
records = new Record[lines.length];
for (int i = 0; i < lines.length; i++) {
String[] pieces = split(lines[i], '\t'); // Load data into array
if (pieces.length == 33) {
records[recordCount] = new Record(pieces);
recordCount++;
}
}

println("records[0].land "+records[0].land);//IT
println("records[0].goals "+records[0].goals);//null
println("records[0].goals.length "+records[0].goals.length);//NullPointerException

}

void draw(){
background(255);
}


class Record {
String land;
int goals[];

public Record(String[] pieces) {
land = pieces[0];
int goals[] = {
int(pieces[3]), int(pieces[5]), int(pieces[7]), int(pieces[9]), int(pieces[14]), int(pieces[16]), int(pieces[18]), int(pieces[20]), int(pieces[25]), int(pieces[27]), int(pieces[29]), int(pieces[31]) };
}
}

Re: nullPointerException
Reply #1 - Apr 1st, 2010, 10:01am
 
>class Record {
>  String land;
>  int goals[];

>  public Record(String[] pieces) {
>    land = pieces[0];
>    int goals[] = { ...

you're defining that second int goals[] is a local variable within the Record constructor. it's not the same as the int goals[] in your class. that's why it's null in your println().
Re: nullPointerException
Reply #2 - Apr 1st, 2010, 10:14am
 
if i don't put int in front of this:

int goals[] = { ..

then i get

unexpected token: {

so how can i fix it?
Re: nullPointerException
Reply #3 - Apr 1st, 2010, 10:17am
 
move it up to the member definition? will that work?
Re: nullPointerException
Reply #4 - Apr 1st, 2010, 10:22am
 
Reference says :
Quote:
Curly braces are also used for defining inital values in array declarations.


You can't use curly braces to assign values to an array when it's declared already.
Re: nullPointerException
Reply #5 - Apr 1st, 2010, 10:27am
 
in other words i'm crappity smacked?
Re: nullPointerException
Reply #6 - Apr 1st, 2010, 10:39am
 
No worries Wink

I would do something like :

Quote:
class Record {
  String land;
  int[] mapping = {3, 5, 7, 9, 14, 16, 18, 20, 25, 27, 29, 31};
  String[] goals;

  public Record(String[] pieces) {
    land = pieces[0];
    goals = new String[mapping.length];
    for (int i = 0; i < mapping.length; i++) {
      goals[i] = pieces[mapping[i]];
    }
  }
}
Re: nullPointerException
Reply #7 - Apr 2nd, 2010, 1:23am
 
smart,

thx
Page Index Toggle Pages: 1