• Lucid Dreaming - Dream Views




    Results 1 to 5 of 5
    1. #1
      Member
      Join Date
      Feb 2008
      Gender
      Location
      Leabrook, Adelaide, South Australia
      Posts
      88
      Likes
      1

      Java Unchecked Cast Problem

      I'm working on a part of my Programming in Java 1 group project to save and load and ArrayList to/from a file but I'm getting an unchecked cast warning at the line:
      Code:
      computers = (ArrayList<Computer>) ois.readObject();
      I've done a bit of research and found that I could just suppress the warning but that there should be a more elegant way to do it. I can't find it, however.

      Relevant code:

      Create the arraylist.
      Code:
      static ArrayList<Computer> computers = new ArrayList<Computer>();
      Method to load serialized arraylist from file:
      Code:
      	public static void loadData(){ 
      		//loads data from computers.dat to ArrayList computers
      		try {
      			//create file input stream
      			FileInputStream fis = new FileInputStream("computers.dat"); 
      			//create object input stream
      			ObjectInputStream ois = new ObjectInputStream(fis); 
      			//copy data from file to object
      			computers = (ArrayList<Computer>) ois.readObject();
      		} catch (IOException e) {
      			e.printStackTrace();
      		} catch (ClassNotFoundException e) {
      			e.printStackTrace();
      		}
      
      	}
      I can't see what I've done wrong. I've specified that the arraylist is only holding objects of the Computer class which is all I need to do according to what I've read.

      Ideas?

    2. #2
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      How did you save the serialized data? Did you save it as a serialized arraylist of computers, or just as a bunch of computers?

      Your problem is that the java VM doesn't know what's in the arraylist, only that it's an arraylist of something. The <ObjectType> is only for checking at compile time. Doing a cast between one type and another is dangerous and java doesn't want you to do that. I'm not sure if this is the more elegant way that you are talking about, but I would fetch each object individually and push them into the arraylist one at a time.

    3. #3
      Member
      Join Date
      Feb 2008
      Gender
      Location
      Leabrook, Adelaide, South Australia
      Posts
      88
      Likes
      1
      At the moment, the entire arraylist is being serialized in the saveData method.
      Code:
      	public static void saveData(){ 
      		try{
      			//create a file output stream
      			FileOutputStream fos = new FileOutputStream("computers.dat"); 
      			//create an object output stream
      			ObjectOutputStream oos = new ObjectOutputStream(fos); 
      			//write the object data to the file
      			oos.writeObject(computers); 
      			//if the object output stream isn't empty, flush and close it
      			if (oos != null) { 
      				oos.flush();
      				oos.close();
      			}
      		}catch (FileNotFoundException ex) {
      			ex.printStackTrace();
      		} catch (IOException ex) {
      			ex.printStackTrace();
      
      		}
      	}
      As far as I know, the cast isn't actually changing anything, it's just there to let the compiler know that the read object will always be of the type ArrayList<Computer> right? Does this mean that as long as the computer.dat file is ok, the program will be fine and I can safely suppress the warning?

      What would be the advantage of doing every object individually?
      Also I don't know how to append/read back multiple objects to/from one file like this and using multiple files would be too messy (the arraylist will hold upwards of fifty objects).

    4. #4
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      You can use a file iterator.

    5. #5
      Member
      Join Date
      Feb 2008
      Gender
      Location
      Leabrook, Adelaide, South Australia
      Posts
      88
      Likes
      1
      That's looking a bit complex for what we're doing. I'm only in first year software engineering.

      I think I'll just suppress the warning and not worry too much about it.

      Thanks for your input.

    Bookmarks

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •