0

In my Android application, I have a list of custom objects

ArrayList<Poke> pcList = new ArrayList<>();

Later in my code, I have save and load methods, that use Java OOS, and OIS to function. I have used these exact methods in other projects and know they work properly.

I believe I am having an issue with saving and loading a list of custom objects.

Here are the lines I'm calling to save, as well as load my list.

// save
oos.writeObject(pcList);

...

// load
pcList = (ArrayList<Poke>)ois.readObject();

Any ideas why I can't properly load/save with my list of custom objects?

Here is the interface that links a few similar objects:

public interface Poke {

    int getNum();

    String getName();

    String getType();

    int getValue();

    boolean getShiny();

    String getImageName();

}

Here is one of the similar object classes

public class BasicPoke implements Poke {

boolean shiny = false;

Random randomInt = new Random();
int iRandom = randomInt.nextInt(34 - 1) + 1;
int iShiny = randomInt.nextInt(31 - 1) + 1;

public int getNum(){
    return this.iRandom;
}

public String getName(){
    return this.pokeNames[iRandom-1];
}

public String getType(){
    return this.pokeTypes[iRandom-1];
}

public int getValue(){
    return 1;
}

public boolean getShiny() {
    return (iShiny == 15);
}

public String getImageName() {
    String threeDigitInteger = String.format(Locale.getDefault(),"%03d",    this.getNum());
    return (this.getShiny()) ? "icon"+threeDigitInteger+"s" : "icon"+threeDigitInteger;
}

String pokeNames = {"..","..",".."};
String pokeTypes = {"..","..",".."};
3
  • Do your custom classes implements Serializable? Commented Aug 24, 2016 at 18:35
  • @Zircon they implement my interface Commented Aug 24, 2016 at 18:38
  • But they do not implement Serializable. You will need to do that. Unfortunately, it's not always the easiest thing to do. I recommend you pick up the book "Effective Java: Second Edition" by Joshua Bloch and read his chapter about implementing the Serializable interface correctly. Once you've implemented Serializable correctly, reading/writing your objects on the file system will be a breeze. Commented Aug 24, 2016 at 18:40

1 Answer 1

1

Please implement Serializable for the BasicPoke class. And by the way, could you tell us what issue (exception) when you load/save a list of objects?

Thanks, Nghia

Sign up to request clarification or add additional context in comments.

2 Comments

There are no save or load errors. It just fails silently, as in, pcList is null even after loading. I can't implement Serializable in my BasicPoke class because it's already implementing my interface, Poke.
Edit: I was under the impression that I could only have one "implements" in my custom classes. My class already had "implements Poke" which is my custom interface. I changed that to, "implements Poke, Serializable" and it works! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.