Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Storing data from .txt file.
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Geostigma




PostPosted: 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
Sponsor
sponsor
DemonWasp




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: