NullPointerException Help

This is my code:

class MemberRec{

public String name;

publicstaticvoid initialise(String line, MemberRec[] member){

String line = line.split("\\s+");

member[i].name = items[0];

}

};

In the main it reads the first line from a file, and passes it here. The code compiles alright, but when i run it it gives me a NullPointerException error.

Can anyone help? Thnx.

[720 byte] By [LudaCa] at [2007-10-3 2:26:31]
# 1
> The code compiles alright,No it doesn't. The compiler will be upset by "items".Post something brief that does compile and which shows the problem. Cut and paste the error message and make sure the source line that it refers to isidentifiable.
pbrockway2a at 2007-7-14 19:25:35 > top of Java-index,Java Essentials,New To Java...
# 2
soz, that line should be:String[] items = line.split("\\s+);
LudaCa at 2007-7-14 19:25:35 > top of Java-index,Java Essentials,New To Java...
# 3
My crystal ball reveals something like this:MemberRec[] member = new MemberRec[10];String s = //.....initialise(s, member);
floundera at 2007-7-14 19:25:35 > top of Java-index,Java Essentials,New To Java...
# 4
> My crystal ball reveals something like thisYes all wrapped up in a for-loop. Most likely the array elementsBut there are several variables in the posted snippet that could be null.Is it asking too much to be given a hint?
pbrockway2a at 2007-7-14 19:25:35 > top of Java-index,Java Essentials,New To Java...
# 5

In my experience noobs believe the below will automagically generate ten objects and store them in the array for them. So my money is one the array being empty.

Object[] obj = new Object[10];

Yeah, but seeing more code would help find the problem.

Message was edited by:

flounder

floundera at 2007-7-14 19:25:35 > top of Java-index,Java Essentials,New To Java...
# 6

Ok well this is how i'm declaring the array:

MemberRec member[] = new MemberRec[num]

//num is a final static int equal to 300

Then i'm calling the function from the main like this:

MemberRec.initialise(line, member);

LudaCa at 2007-7-14 19:25:35 > top of Java-index,Java Essentials,New To Java...
# 7
> Ok well this is how i'm declaring the array:MemberRec member[] = new MemberRec[num];This is well and good but are you creating 300 MemberRec objects and storing them in the array?Message was edited by: flounder
floundera at 2007-7-14 19:25:35 > top of Java-index,Java Essentials,New To Java...
# 8

> Ok well this is how i'm declaring the array:

>

> MemberRec member[] = new MemberRec[num]

>

> //num is a final static int equal to 300

You now have an array of 300 references to MemberRec. Each of those references is null. You would need to do something like this: for (int ix = 0; ix < member.length; ix++) {

member[ix] = new MemberRec(); \

}

> Then i'm calling the function from the main like

> this:

>

> MemberRec.initialise(line, member);

If you haven't initialized the elements of the array, and you try to access member[ix].someMethodOrVariable, you'll get the NPE you're observing.

jverda at 2007-7-14 19:25:35 > top of Java-index,Java Essentials,New To Java...
# 9

The lineMemberRec member[] = new MemberRec[num]

creates an array of MemberRecs all of which are null. Later, when you refer to

member[i].name you will get the NullPointerException. You have to initialise

all the elements of the array. Like thisMemberRec member[] = new MemberRec[num];

for(int i = 0; i < num; i++) {

member[i] = new MemberRec();

}

This assumes that flounder's crystal balls aren't playing up and that the NPE

occured with member[i].name. Some compilable code really would settle

the matter. Really.

pbrockway2a at 2007-7-14 19:25:35 > top of Java-index,Java Essentials,New To Java...
# 10
> This assumes that flounder's crystal balls aren't> playing up I don't think a Java forum is the appropriate place to discuss flounder's medical problems.
jverda at 2007-7-14 19:25:35 > top of Java-index,Java Essentials,New To Java...
# 11
But the topic of goatsex is not taboo?
floundera at 2007-7-14 19:25:35 > top of Java-index,Java Essentials,New To Java...
# 12
Awesome, you guys are legends. That fixed it up. Thanx heaps.
LudaCa at 2007-7-14 19:25:35 > top of Java-index,Java Essentials,New To Java...
# 13
> Awesome, you guys are legends. That fixed it up.If only flounders balls were as easy to fix.
cotton.ma at 2007-7-14 19:25:35 > top of Java-index,Java Essentials,New To Java...
# 14
Pity the woman don't appear to be as interested as you guys!
floundera at 2007-7-14 19:25:35 > top of Java-index,Java Essentials,New To Java...
# 15

Ok i have a new problem now, lol.

I'm trying to get the program to read along a line of char's in a file, into an array, and when the integer it reads is "0", it stops looping.

for(;;) {

if (items[j]=="0") break;

member[i].array[k] = Integer.valueOf(items[j]);

j++;

k++;

}

The problem its having is that when it reads "0" its not stopping, and the next character in the line is of a different variable type, so the compiler is havin a cry to me.

LudaCa at 2007-7-21 9:53:55 > top of Java-index,Java Essentials,New To Java...
# 16
This is a classic error people make when comparing strings. Use equals() instead of ==.
floundera at 2007-7-21 9:53:55 > top of Java-index,Java Essentials,New To Java...
# 17
Rippin, thanx heaps.I've been using C++ for the past year and a half so i'm used to that.
LudaCa at 2007-7-21 9:53:55 > top of Java-index,Java Essentials,New To Java...