• 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

Trying to grab a string from textFile, get NullPointerException

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Finally had a good idea for after like 2 hours but back to being stumped. Can I still use this trick or anybody have suggestions? Here's a snippet from the code:


and here is the txt file:
Henry
2
27.3
Lucas
12
30.2
Killer
3
12.9

My plan was for array test to only grab the strings and if that worked correctly I wanted test2 to grab only the ints and test3 to grab only the doubles. I believe I already wasted 3 hours of trying to get this correct lol, that is why I'am now trying to get help...

Ps. the lines variable is the amount of lines there are in the text file, so in this case there is 9, already been tested.
 
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 probably don't need three arrays of PetRecords. I'm assuming each PetRecord contains a name, age(?), and weight(?). Just the one will be enough. However, when you instantiate an array, like on line 3 here, you just create a number of empty slots ready to hold PetRecords, not the records themselves. It takes awhile to get used to that idea, but it's very important. You'll want something like test[i] = new PetRecord(); before you try to start setting data to it.
 
Raven Ramirez
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I'm assuming each PetRecord contains a name, age(?), and weight(?). Just the one will be enough.



Yes you are indeed correct, however I did find it headaching trying to make it work. I only used one array and try using the set methods/mutators. Something like this:
PersonArray[i].setName(InputStream.next());
PersonArray[i].setAge(InputStream.nextInt());
blah blah blah...

but I still get some error doing that. You have a suggestion on how I can write this better? because yeah i only chose the previous style beacuse I though it would be easier but now that I think about I'ma have serialize the array anyways from the petRecord class.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you fill your PersonArray with objects as Greg showed?

Saying you've changed the code and still get some error doesn't help us to help you - you have to show us the new code and the error message + stack trace (remembering to wrap the code in code tags).
 
Raven Ramirez
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok awesome I got it it to somewhat work, no more errors but still no cigar. Here is the new code:



As you can see, everything works fine but how do I just get specific things? No longer any nullExceptions and the test array is finally grabbing form the text file but if i use the getName() method to see what the array exactly grabbed, it grabs everything you see on that text file when I only want to grab a string name. Thanks for the help everything is coming out smoother though
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you only want the lines containing the alpha chars then you need to read the lines containing the digits and ignore the returned values.For example: you could change your for loop to iterate for "lines / 3" times and add two more calls to read the next line, ignoring the returned data.
 
Raven Ramirez
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
grammatically I understand what you are saying, trying to interpret that in coding is difficult though. How do I exactly ignore a value if the for loop only reads one line at a time? I haven't come across something like this before so its kind of chinese right now to me, know what I mean?
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raven Ramirez wrote:grammatically I understand what you are saying, trying to interpret that in coding is difficult though. How do I exactly ignore a value if the for loop only reads one line at a time? I haven't come across something like this before so its kind of chinese right now to me, know what I mean?


The trick is for the for loop to read 3 lines at a time and ignore 2 of them. That's why I suggested the for loop should run for "lines / 3" iterations.
reply
    Bookmark Topic Watch Topic
  • New Topic