Josh Beckwith
Josh Beckwith

Reputation: 1540

Problems writing and reading ArrayList<Object> with filestream in Java

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 = {"..","..",".."};

Upvotes: 0

Views: 95

Answers (1)

Nghia Do
Nghia Do

Reputation: 2658

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

Upvotes: 1

Related Questions