Map<UUID, MyOwnClass> gets error

I am trying to read a file and put it into my Map<UUID, MyOwnClass> but it says:

java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.util.Map
      dk.bec.dao.DeploymentDao.getDeployments(DeploymentDao.java:15)


I think everything looks right.

First I do this
---------------------
 try {
        String fileName2 = getServletContext().getInitParameter("deploymentFile");
        Map<UUID, Deployment> deployments = new HashMap<UUID, Deployment>();
        Deployment deployment = new Deployment();
        deployment.setId(UUID.randomUUID());
        deployments.put(deployment.getId(), deployment);
        SerializableUtil.write(deployments, fileName2);
        } catch (ClassNotFoundException e) {
        throw new ServletException("Bang!!!");
        }
---------------------

the list is created
then when I try to read it back
--------------------

 public Map<UUID, Deployment> getDeployments() throws IOException, ClassNotFoundException {
        Map<UUID, Deployment> deployments = (Map<UUID, Deployment>) SerializableUtil.read(fileName);
        return deployments;
    }

-----------------------------------
this is where I get the error. The serializableutil are returning an object with the correct things inside. (except UUID is null)
-----------------------------
public static Object read(String name) throws IOException, ClassNotFoundException, FileNotFoundException {

        FileInputStream fis = new FileInputStream(name);
        ObjectInputStream ois = new ObjectInputStream(fis);
        Object o = ois.readObject();
        fis.close();

        return o;
    }

Open in new window

MickeysAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ksivananthCommented:
looks like SerializableUtil.read(fileName);  returns ArrayList which can be casted to Map!
0
ksivananthCommented:
>>which can be

typo, it should be "which can't be"

so either you should read to return ArrayList or create map from the returned list!
0
MickeysAuthor Commented:
How do I create a Arraylist to a map then?
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

ksivananthCommented:
>> Object o = ois.readObject();

what is the actual type the o holds?

>> The serializableutil are returning an object with the correct things inside. (except UUID is null)

what do u mean by correct things here?
0
MickeysAuthor Commented:
Look at the code below:

