• Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerException trying to output an array into txt file  RSS feed

 
George Avilez
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • 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):

 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!