FileWriter problem with my toptenScores method!!!
Hey ya'll -- i'm STILLLLLL writing this dumb trivia program. I'm now having a problem with the FileWriter - that writes the final scores BACK to the toptenFile.
I'm hoping all these troubles are just growing pains of my first "big" program (big for me) - BUT I WORKED on this honker all night last night and into this afternoon and I am at my wits end!
I am posting the writing portion below hoping I am just missing something, but if you guys need the whole code let me know -- and below this code I will show the error I am getting as it trys to write to the file.
if (questionsMissed == 3 )
{
if (score > topScoresInt[0])
{
String name = JOptionPane.showInputDialog(null,
"GAME OVER. Your score is " + score + "\n Please enter you initials - no spaces",// Message to display
"You got a High Score!", JOptionPane.INFORMATION_MESSAGE);
try {
BufferedWriter bw=new BufferedWriter(new FileWriter(toptenFile));String[] scorestoFile = new String[10];
scoreToFile[0] = (score + " " + name);
bw.write(scoreToFile[0] + "\n");
for (i=1; i<=9; i++)
{
bw.write(scoreToFile + "\n");
}
bw.close();
System.exit(0);
} catch (IOException ex) {
ex.printStackTrace();
}
HERE IS THE ERROR WHEN IT TRIES TO WRITE TO THE FILE
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.io.Writer.write(Writer.java:126)
at TriviaGame$answerButtonHandler.actionPerformed(TriviaGame.java:299)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.plaf.basic.BasicButtonListener$Actions.actionPerformed(BasicButtonListener.java:285)
at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1571)
at javax.swing.JComponent.processKeyBinding(JComponent.java:2763)
at javax.swing.JComponent.processKeyBindings(JComponent.java:2798)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2726)
at java.awt.Component.processEvent(Component.java:5265)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1810)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:672)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:920)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:798)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:636)
at java.awt.Component.dispatchEventImpl(Component.java:3841)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Window.dispatchEventImpl(Window.java:1774)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

