• 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 trying to output an array into txt file

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Completely stumped. Trying to write on a text file from user inputs. I get a NullPointerException. I'm really stumped because I followed an example from my old work exactly as written. Only difference is that I'm writing to a text file so maybe syntax or something is not correct. Anyways I hope you can help because at this point I'm completely lost. Here's the code, it's only half the main class so the problem is at the bottom (last for loop):

 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A couple of administrative points: First, please UseCodeTags (⇐Click). I've added them for you above. Second, when you have an exception, tell us which line it's on.

OK, so your second for loop runs one more iteration than your first, so you try to print out one more pet than you have. And a bonus tip: you don't have to keep putting your Pet object into your array every time you change something. Once it's there, it stays there until replaced with another Pet object (or null), even if its internal values change. I hope that helps!
 
George Avilez
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok cool thanks for those tips. And yeah the problem starts at line 54.

And yes thank you! Silly mistake as always...
 
Greg Charles
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome! All mistakes are silly once you spot them. The trick is finding them, but that does get easier with experience.
reply
    Bookmark Topic Watch Topic
  • New Topic