Storing data from .txt file.
Author |
Message |
Geostigma
|
Posted: Fri Feb 12, 2010 9:48 pm Post subject: Storing data from .txt file. |
|
|
I'm trying to store data from a text file and save every letter as a separate character. The file I'm using has carriage returns and some kind of separator that I can't store and causes a null pointer exception. How do I bypass this?
EDIT: I forgot to initialize my array lol........
Java: | ]import java.util.*; // Needed for use of Scanner and NoSuchElementException
import java.io.*;
class arrayStorage001 {
char [] charArray;
public void fileOpen (){
try{
Scanner in = new Scanner (new FileReader("numbers.txt"));
String awesome = in. nextLine();
System. out. println(awesome. length());
for (int i= 0; i < awesome. length(); i++ ){
System. out. println(awesome. charAt(i ));
charArray [i ]=awesome. charAt(i );
}
} catch (FileNotFoundException e ) {
System. out. println("Error: Cannot open file for reading");
} catch (NoSuchElementException e ) {
System. out. println("Error: EOF encountered, file may be corrupt");
} catch (IOException e ) {
System. out. println("Error: Cannot read from file");
}
}
public static void main (String args []) {
arrayStorage001 f = new arrayStorage001 ();
f. fileOpen();
}
} |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
DemonWasp
|
Posted: Sat Feb 13, 2010 10:46 pm Post subject: RE:Storing data from .txt file. |
|
|
You've never allocated charArray, so of course accessing it throws a NullPointerException. Use new. |
|
|
|
|
|
|
|