Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerException  RSS feed

 
Jing Liang
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my codes, I put in System.out.println("got to a")...("got to b"). It is able to print out "got to a" but not "got to b". "got to b" is put after for (int i=0; i<size; i++){...} In my console, I have NullPointerException after "got to a". Looks like my program never able got pass that for loop in the display() method. Could anyone help me out please?

There are my codes


[edited to put in code tags]
[ October 27, 2006: Message edited by: fred rosenberger ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you get one "got to a", but not any "got to b"s, then at some point the second time through your loop your getting a null object somewhere.

sprinkle the inside of your loop with more "got to X" messages and see what happens.
 
Jing Liang
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I put a few extra System.out, "got to 1"..."got to 3". They all print to the console except "got to b"

Here are my codes


[ October 27, 2006: Message edited by: fred rosenberger ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't compile your code, because i don't have the person class. everything prints exactly once?
 
Jing Liang
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi fred,

Thank you for helping me out. I have post the necessary files in the following website http://www.ecst.csuchico.edu/~jhliang/csci111/lab6/lab6.html
you need all these classes to run it, of course you have to compile FamilyTree.java too. Once you have FamilyTree.java compiled, type "java Main restore" in the console to run it. Thanks
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Against my better judgement, I downloaded and compiled your code. I was surprised to find out that the NullPointerException thrown contained no useful information. I looks like somewhere (probably in your main method) you swallow the stack trace of the NullPointerException, and only print its name. Without seeing the stack trace, and since you only provided compiled classes and no source code, it hard to even say if the NPE even occured in the FamilyTree class. Its likely that one of the (public?) fields in one of your Person instances is null and you are calling a method on this null field.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, i'm not sure what's going on. your array of persons is populated, and has a length of 5. the first two persons seem fine.

you get to familyRecord[2], and it is indeed not null, but when you call getName() on it, the null pointer error is thrown.

are you sure that all the object put into familyRecord are being created properly? can you examine all of those?

here's what i added at the top of your display() method:


and here is the output:

H:\HFJava\slop>java Main restore
All of the records
length is 5
NIKKO, Katy
DAVISON, Michael
Person@de6ced
not null
java.lang.NullPointerException


so, it looks like the getName is throwing a NPE on the third person. if you didn't write that code, i think it's not your fault.
 
Jing Liang
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, the NPE did not point to anything at all.

The variables that I used from the Person class are parents, lotsChildren and children. In my FamilyTree.class, I checked if they are null before calling them. I don't know where the NPE is coming from. I have the source code of the Person.class and Main.class.

http://www.ecst.csuchico.edu/~jhliang/csci111/lab6/lab6.html

Thanks guys
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just like I thought, you were catching Exception in main() and swallowing the stack trace. When I print the stack trace I get:
So I changed the getName() method in Person to read:

And this was the output:
 
Jing Liang
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Garrett

How do you print the stack trace? My console only prints NullPointerException and that is it. Nothing else.

Thanks
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jing Liang:
Hi Garrett

How do you print the stack trace? My console only prints NullPointerException and that is it. Nothing else.

Thanks
In Main class I changed all these to
 
Jing Liang
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Fred and Garrett. I appreciate for all your help.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!