aspose file tools*
The moose likes I/O and Streams and the fly likes Trying to grab a string from textFile, get NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Trying to grab a string from textFile, get NullPointerException" Watch "Trying to grab a string from textFile, get NullPointerException" New topic
Author

Trying to grab a string from textFile, get NullPointerException

Raven Ramirez
Greenhorn

Joined: Sep 25, 2013
Posts: 14
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.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2864
    
  11

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

Joined: Sep 25, 2013
Posts: 14
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.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2405
    
  50
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

Joined: Sep 25, 2013
Posts: 14
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

Joined: Aug 07, 2007
Posts: 2405
    
  50
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

Joined: Sep 25, 2013
Posts: 14
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

Joined: Aug 07, 2007
Posts: 2405
    
  50
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trying to grab a string from textFile, get NullPointerException