Kevin Fuentes
Kevin Fuentes

Reputation: 11

java.io.NotSerializableException: javax.swing.GroupLayout

I have this code and it throws the exception

java.io.NotSerializableException: javax.swing.GroupLayout

The code is the following

package basededatosmuebleria;
import FrontEnd.VentanaIngresoMedianteArchivo;
import java.io.*;
import javax.swing.JOptionPane;

public class FileController implements Serializable {
    public static void escribirObjetoPieza(String nombre, Pieza pieza){
        try{
            ObjectOutputStream fileOut = new ObjectOutputStream(
        new FileOutputStream(nombre));
        fileOut.writeObject(pieza);
        fileOut.close();
        }
        catch (IOException e){ 
            JOptionPane.showMessageDialog(null, e);
        }
    }
}

The class that sends invokes the method is the following ...

package basededatosmuebleria;
import FrontEnd.VentanaIngresoMedianteArchivo;
import static java.lang.Double.parseDouble;
import java.util.StringTokenizer;
import java.io.*;

public class Pieza implements Serializable {
    String tipo;
    double costo;
    VentanaIngresoMedianteArchivo comunicador = new                     
    VentanaIngresoMedianteArchivo();
    public Pieza(String tipo, double costo){
        this.tipo=tipo;
        this.costo=costo;
    }
    public Pieza(){}
    public String getTipo(){
        return this.tipo;
    }
    public double getCostoPieza(){
        return this.costo;
    }
    public void evaluarLinea(String line) {
            try{
            StringTokenizer token = new StringTokenizer(line,",");
            tipo=token.nextToken()
                    .replaceAll("PIEZA", "")
                    .replace("(", "")
                    .replaceAll("\"","");
            costo=parseDouble(token.nextToken()
                    .replace(")",""));
            Pieza pieza = new Pieza (tipo, costo);
            int contador = FileController.leerContadorPiezas(tipo);
            String nombreDelObjeto=
                    "Pieza"
                    +tipo
                    +String.valueOf(contador)
                    +".dat";
            FileController.escribirObjetoPieza(nombreDelObjeto, pieza);   
        }
        catch(NullPointerException e){
            VentanaIngresoMedianteArchivo.cajaDeMensajes.append("No se ha ingresado un valor en el precio de la pieza");
        }
        catch(NumberFormatException e){
            VentanaIngresoMedianteArchivo.cajaDeMensajes.append("El formato ingresado es incorrecto. no es un numero real");
        }
    }  
}

Upvotes: 0

Views: 963

Answers (2)

Santiago Salinas
Santiago Salinas

Reputation: 11

If you happen to be using Javax.swing and you have been using PropertyChangeSupport to keep your windows synced (or other purposes).

You may get java.io.NotSerializableException: javax.swing.GroupLayout while trying to serialize that specific class holding the PropertyChangeSupport. By changing PropertyChangeSupport to transient private PropertyChangeSupport variableName;,

while initializing you may resolve this error.

Upvotes: 1

Tom Hawtin - tackline
Tom Hawtin - tackline

Reputation: 147164

As the exception message states, javax.swing.GroupLayout is not serialisable.

What to do?

Your favourite Java Serialisation reference should give you the options in this case. Briefly your choices are:

  • Use a serialisable LayoutManager. (I never liked GroupLayout.)
  • Subclass GroupLayout with a class that implements Serializable. The subclass should copy out all of the state from the superclass in a custom writeObject and copy in back in in a custom readObject (a variation is to use a serial proxy with writeReplace and readResolve, but that's even more complication).
  • "Manually" remove the layout before serialisation and either manually replace on deserialisation or insert a serial proxy (a LayoutManager with a readResolve method).
  • Use an alternative serialisation mechanism. Java Serialisation has now been seriously, publicly proposed for removal. However, alternative serialisation mechanisms are likely to be much worse.
  • Write a "hand-rolled" specific serialiser to JSON or similar.
  • Don't serialise UIs. This is the best choice.

Upvotes: 1

Related Questions