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

Username:   Password: 
 RegisterRegister   
 Creating new file?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Aziz




PostPosted: Mon Jun 27, 2005 1:17 pm   Post subject: Creating new file?

How would I go about creating a new file (like a .dat file for my highscorelist class)?

I've tried writing to it (thought it would create a new file if the file it was trying to write to didnt exist) so its either that it doesnt work, or it's giving me a NullPointerException for some reason. Here's the code I'm concerned with:

code:
       //Creates the highscore file, settings names to "---" and scores to 0
        protected void createFile() throws IOException
        {
               //New PrintWriter to output to highscore file
[line 149]  PrintWriter output = new PrintWriter(new FileWriter(fileName));
               
               //Write as many blank entries as needed
               for (int i = 0; i < scores.length - 1; i++)
               {
                      output.println("---" + "#" + 0 + "$");
               }
               
               //Close stream
               output.close();
        }


This is the program I'm using to test:

code:
import java.io.*;

public class TopList
{
        public static void main(String[] args) throws IOException
        {
                String choice;
               
                BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
                HighScoreList myScoresList= new HighScoreList(10, "toplist.txt");
               
                do
                {
                        System.out.print("Do you want to see the Top List? [Yes/No]");
               
                        choice = input.readLine();
                } while (choice.toUpperCase() != "YES" && choice.toUpperCase() != "NO");               
               
                input.close();
        }
}


and it gives me these run-time errors:

code:

Exception in thread "main" java.lang.NullPointerException
      at java.io.FileOutputStream.<init><FileOutputStream.java:172>
      at java.io.FileOutputStream.<init><FileOutputStream.java:70>
      at java.io.FileWriter.<init><FileWriter.java:46>
      at HighScoreList.createFile<HighScoreList.java:149>
      at HighScoreList.<init><HighScoreList.java:92> [thats the constructor i call in TopList.java]
      at TopList.main<TopList.java:10>


check here for my HighScoreList.class code.
Sponsor
Sponsor
Sponsor
sponsor
rizzix




PostPosted: Mon Jun 27, 2005 1:25 pm   Post subject: (No subject)

scores has not been initialised?
1of42




PostPosted: Mon Jun 27, 2005 1:27 pm   Post subject: (No subject)

Last I checked, the PrintWriter (or maybe it's the FileWriter) will create a new output file if the one you specify doesn't exist yet. Otherwise, you could use the File class to create one, I believe.
Aziz




PostPosted: Mon Jun 27, 2005 1:35 pm   Post subject: (No subject)

rizzix you're a genius! I believe I didn't do this in my constructors:

code:
scores = new int[size+1];


Which means it didn't have a size. Sweet. I love you man Dance

EDIT: Except it's still giving me the same error. Something with the output stream...

EDIT2: In my createFile() method, I set the line:

code:
PrintWriter output = new PrintWriter(new FileWriter(fileName));


to

code:
PrintWriter output = new PrintWriter(new FileWriter("test.dat"));


and it worked fine. I have no clue what the problem is there Shocked [/code]

LAST EDIT:
I got it to work. I think their was errors in the fact that it didn't have a fileName. I think I used the "this" keyword wrong...I better stay away from it Razz
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  [ 4 Posts ]
Jump to:   


Style:  
Search: