Fehler beim Schreiben einer ArrayList in eine Datei

  • Antworten:4
Andreas-George
  • Forum-Beiträge: 14

02.06.2014, 17:59:00 via Website

Also ich möchte eine ArrayList mit Objecten in eine Datei speichern. Allerdings kriege ich eine NullPointer und ich kann nicht ganz verstehen warum. Kann mir jemand nen tipp geben oder mir vielleicht sogar helfen?

Die ArrayList ist nicht Null. Allerdings versteh ich nicht den kompletten ablauf an welcher stelle genau der Fehler auftritt.

In der MainActivity rufe ich das speichern auf:

public void WriteFavorites(){
        ArrayList<ItemProperties> ser = SerializeObject.objectToArrayList(ItemList.getM_lstFav()); //Nullpointer
        if (ser != null) { //Nullpointer
            SerializeObject.WriteSettings(this, ser, "myobject.dat");
        } else {
            ser = new ArrayList<ItemProperties>();
            SerializeObject.WriteSettings(context, ser, "myobject.dat");
        }
    }

Hier die dazugehörigen funktionen:

public static void WriteSettings(Context context, ArrayList<ItemProperties> data, String filename){ 
        FileOutputStream fOut = null; 

        ObjectOutputStream oos = null;

        try{
            fOut = context.openFileOutput(filename, Context.MODE_PRIVATE);       
            oos = new ObjectOutputStream(fOut); 
            oos.writeObject(data); 
            oos.flush(); 
            Toast.makeText(context, "Settings saved",Toast.LENGTH_SHORT).show();
        } catch (Exception e) {       
            e.printStackTrace(); 
            Toast.makeText(context, "Settings not saved",Toast.LENGTH_SHORT).show();
        } 
        finally { 
            try { 
                if(oos!=null)
                    oos.close();
                if (fOut != null)
                    fOut.close(); 
            } catch (IOException e) { 
                   e.printStackTrace(); 
            } 
        } 
    }

public static ArrayList<ItemProperties> objectToArrayList(ArrayList<ItemProperties> object) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ArrayList<ItemProperties> temp = new ArrayList<ItemProperties>();
        try {
            new ObjectOutputStream(out).writeObject(object);
            byte[] data = out.toByteArray();
            out.close();

            out = new ByteArrayOutputStream();
            Base64OutputStream b64 = new Base64OutputStream(out,0);
            b64.write(data);
            b64.close();
            out.close();


            temp.addAll((ArrayList<ItemProperties>) CreateObjectFromBytes(out.toByteArray())); //Nullpointer
            //return new ArrayList<ItemProperties>(out.toByteArray());
            return temp;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return temp;
    }

public static Object CreateObjectFromBytes(byte[] yourBytes){
        ByteArrayInputStream bis = new ByteArrayInputStream(yourBytes);
        ObjectInput in = null;
        Object o = null;
        try {
          try {
            in = new ObjectInputStream(bis);
        } catch (StreamCorruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
          try {
            o = in.readObject();  //Nullpointer
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 

        } finally {
          try {
            bis.close();
          } catch (IOException ex) {
            // ignore close exception
          }
          try {
            if (in != null) {
              in.close();
            }
          } catch (IOException ex) {
            // ignore close exception
          }
        }
        return o;
    }

P.S.:Ich bin auch offen für komplett andere Lösungen!

Grüße Andreas-George

Antworten
Andreas-George
  • Forum-Beiträge: 14

02.06.2014, 19:17:16 via Website

Pascal P.

Stimmt, hilft dann das?
http://www.gutefrage.net/frage/java-arraylist-serialisieren

Werd ich gleich mal ausprobieren! Sieht schonmal gut aus. Frag mich nicht warum google mir das net ausgepsuckt hat :p

Danke schonmal. Melde mich Später!

Grüße

Edit: Alles klar, funktioniert wunderbar! Dankeschön :)

— geändert am 02.06.2014, 22:02:23

Antworten