• Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerException  RSS feed

 
Kiera Hakikawa
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

This program is for my ICS class. It asks to use the two classes Kiera and Trees and extend the class Trees and add two more columns to the array.

I believe my program is okay except for one thing,
I'm getting a NullPointerException at line 126 when it calls to the method setName.
Why can it not find(?) the method? What is going on?

Thank You!

 
Jason Bullers
Ranch Hand
Posts: 115
11
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm... are you sure the exception is on the call to setName(String)? Looking at the code, that doesn't make much sense to me. Your setName(String) method only performs an assignment, so it wouldn't matter if its argument (name) was null. The only other thing that could be null on that line is array(i), but if that was null, then the getName() call would have failed with an NPE.

What does seem possible is that getName() fails with an NPE, since internally it calls toUpperCase() on the name field before returning it.

Disclaimer: I haven't tried running the code yet, just read through it.
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have declared an array with size MAX of Trees. All elements default with null.
You have read the content from a file with a count.
What would happen if your file is empty and you try to change all the elements in an array which have not been initalized.
 
Jason Bullers
Ranch Hand
Posts: 115
11
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ack, how did I miss that! Still, that implies that the call to getName() should be generating the NPE, so I'm not completely nuts.
 
Kiera Hakikawa
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raymond Tong wrote:You have declared an array with size MAX of Trees. All elements default with null.
You have read the content from a file with a count.
What would happen if your file is empty and you try to change all the elements in an array which have not been initalized.



Hello,

Sorry, but I still don't understand what is happening. Aren't the elements initialized in the main method?
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code in main()

Will create an array of Trees objects that will be initialized with null references. To initialize them you'd need to add something like this also.
>
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kiera Hakikawa wrote:Sorry, but I still don't understand what is happening. Aren't the elements initialized in the main method?

Well, one thing that would be very useful is if you provide us with an exact copy of the NPE stacktrace you're getting.

Perhaps only the first 10 lines or so if it's very big.

Winston
 
Kiera Hakikawa
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Kiera Hakikawa wrote:Sorry, but I still don't understand what is happening. Aren't the elements initialized in the main method?

Well, one thing that would be very useful is if you provide us with an exact copy of the NPE stacktrace you're getting.

Perhaps only the first 10 lines or so if it's very big.

Winston


Apologies for my stupidity as I've found the reason for the exception,
I had the csv file and java file in different files.

Sorry for time wasted.
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not wasted time if we helped you find a solution. And we all make "stupid" mistakes; it comes with the territory.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!