• Post Reply Bookmark Topic Watch Topic
  • New Topic

nullpointerexception with String???  RSS feed

 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not quite sure why I'm getting this null pointer exception with this toString method. I've looked at it over and over, and don't see a single thing wrong with it. Maybe someone has a sharper eye than me?
The output I get is the below, so it never gets into the loop, as my print statement's aren't reached:

run:
s = My Grocery Store-[
Exception in thread "main" java.lang.NullPointerException
at project4.Shop.toString(Shop.java:51)
at java.lang.String.valueOf(String.java:2827)
at java.lang.StringBuilder.append(StringBuilder.java:115)
at project4.Tester.main(Tester.java:17)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)



Thanks in advance!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"inventory" must be null; that's the only possibility. The stack trace tells you the error message is on line 51; that must be the line

for(int i = 0; i < inventory.size(); i++){

right?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:"inventory" must be null; that's the only possibility.

Not necessarily. There are also several accesses to members of inventory.get(i), so the conclusion is: either inventory is null, or one of its elements is.
 
Adam Michalik
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But there's System.out.println("Iteration"); at the beginning of the loop, which isn't printed. So I'm with Ernest - inventory must be null. Or Patrick didn't post the whole output ;) Patrick, which line is no. 51?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Michalik wrote:But there's System.out.println("Iteration"); at the beginning of the loop, which isn't printed. So I'm with Ernest - inventory must be null.

Ah yes, you're right. Although it is possible that the "regular" output and error output are mixed, the "Iteration" should at least show up anywhere, and it doesn't.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not only "Iteration" but also " System.out.println("s = " + s); " would both occur before the first call to get(). So I stand firm
 
James Brooks
Gunslinger
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D'oh! Yes, now that I look at my inventory ArrayList, it was never initialized. Thanks, I was a little confused by the stacktrace going back to String.

-Patrick
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!