views:

620

answers:

5

hey, I have this code that should save a java.util.Vector of custom serializable classes:

if(filename.equals("")){
 javax.swing.JFileChooser fc = new javax.swing.JFileChooser();
 if(fc.showSaveDialog(this) == javax.swing.JFileChooser.APPROVE_OPTION){
  filename = fc.getSelectedFile().toString();
 }
}
try{
 java.io.FileOutputStream fos = new java.io.FileOutputStream(filename);
 java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
 java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream(baos);
 oos.writeObject((Object)tl.entities);
 baos.writeTo(fos);
 oos.close();
 fos.close();
 baos.close();
}catch(java.io.FileNotFoundException e){
 javax.swing.JOptionPane.showMessageDialog(this, "FileNotFoundException: Could not save file: "+e.getCause()+" ("+e.getMessage()+")", "Error", javax.swing.JOptionPane.ERROR_MESSAGE);
}catch(java.io.IOException e){
 javax.swing.JOptionPane.showMessageDialog(this, "IOException: Could not save file: "+e.getCause()+" ("+e.getMessage()+")", "Error", javax.swing.JOptionPane.ERROR_MESSAGE);
}

But when saving, it shows one of the defined dialog errors saying: "IOException: Could not save file: null (com.sun.java.swing.plaf.windows.WindowsFileChooserUI)" and there's an NullPointerException in the command line at javax.swing.plaf.basic.BasicListUI.convertModelToRow(BasicListUI.java:1251)

A: 

Hi, maybe you can do a better check for the filename :

if (filename == null || "".equals(filename)){
    javax.swing.JFileChooser fc = new javax.swing.JFileChooser();
    if(fc.showSaveDialog(this) == javax.swing.JFileChooser.APPROVE_OPTION){
        filename = fc.getSelectedFile().toString();
    }
    if (filename == null || "".equals(filename)) {
        // Display a message or anything else
        return;
    }
}
try {
 ...
 }
romaintaz
A: 

I don't know exactly what the problem is, since your Exception messages are not that clear. But I have 2 remarks about your code:

  1. A Null-Check of the filename (or File) would be ok (as suggested by romaintaz)
  2. Why do you change the File to its filename? Keep the File-Object and pass it to the stream.
boutta
A: 

There are a few things wrong in your code.

  • You set the filename conditionally. If you don't set it, you still try to use it.
  • You close a ByteArrayOutputStream (which is useless, see api)
  • You convert a file object back to a filename, while you can use the fileobject for writing to the stream

I would suggest to code this like this:

while(file == null) { // force a file to be choosen
    javax.swing.JFileChooser fc = new javax.swing.JFileChooser();
    if(fc.showSaveDialog(this) == javax.swing.JFileChooser.APPROVE_OPTION) {
            file = fc.getSelectedFile()
    }
    else {
        javax.swing.JOptionPane.showMessageDialog(this, "No file selected", "Error", javax.swing.JOptionPane.ERROR_MESSAGE);
    }
}

try{
        java.io.FileOutputStream fos = new java.io.FileOutputStream(file);
        java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
        java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream(baos);

        oos.writeObject((Object)tl.entities);
        baos.writeTo(fos);
        oos.close();
        fos.close();

}catch(java.io.FileNotFoundException e){
        javax.swing.JOptionPane.showMessageDialog(this, "FileNotFoundException: Could not save file: "+e.getCause()+" ("+e.getMessage()+")", "Error", javax.swing.JOptionPane.ERROR_MESSAGE);
}catch(java.io.IOException e){
        javax.swing.JOptionPane.showMessageDialog(this, "IOException: Could not save file: "+e.getCause()+" ("+e.getMessage()+")", "Error", javax.swing.JOptionPane.ERROR_MESSAGE);
}
Peter Smit
A: 

Your problem is hard to diagnose because you're not showing the stack traces of the exceptions you're catching.

And why would you serialize something into a ByteArrayOutputStream only to write the byte array to a file? Do you need to waste memory for some reason?

Michael Borgwardt
Unusual requirement :)
willcodejavaforfood
A: 

I found the error, the save dialog script itself worked perfectly but the class in the vector had a null pointer that caused the error.

But thanks for all the replies, I could use some of them :)