简体   繁体   中英

Store inherited class object in codename one storage

I'm using the codenameone framework with java. I've been a serious issue saving a List of a objects of a class farmer inheriting from a class Person. Both class implement the externalizable interface and i've correctly implemented the four required methods ( getVersion, externalize, internalize, getObjectId) in both classes too. I call super.initialize() as the first line of the initialize method in the farmer class and do the same thing for the externalize method too.

I can save a List but i can't seem to read it from storage after closing and restarting the simulator. It just gives me a list with first element being a Farmer but others elements being null.or Date values.

I've been seeking for solutions for this for several days. Any tip or advice would be really much appreciated from you. Thank you in advance.

I'm guessing that you implemented this method like that:

public String getObjectId() {
   return getClass().getName();
}

Instead of like this:

public String getObjectId() {
   return "MyObject";
}

You need to give a unique per object name for a class and need to have it hardcoded as class names might differ on the devices due to obfuscation.

Here are the methods for the Person class

public void externalize(DataOutputStream out) {
            try {
                //Personne
                Util.writeUTF(getNomPersonne(), out);
                Util.writeUTF(getPrenomPersonne(), out);
                Util.writeUTF(getAdressePersonne(), out);
                Util.writeUTF(getLieuNaisPersonne(), out);
                Util.writeUTF(getPhotoPersonne(), out);
                Util.writeUTF(getSituationMatrimonialePersonne(), out);
                Util.writeUTF(getTelPersonne(), out);

                out.writeLong(getIdPersone() == null ? 0 : getIdPersone());
                out.writeInt(getVERSION() == null ? 0 : getVERSION() );
                out.writeChar(getSexePersonne());

                Util.writeObject(getDateNaisPersonne(), out);
                Util.writeObject(getLocalisationPersonne() , out);
            } catch (IOException ex) {
                Dialog.show("Erreur ","L'exception "+ex.getMessage(),"Ok",null);
            }
        }

    public void internalize(int version, DataInputStream in){
            try {
                nomPersonne = Util.readUTF(in);
                prenomPersonne = Util.readUTF(in);
                adressePersonne = Util.readUTF(in);
                lieuNaisPersonne =  Util.readUTF(in);
                photoPersonne = Util.readUTF(in);
                situationMatrimonialePersonne = Util.readUTF(in);
                telPersonne = Util.readUTF(in);

                idPersone = in.readLong();
                VERSION = in.readInt();
                sexePersonne = in.readChar();

                dateNaisPersonne = (Date) Util.readObject(in);
                localisationPersonne = (StructureAdministrative) Util.readObject(in);
            } catch (IOException ex) {
                 Dialog.show("Erreur ","L'exception "+ex.getMessage(),"Ok",null);
            }
        }

An here come the methods for the farmer class which inherits from Person

@Override
    public void externalize(DataOutputStream out) {
        try {
            //Personne
            super.externalize(out);
            //Agriculteur
            Util.writeUTF(this.getNumPieceIdentite(), out);
            Util.writeUTF(this.getPhotoPieceIdentite(), out);
            Util.writeUTF(this.getPhotoPieceRecto(), out);
            Util.writeUTF(this.getPhotoPieceVerso(), out);
            Util.writeUTF(this.getTelAgripme(), out);

            out.writeInt(this.getNombreEnfant() == null ? 0 : this.getNombreEnfant()  );
            out.writeInt(getTailleMenage() == null ? 0 : getTailleMenage());
            out.writeDouble(getDistanceParcourue() == null ? 0 : getDistanceParcourue());
            out.writeDouble(getDureeAttente() == null ? 0 : getDureeAttente());
            out.writeBoolean(getaPiece() == null ? false : getaPiece());
            out.writeBoolean(getMembreCooperative() == null ? false : getMembreCooperative());
            out.writeInt(getParidPersone() == null ? 0 : getParidPersone());


            Util.writeObject(getDateExpirPiece(), out);
            Util.writeObject(getTypePieceIdentite(), out);
            Util.writeObject(getTypeProprieteFoncier() , out);
            Util.writeObject(getListeChamps(), out);
        } catch (IOException ex) {
            System.out.println("ERREUR DE L'ECRITURE DES AGRICULTEUR EST"+ ex.getMessage());
        }
    }

@Override
public void internalize(int version, DataInputStream in)  {
    try {
        //Personne
        super.internalize(version, in);
        //Agriculteur
        numPieceIdentite = Util.readUTF(in);
        photoPieceIdentite = Util.readUTF(in);
        photoPieceRecto = Util.readUTF(in);
        photoPieceVerso = Util.readUTF(in);
        telAgripme = Util.readUTF(in);

        nombreEnfant = in.readInt();
        tailleMenage = in.readInt();
        distanceParcourue = in.readFloat();
        dureeAttente = in.readFloat();
        aPiece = in.readBoolean();
        membreCooperative = in.readBoolean();
        ParidPersone = in.readInt();


        dateExpirPiece = (Date) Util.readObject(in);
        typePieceIdentite = (TypePieceIdentite) Util.readObject(in);
        typeProprieteFoncier = (TypeProprieteFoncier) Util.readObject(in);
        listeChamps = (ArrayList<Champ>) Util.readObject(in);
    } catch (IOException ex) {
        System.out.println("LA LECTURE DES AGRICULTEURS DE LA BASE A TEL PROBLÈME "+ ex.getMessage());
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM