Hallo zusammen,
ich bin leider blutiger Anfänger und hoffe Ihr könnt mir helfen. Ich habe eine Methode Speichern in der ich den FileChooser verwende, klicke ich auf Cancel erscheinen haufenweise Fehlermeldungen, wie kann ich diese Abfangen? Bin über jeden Ansatz dankbar.
Grüße Dani
[CODE lang="java" title="Methode"]public void sichereTabelle(){//Speichert Tabelle in ein von dem User übergebenes Verzeichnis
JFileChooser fileauswahl = new JFileChooser();
fileauswahl.showDialog(null, "Gladiatorentabelle speichern");
File file = fileauswahl.getSelectedFile().getAbsoluteFile(); //Verzeichnis in das die Datei geschrieben
System.out.println(file);
try
{
FileWriter fw=new FileWriter(file);
for(int row=0;row<kampftabelle.getRowCount();row++)
{
StringBuilder sb=new StringBuilder();
for(int col=0;col<kampftabelle.getColumnCount();col++)
{
sb.append(kampftabelle.getValueAt(row,col)).append(";");
}
sb.append(System.getProperty("line.separator"));
fw.write(sb.toString());
}
fw.close();
}
catch(Exception e)
{
System.out.println("Schreibfehler: "+e.getMessage());
}
}[/CODE]
[CODE lang="java" title="Felermeldungen"]Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "java.io.File.getAbsoluteFile()" because the return value of "javax.swing.JFileChooser.getSelectedFile()" is null
at Projekt_v01.GUI_Gladiatoren.sichereTabelle(GUI_Gladiatoren.java:266)
at Projekt_v01.GUI_Gladiatoren$4.actionPerformed(GUI_Gladiatoren.java:130)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
at java.desktop/java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:297)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6614)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6379)
at java.desktop/java.awt.Container.processEvent(Container.java:2263)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4990)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4822)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4919)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4548)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4489)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2769)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4822)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)[/CODE]
ich bin leider blutiger Anfänger und hoffe Ihr könnt mir helfen. Ich habe eine Methode Speichern in der ich den FileChooser verwende, klicke ich auf Cancel erscheinen haufenweise Fehlermeldungen, wie kann ich diese Abfangen? Bin über jeden Ansatz dankbar.
Grüße Dani
[CODE lang="java" title="Methode"]public void sichereTabelle(){//Speichert Tabelle in ein von dem User übergebenes Verzeichnis
JFileChooser fileauswahl = new JFileChooser();
fileauswahl.showDialog(null, "Gladiatorentabelle speichern");
File file = fileauswahl.getSelectedFile().getAbsoluteFile(); //Verzeichnis in das die Datei geschrieben
System.out.println(file);
try
{
FileWriter fw=new FileWriter(file);
for(int row=0;row<kampftabelle.getRowCount();row++)
{
StringBuilder sb=new StringBuilder();
for(int col=0;col<kampftabelle.getColumnCount();col++)
{
sb.append(kampftabelle.getValueAt(row,col)).append(";");
}
sb.append(System.getProperty("line.separator"));
fw.write(sb.toString());
}
fw.close();
}
catch(Exception e)
{
System.out.println("Schreibfehler: "+e.getMessage());
}
}[/CODE]
[CODE lang="java" title="Felermeldungen"]Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "java.io.File.getAbsoluteFile()" because the return value of "javax.swing.JFileChooser.getSelectedFile()" is null
at Projekt_v01.GUI_Gladiatoren.sichereTabelle(GUI_Gladiatoren.java:266)
at Projekt_v01.GUI_Gladiatoren$4.actionPerformed(GUI_Gladiatoren.java:130)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
at java.desktop/java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:297)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6614)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6379)
at java.desktop/java.awt.Container.processEvent(Container.java:2263)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4990)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4822)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4919)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4548)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4489)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2769)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4822)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)[/CODE]