# Off-Topic Discussion > The Lounge > Tech Talk >  >  Java Unchecked Cast Problem

## bobthemonkey

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:



```
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.



```
static ArrayList<Computer> computers = new ArrayList<Computer>();
```


Method to load serialized arraylist from file:



```
	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?

----------


## ninja9578

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.

----------


## bobthemonkey

At the moment, the entire arraylist is being serialized in the saveData method.



```
	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).

----------


## ninja9578

You can use a file iterator.

----------


## bobthemonkey

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.

----------

