Serialize Image

Hi people !
i have a problem with this class.
I use class Pedestrian for creating units of different types, it inherited from class Unit ,what describe vehicles and units.
PTD is ingame “dictionary” of units, but actually I want to use it for creating “dictionaries” on hdd.
As you can see i tried use ObjectOutputStream/ObjectInputStream , IDe said:“Nope”.
So how i can serialize it?

Unit

import java.io.Serializable;
import org.newdawn.slick.Image;


abstract class Unit implements Serializable
{
    protected String uName;
    protected String uSpecName;
    protected String uRace;
    protected String uType;
    protected Image uActiveImage;
    protected String uActiveImageDest;
    protected Image uNorImage;
    protected String uNorImageDest;
    protected Image uSelImage;
    protected String uSelImageDest;
    protected String uComment;
    protected boolean uChosen=false;
    protected boolean uMoved;
    protected boolean uAttacked;
    protected int uFormCoordX;
    protected int uFormCoordY;
    protected int uCurCoordX;
    protected int uCurCoordY;
    protected String uAttacker;
    protected String uDefender;
    
    Unit()
    {
        
    }
}

Pedestrian

import java.io.IOException;
import java.io.Serializable;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.MouseListener;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.util.pathfinding.Mover;

public class Pedestrian extends Unit implements Serializable, Mover
{
    private boolean pRunned=false;
    private int pWS;
    private int pBS;
    private int pS;
    private int pT;
    private int pW;
    private int pI;
    private int pA;
    private int pLD;
    private int pSV;
    private int pNSW;
    private Weapon pActiveWeapon;
    
    Pedestrian ()
    {
    }
    Pedestrian (String name,String type,String race,String image,String selimage,int pws,int pbs,int ps,int pt,int pw,int pi,int pa,int pld,int psv,int pnsw ) throws SlickException
    {
        uName=name;
        uRace=race;
        uType=type;
        uNorImageDest=image;
        uSelImageDest=selimage;
        uNorImage=new Image(uNorImageDest);
        uSelImage=new Image(uSelImageDest); 
        pWS=pws;
        pBS=pbs;
        pS=ps;
        pT=pt;
        pW=pw;
        pI=pi;
        pA=pa;
        pLD=pld;
        pSV=psv;
        pNSW=pnsw;
    }
    Pedestrian (String name,String type,String race,String image,String selimage,int pws,int pbs,int ps,int pt,int pw,int pi,int pa,int pld,int psv,int pnsw,int cordX,int cordY) throws SlickException
    {
        uName=name;
        uRace=race;
        uType=type;
        uNorImage=new Image(image);
        uSelImage=new Image(selimage);
        pWS=pws;
        pBS=pbs;
        pS=ps;
        pT=pt;
        pW=pw;
        pI=pi;
        pA=pa;
        pLD=pld;
        pSV=psv;
        pNSW=pnsw;
        uCurCoordX=cordX;
        uCurCoordY=cordY;
    }

}

PTD

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import org.newdawn.slick.SlickException;

public class PTD
{
    protected Pedestrian warior;
    protected Tech mahina;
    protected Weapon gun;
    protected List<Pedestrian> street = new ArrayList();
    protected List<Tech> warfare = new ArrayList();
    protected List<Weapon> weaponry = new ArrayList();
    
    PTD() throws SlickException, FileNotFoundException, IOException, ClassNotFoundException
    {
///=========PEDESTRIAN====
    //=pimp Tau=       
        chuvak = new Pedestrian("Fire Warrior", "Pedestrian", "Tau Empire", "Resources/Graphic/Tau/Firewarrior.png", "Resources/Graphic/Tau/FirewarriorSel.png", 2, 3, 5, 4, 2, 2, 2, 8, 2, 0);
        street.add(chuvak);
        FileOutputStream fos = new FileOutputStream("tau.Infantry"); 
        GZIPOutputStream gzos = new GZIPOutputStream(fos); 
        ObjectOutputStream out = new ObjectOutputStream(fos); 
        out.writeObject(warior); 
        out.flush(); 
        out.close(); 
        FileInputStream fis = new FileInputStream("tau.Infantry"); 
        GZIPInputStream gzis = new GZIPInputStream(fis); 
        ObjectInputStream in = new ObjectInputStream(fis); 
        Pedestrian gelezen_veld = (Pedestrian)in.readObject(); 
        in.close();
}

Do you actually need to save the images into a file?

Otherwise you could simply save the Image paths, then set your image fields to null before saving the file, and then after loading the file, load the images based on the path you saved.

Actually i want to know how to save info about image into file.

I think about saving path,but how i can then restore it when i create object ?

	
	public void nullifyImages(){
		uSelImage = null;
		uNorImage = null;
		uActiveImage = null;
	}
	
	public void loadImages() throws SlickException{
		uSelImage = new Image(uSelImageDest);
		uNorImage = new Image(uNorImageDest);
		uActiveImage = new Image(uActiveImageDest);
	}

Try putting these in your unit class.
Call nullifyImages() on you object just before out.writeObject(object) and call loadImages() just after loading in.readObject().

If your program still need the object after saving you can either.

  1. pass the images to somewhere else, where you can retrieve them from, before you nullify the fields.
  2. reload them with loadImages() after saving.

it work , thank you

how i couldn’t get it early ?