• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

NullPointerException trying to output an array into txt file  RSS feed

 
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):

 
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!