serverMap works fine
testMap doesent
/*
            try {
        Map<UUID, Server> servers = new HashMap<UUID, Server>();
        Server server = new Server();
        server.setId(UUID.randomUUID());
        server.setName("Kalle");
        servers.put(server.getId(), server);
        SerializableUtil.write(servers, fileName);
        } catch (ClassNotFoundException e) {
        throw new ServletException("Bang!!!");
        }

        try {
        String fileName2 = getServletContext().getInitParameter("deploymentFile");
        Map<UUID, Deployment> deployments = new HashMap<UUID, Deployment>();
        Deployment deployment = new Deployment();
        deployment.setId(UUID.randomUUID());
        deployments.put(deployment.getId(), deployment);
        SerializableUtil.write(deployments, fileName2);
        } catch (ClassNotFoundException e) {
        throw new ServletException("Bang!!!");
        }

    */

        try {
            Map<UUID, Server> serverMap = (Map<UUID, Server>) SerializableUtil.read(fileName);
            Map<UUID, Deployment> testMap = (Map<UUID, Deployment>) SerializableUtil.read("deployment.bin");

------------------------------------
package dk.bec.model;

import java.io.Serializable;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class Deployment implements Serializable , Comparable<Deployment>{

    private static final long serialVersionUID = 1L;
    private UUID id;
    private String name;
    private String customer;
    private String database;
    private String startDate;
    private String url;
    private String pakke;
    private String load;
    private List<String> description;
    private List<String> preDeploy;
    private List<String> deploy;
    private List<String> postDeploy;
    private List<String> buildFailureTrigger;


    public Deployment() {
        this.name = "empty";
        this.database = "empty";
        this.startDate = "empty";
        this.pakke = "empty";
        this.customer = "empty";
        this.load = "empty";
        this.url = "empty";
        
        description = new ArrayList<String>();
        preDeploy = new ArrayList<String>();
        deploy = new ArrayList<String>();
        postDeploy = new ArrayList<String>();
        buildFailureTrigger = new ArrayList<String>();

    }

    public List<String> getBuildFailureTrigger() {
        return buildFailureTrigger;
    }

    public void setBuildFailureTrigger(List<String> buildFailureTrigger) {
        this.buildFailureTrigger = buildFailureTrigger;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public List<String> getDeploy() {
        return deploy;
    }

    public void setDeploy(List<String> deploy) {
        this.deploy = deploy;
    }

    public List<String> getDescription() {
        return description;
    }

    public void setDescription(List<String> description) {
        this.description = description;
    }

    public List<String> getPostDeploy() {
        return postDeploy;
    }

    public void setPostDeploy(List<String> postDeploy) {
        this.postDeploy = postDeploy;
    }

    public List<String> getPreDeploy() {
        return preDeploy;
    }

    public void setPreDeploy(List<String> preDeploy) {
        this.preDeploy = preDeploy;
    }

    
    public String getStartDate() {
        return startDate;
    }

    public void setStartDate(String startDate) {
        this.startDate = startDate;
    }

    public String getDate() {
        return startDate;
    }

    public void setDate() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = new Date();
        this.startDate = dateFormat.format(date);
    }

     public String getLoad() {
        return load;
    }

    public void setLoad(String load) {
        this.load = load;
    }

    public String getCustomer() {
        return customer;
    }

    public void setCustomer(String customer) {
        this.customer = customer;
    }

    public String getPakke() {
        return pakke;
    }

    public void setPakke(String pakke) {
        this.pakke = pakke;
    }

    public UUID getId() {
        return id;
    }

    public void setId(UUID id) {
        this.id = id;
    }

    
    public void setDatabase(String set) {
        this.database = set;
    }

    public String getDatabase() {
        return this.database;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int compareTo(Deployment aThat) {

        final int EQUAL = 0;

        if (this == aThat) {
            return EQUAL;
        }

        final Deployment that = (Deployment) aThat;

        int comparison = this.name.compareTo(that.name);
        if (comparison != EQUAL) {
            return comparison;
        }

        return EQUAL;
    }


     @Override
    public String toString() {
        String output = "UUID: " + id
                + "\nName: " + name
                + "\nUrl: " + url
                + "\nCustomer: " + customer
                + "\nDatabase: " + database
                + "\nStartDate: " + startDate
                + "\nPakker: " + pakke
                + "\nDescription: " + description
                + "\nPreDeploy: " + preDeploy
                + "\nDeploy: " + deploy
                + "\nPost Deploy: " + postDeploy
                + "\nBuild Faliure Trigger: " + buildFailureTrigger;

        return output;
    }

}


-------------------------
package dk.bec.model;

import dk.bec.util.StringUtils;
import java.io.IOException;
import java.io.Serializable;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class Server implements Serializable, Comparable<Server> {

    private UUID id;
    private String name;
    private String status;
    private String url;
    private String hostname;
    private String port;
    private String user;
    private Deployment deployment;
    private Date dateForLastChange;
    private Properties properties;
    private List<String> propertyList;

    public Server() {
        this.name = "";
        this.status = "grey";
        this.url = "";
        this.hostname = "";
        this.port = "";
        this.user = "empty";

        this.deployment = new Deployment();
        this.dateForLastChange = new Date();

        this.propertyList = new ArrayList<String>();
        this.properties = new Properties();
    }

    public Deployment getDeployment() {

        return deployment;
    }

    public void setDeployment(Deployment deployment) {
        this.deployment = deployment;
    }

    public UUID getId() {
        return id;
    }

    public void setId(UUID id) {
        this.id = id;
    }

    public String getDateForLastChange() {
        Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String s = formatter.format(this.dateForLastChange);
        return s;
    }

    public void setDateForLastChange(Date dateForLastChange) {
        this.dateForLastChange = dateForLastChange;
    }

    public String getHostname() {
        return hostname;
    }

    public void setHostname(String hostname) {
        this.hostname = hostname;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public Properties getProperties() {
        return properties;
    }

    public List<String> getPropertiesAsString() throws IOException {
        return StringUtils.propertiesToList(this.properties);
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public List<String> getPropertyList() {
        return propertyList;
    }

        public String getpropertyListAsString() {
        return StringUtils.join(this.propertyList, "");
    }


    public void setPropertyList(List<String> propertyList) {
        this.propertyList = propertyList;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

   
    public int compareTo(Server aThat) {

        final int EQUAL = 0;

        if (this == aThat) {
            return EQUAL;
        }

        final Server that = (Server) aThat;

        int comparison = this.name.compareTo(that.name);
        if (comparison != EQUAL) {
            return comparison;
        }

        return EQUAL;
    }


     @Override
    public String toString() {
        String output = "UUID: " + id
                + "\nName: " + name
                + "\nUrl: " + url
                + "\nHostname: " + hostname
                + "\nStatus: " + status
                + "\nPort: " + port
                + "\nUser: " + user
                + "\nDeployment: " + deployment
                + "\nDate: " + dateForLastChange
                + "\nProperties: " + properties
                + "\nProp List: " + propertyList;

        return output;
    }
}
--------------------------------

public static void write(Object o, String name) throws IOException, FileNotFoundException, ClassNotFoundException {

        FileOutputStream fos = new FileOutputStream(name);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(o);
        oos.flush();
        fos.close();
    }

    public static Object read(String name) throws IOException, ClassNotFoundException, FileNotFoundException {

        FileInputStream fis = new FileInputStream(name);
        ObjectInputStream ois = new ObjectInputStream(fis);
        Object o = ois.readObject();
        fis.close();

        return o;
    }

Open in new window

0
ksivananthCommented:
if that returns of type list of Deployment, here is how you create map,

Map<UUID, Deployment> map = new HashMap<UUID, Deployment>() ;
for( Deployment d : SerializableUtil.read(fileName) ){
   map.put( UUID.randomUUID(), d ) ;
}
0
ksivananthCommented:
can you post the code of SerializableUtil.write?
0
ksivananthCommented:
slight modification

Map<UUID, Deployment> map = new HashMap<UUID, Deployment>() ;
for( Deployment d : SerializableUtil.read(fileName) ){
   map.put( d.getId(), d ) ;
}
0
MickeysAuthor Commented:
you got that two post up in the code
0
ksivananthCommented:
looks like what you have done is right, make sure you read from right file!
0
MickeysAuthor Commented:
I do that as well. :-/ I have debugged I get everyting into my object.....it is just when I return it and shall make it to Map it breaks.
0
ksivananthCommented:
are you sure nobody else is wrtiting to the same file with list first?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ksivananthCommented:
can print this

sop( o.getClass().getName() ) ;
0
MickeysAuthor Commented:
hmmm that is strange.....I create the list and read it directly......
java.util.HashMap
java.util.HashMap


then when I really go to the page where I shall read it I get
java.util.ArrayList
java.util.ArrayList
0
MickeysAuthor Commented:
You were right. I acceidental have put some code in that writes to the list again. Thx for the help
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.