• 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

Problem with arrays. StringTokenizers (null pointer)

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nevermind, I figured out a way around it - I will just put all the following data after the first line into the array, and store the first line (which is just an int value that says the number of lines after it) in a different public variable. Just did it, works like a charm!

I am trying to load a file and put its data into an array so that I can more easily use it; that's not really presenting a problem, as I can get that just fine. What I'm encountering is that when I try to extract a specific set of chars using a StringTokenizer from each element in that array I get some funky results.
This makes the array, but I can't use it how it is. I need to make it one element longer. Each time I do, though, I get a null pointer exception. (that's if I have ).

Here is where I'm using the StringTokenizer:

The last value of the array returns null (default value) which isn't that much of a surprise, since I'm making it one longer than my libraryFile array.

If I change the length of my libraryFile array to the proper size, I get a null pointer exception on the StringTokenizer.

So my issue, basically, is that I can't get my original array to be sized properly. Does anybody know why, or have any tips?>
 
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't dwell into your problem but String#Split method is suggested over StringTokenizer.
From API documentation of StringTokenizer,

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is something odd; you seem to have a variable which records how many lines the file contains. That is a potential source of errors, if the counting is incorrect. You can use a while loop and != null.

Declare line inside the try block. Do your reading from the file with a while loop.

. . . while((line = fin.readLine()) != null) . . .

If you know how to use a List (ArrayList is probably the best kind of List for this purpose) simply add lines to the List.
Then you can get them back with the method in the Collections class which changes a Collection to an array, or use the List.
 
reply
    Bookmark Topic Watch Topic
  • New Topic