Computer Science Canada Creating new file? |
| Author: | Aziz [ 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:
This is the program I'm using to test:
and it gives me these run-time errors:
check here for my HighScoreList.class code. |
|||||||
| Author: | rizzix [ Mon Jun 27, 2005 1:25 pm ] |
| Post subject: | |
scores has not been initialised? |
|
| Author: | 1of42 [ Mon Jun 27, 2005 1:27 pm ] |
| Post 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. |
|
| Author: | Aziz [ Mon Jun 27, 2005 1:35 pm ] | ||||||
| Post subject: | |||||||
rizzix you're a genius! I believe I didn't do this in my constructors:
Which means it didn't have a size. Sweet. I love you man
EDIT: Except it's still giving me the same error. Something with the output stream... EDIT2: In my createFile() method, I set the line:
to
and it worked fine. I have no clue what the problem is there 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 |
|||||||