• Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerException  RSS feed

 
Cody Moss
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy all!

Working on a program that creates an array from a file, organizes it in various ways, and then prints the results. Right now I'm specifically having trouble with the printAry() method - when I try to run it, I receive a NullPointerException for the active line in printAry() (EDIT: line 102). Not sure which sections are important to this, so I will include the client class and most of the service. I'd really appreciate and help that could be provided I realize that the amount of documentation I included is not necessary, but it is required for the class.






 
Paul Clapham
Sheriff
Posts: 22834
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say that if you're asking about the printAry() method, then that's one of the things you should be posting.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 102 is presumably the printInfo() method which is close enough to printAry()

So what could be null at:


Could studentAry be null?
Does every entry in this array have a value? If any of them are null then calling toString() on them would throw the exception.
How many elements are in your array?
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

All 50 slots of studentAry are probably not filled in, therefore, null;
Line 1: should be: i < count
 
Cody Moss
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
All 50 slots of studentAry are probably not filled in, therefore, null;
Line 1: should be: i < count


Thank you so much! Worked like a charm

EDIT: but now nothing will print! Should I make a separate thread to figure out what's going on there?
 
Paul Clapham
Sheriff
Posts: 22834
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


That looks a bit odd to me. Did you really mean to use "toString(i)" there instead of "toString()"?
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So far all you are printing is the element in the array (calling its toString method).

Putting in a couple more System.out.println() statements might help you see what is happening in your program.

for instance
 
Cody Moss
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:

That looks a bit odd to me. Did you really mean to use "toString(i)" there instead of "toString()"?


We are supposed to use a separate toString method to incorporate printf into the mix, I forgot to include it! Haven't written the printf yet, but the placeholder is below:



 
Paul Clapham
Sheriff
Posts: 22834
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It still looks odd. What's the parameter for? -- you don't use it anywhere inside the method, and normally a toString() method doesn't have any parameters.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't call println(myObject.toString())
Call printlin(myObject) instead. If the object is not null it calls toString automatically, and if it is null prints “null”. Then you know where the problem is.

It doesn't work well for this sort of class
 
Thomas Gard
Ranch Hand
Posts: 51
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Don't call println(myObject.toString())
Call printlin(myObject) instead. If the object is not null it calls toString automatically, and if it is null prints “null”. Then you know where the problem is.

It doesn't work well for this sort of class


I'm not sure what you're telling the OP here. AYK, the signature for public String toString() doesn't match the call toString(null), because that method declaration takes no reference.

Were you giving an example of what he shouldn't expect to work when you said "doesn't work well"?
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well consider the following code


What is the expected output?
Is it useful in telling if Foo is null or not?
My interpretation: Campbell was making a joke
 
Thomas Gard
Ranch Hand
Posts: 51
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:My interpretation: Campbell was making a joke


Gotcha. I didn't interpret his intent very well.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote: . . . My interpretation: Campbell was making a joke
Correct
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!