JFileChooser & data saver error

Hi there,

I use

publicvoid savePlayers(){

try{

BufferedWriter bufferedWriter =new BufferedWriter(new FileWriter(new File("Filename.txt")));

for (int i = 0; i < table.getModel().getRowCount(); i++){

bufferedWriter.write((String) table.getValueAt(i, 0) +"|" +

(String) table.getValueAt(i, 1) +"|" +

(Integer) table.getValueAt(i, 2) +"|");

}

bufferedWriter.close();

}catch (Exception e){

e.printStackTrace();

}

}

to save my table into a txt file.

now im trying to make this work with a filechooser and I made

if (source.getSource() == (JButton) cmdSave){

JFileChooser fcs =new JFileChooser(new File("Sporters.txt"));

fcs.addChoosableFileFilter(new MyFilter());

fcs.showSaveDialog(frame);

File file2 = fcs.getSelectedFile();

savePlayers();

}

to do so.

MyFilter is just a filter filtering all *.txt files to be seen only.

Now when I try to use savePlayers and change "Filename.txt" to file2.getName() it gives me an error

//error code:

ava.lang.NullPointerException

at sportzicht.ScoreOverzicht.savePlayers(ScoreOverzicht.java:118)

at sportzicht.ScoreOverzicht.actionPerformed(ScoreOverzicht.java:253)

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.mouseReleased(BasicButtonListener.java:234)

at java.awt.Component.processMouseEvent(Component.java:5488)

at javax.swing.JComponent.processMouseEvent(JComponent.java:3093)

at java.awt.Component.processEvent(Component.java:5253)

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.LightweightDispatcher.retargetMouseEvent(Container.java:4212)

at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)

at java.awt.Container.dispatchEventImpl(Container.java:2010)

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)

What could can be the problem? What should I use instead of filename.txt/file2.getName()

or is the problem somewhere else?

[4077 byte] By [Tjirpovfa] at [2007-10-2 10:29:04]
# 1

you have this in your actionPerformed's if statement

File file2 = fcs.getSelectedFile();

which makes file2 local to the if statement.

if you are using file2.getName() in savePlayers(), and you don't get a

"cannot find symbol"

you probably have a class variable declared

File file2;

if this is what you have, change

File file2 = fcs.getSelectedFile();

to

file2 = fcs.getSelectedFile();

as an aside, to tweak the performance, this line

for (int i = 0; i < table.getModel().getRowCount(); i++)

could be written like this

for (int i = 0, ii = table.getModel().getRowCount(); i < ii; i++)

just saves the continual method calls

Michael_Dunna at 2007-7-13 2:10:56 > top of Java-index,Desktop,Core GUI APIs...
# 2
I fixed it already by calling the savePlayers function with the file2.getName();Thanks tho
Tjirpovfa at 2007-7-13 2:10:56 > top of Java-index,Desktop,Core GUI APIs...