-1

I was trying to figure out why my code is throwing a "java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: core.FlightOrder$FlightTicket" exception.

I have class declared as:

public class FlightOrder implements Serializable 

that contains a private Set of FlightTickets.

And an inner class declared as:

public class FlightTicket

The solution i read about, was to make the inner class "FlightTicket" a static class, but im not sure thats what I'm supose to do to make my code work properly. can someone help me figure out whats the right way to approach this problem?

public class FlightTicket
{
    /**
    * The passenger for this ticket
    */
    private Customer passenger;
    /**
     * passenger's seat
     */
    private int seat;
    /**
     * passenger's row
     */
    private int row;
    /**
     * The passenger's class in the plane 
     */
    private E_ClassType classType;

    /**
     * Full constructor
     * @param passenger
     * @param seat
     * @param row
     * @param classType
     */
    public FlightTicket(Customer passenger, int seat, int row , String classType)
    {
        this.passenger = passenger;
        setSeat(seat);
        setRow(row);
        setClassType(classType);
    }

    /**
     * Partial constructor
     * @param seat
     * @param row
     */
    public FlightTicket(int seat, int row)
    {
        setSeat(seat);
        setRow(row);
    }

    //-------------------------------Getters And Setters------------------------------
    /**
     * seat has to be positive number
     * @param seat
     */
    public void setSeat(int seat) {
        if(seat>0)
            this.seat = seat;
    }


    /**
     * row has to be positive number
     * @param row
     */
    public void setRow(int row) {
        if(row>0)
            this.row = row;
    }

    /**
     * 
     * @return classType
     */
    public E_ClassType getClassType() {
        return classType;
    }

    public int getSeat(){
        return seat;
    }
    public int getRow(){
        return row;
    }

    /**
     * set the class type from the array classType located in Constants.
     * if the classType not exists, the default value is Economy.
     * @param classType
     */
    public void setClassType(String classType) {

        for(E_ClassType c : E_ClassType.values())
        {
            if(c.toString().equals(classType))
            {
                this.classType = E_ClassType.valueOf(classType);
                return;
            }
        }
        this.classType = E_ClassType.Tourists;

    }

    public FlightOrder getOuterType() {
        return FlightOrder.this;
    }


    /* (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + getOuterType().hashCode();
        result = prime * result + row;
        result = prime * result + seat;
        return result;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        FlightTicket other = (FlightTicket) obj;
        if (!getOuterType().equals(other.getOuterType()))
            return false;
        if (row != other.row)
            return false;
        if (seat != other.seat)
            return false;
        return true;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "FlightTicket [passenger=" + passenger + ", seat=" + seat
                + ", row=" + row + ", flight number=" + getFlight().getFlightNumber() + "]";
    }

}// ~ END OF Inner Class FlightTicket
Roi811
  • 67
  • 1
  • 6
  • 1
    Make `FlightTicket` also implement `Serializable`? – Alan Stokes Aug 30 '14 at 20:05
  • @AlanStokes Tried that. Did not work and according to what i have read, youre no supose to do that. – Roi811 Aug 30 '14 at 20:07
  • Inner/static doesn't matter, if you want to serialize an object then it **must** be `Serializable`. – Bhesh Gurung Aug 30 '14 at 20:11
  • "Did not work" in what way? Why is `FlightTicket` not static already - how does it depend on the `FlightOrder` instance? – Alan Stokes Aug 30 '14 at 20:13
  • Every class member you use needs to be Serializable. So Customer needs to be serializable as well as FlightTicket. – Christoffer Aug 30 '14 at 20:15
  • @BheshGurung When both serialized, i get a `NullPointerException` over `getOuterType()`in FlightTicket – Roi811 Aug 30 '14 at 20:15
  • @Chris All are `Serializable`. – Roi811 Aug 30 '14 at 20:17
  • You need to provide an [SSCCE](http://sscce.org) if you want help with that exception. – Alan Stokes Aug 30 '14 at 20:42
  • @AlanStokes Thats the methods that throws the `NullPointerException` at getOuterType().hashCode(): `public int hashCode() { final int prime = 31; int result = 1; result = prime * result + getOuterType().hashCode(); result = prime * result + row; result = prime * result + seat; return result; }` – Roi811 Aug 30 '14 at 22:10

2 Answers2

2

Making inner class Serializable will work and this is exactly that you are supposed to do if you want to serialize it together with the outer class. The following code demonstrates:

public class Outer implements Serializable {
    class Inner implements Serializable {
        int value = 17;
    }

    Inner inner = new Inner();

    public static void main(String[] args) throws Exception {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ObjectOutputStream oout = new ObjectOutputStream(out);
        Outer obj = new Outer();
        obj.inner.value = 22;
        oout.writeObject(obj);
        Outer r = (Outer) new ObjectInputStream(new ByteArrayInputStream(
                out.toByteArray())).readObject();
        System.out.println(r.inner.value);
    }
}

The output is 22, the value that has been correctly serialized and deserialized from the inner class field.

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
  • When both serialized, I get a `NullPointerException` over `getOuterType()` in FlightTicket hashcode method. Might know why? – Roi811 Aug 30 '14 at 20:20
0

You just need to implement Serializable interface by all the classes which will be serialized. I mean all classes of instance variables declared in your serializable class should also implement Serializable.

In your case FlightTicket and Customer need implement Serializable.

Sergey Pekar
  • 8,555
  • 7
  • 47
  • 54