
-----------------------------------
Aziz
Mon Jun 27, 2005 1:17 pm

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:

 	//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:

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:


Exception in thread "main" java.lang.NullPointerException
      at java.io.FileOutputStream.
      at java.io.FileOutputStream.
      at java.io.FileWriter.
      at HighScoreList.createFile
      at HighScoreList. [thats the constructor i call in TopList.java]
      at TopList.main


check [url=http://www.compsci.ca/v2/viewtopic.php?t=9375]here for my HighScoreList.class code.

-----------------------------------
rizzix
Mon Jun 27, 2005 1:25 pm


-----------------------------------
scores has not been initialised?

-----------------------------------
1of42
Mon Jun 27, 2005 1:27 pm


-----------------------------------
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
Mon Jun 27, 2005 1:35 pm


-----------------------------------
rizzix you're a genius! I believe I didn't do this in my constructors:

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:

PrintWriter output = new PrintWriter(new FileWriter(fileName));

to

PrintWriter output = new PrintWriter(new FileWriter("test.dat"));

and it worked fine. I have no clue what the problem is there  :shock: [/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 :P
