• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

nullpointerexception with String???

 
Gunslinger
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic