#Trying to serialize an arraylist but it just keeps returning weird characters

1 messages · Page 1 of 1 (latest)

amber marlin
#
ArrayList<CreatePet> list = null;


public void savePet(String name, String breed, int age, String attribute, int attVal) {
        petHolder.add(new CreatePet(name, breed, age, attribute, attVal)); 
        
        try {
            FileOutputStream fileOut = new FileOutputStream("Pet.ser");
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
            
            
            out.writeObject(petHolder);
            
            out.close();
            fileOut.close();
            
            System.out.println("Data saved in Pet.ser");
        } catch (IOException i) {
            i.printStackTrace();
        }
    }
    
    public void openSavedPet() {
        try {
            FileInputStream fileIn = new FileInputStream("Pet.ser");
            ObjectInputStream in = new ObjectInputStream(fileIn);
            
            list = (ArrayList) in.readObject();
            
            in.close();
            fileIn.close();
            
            for (CreatePet petHolder : list) {
                System.out.println(petHolder);
            }
 
        } catch(IOException i){
            i.printStackTrace();
        } catch (ClassNotFoundException c) {
            c.printStackTrace();
        }
    }

Output: "CreatePet@531be3c5" I think this is the memory address.
Here's the CreatePet constructor

public CreatePet(String petName, String breed, int petAge, String attribute, int attVal) {
        super(petName, breed, petAge, attribute, attVal);
    }

I'm really stumped on this one and I think I'm going in the complete wrong direction. I would greatly appreciate any help.

astral breachBOT
#

This post has been reserved for your question.

Hey @amber marlin! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

amber marlin
#

.

frail birch
amber marlin
#

is it not already being created before serialization?

frail birch
#

That's not my point. Did you try?

amber marlin
#

yes

frail birch
#

And the result is?

amber marlin
#

same result

frail birch
#

So is serialization at fault for it?

amber marlin
#

I thought it was already being made before serialization

#

I think its adding the info to the arraylist thats the problem. I'm about to try something

frail birch
#

Don't bother too long though.
So, CreatePet@531be3c5 is not what you expected to see. Okay. May I ask what were you expecting to see instead?

amber marlin
#

I'm expecting to see it print out the values inside the list

frail birch
#

And what would that look like?

amber marlin
#

<whatever the name is> <whatever the breed is> <the age> <the attribute> <the attributevalue>

frail birch
#

Hmm. Isn't that weird? Why not separated by commas, or hyphens, or newlines?

amber marlin
#

I'm planning on dealing with that later. Right now I just want to figure out how to print the data. After that I can style it

frail birch
#

good. Feel free to ask again

amber marlin
#

ok. How do I serialize, deserialize, and then print out the values in an arraylist

frail birch
#

..... Same way as you print out the values in an arraylist without having done any serialization nor deserialization

amber marlin
#

1 sec

amber marlin
#

i think i figured out the problem

#

I was never actually setting the the values so they would return null. When I println(petHolder) it just prints the memory position of pet holder. I need to actually tell it what to print in petholder

frail birch
# amber marlin I was never actually setting the the values so they would return null. When I pr...

At least that should have been what you figure first.
It so happens that Java provides a mechanism to define how objects are supposed to represent themselves textually.
Whenever you try to convert an object to String, its method toString() is called. The result is used as the text representation of the object.
So, you could redefine the method toString() in your CreatePet class, so as to make to convert itself to String the way you prefer

#

Alternative: whenever possible, you may want to use records rather than old plain classes for your data holder types.
records automatically define a good enough toString() method when you don't define one yourself.