Search code examples
javacompiler-warningsobjectinputstream

Unchecked or unsafe operations at ObjectInputStream


I'm getting a "Unchecked or unsafe operations" error at this method:

public static MyClass initApp(){
        MyClass obj = new MyClass(); 
        try{
            ObjectInputStream oin = 
                new ObjectInputStream(new FileInputStream("file.dat"));

            obj.setSomeList ( (Map<String,MyOtherClass>) oin.readObject()); //error line

        }catch(IOException e)
            { System.out.println(e.getMessage()); }
         catch(ClassNotFoundException e)
            { System.out.println(e.getMessage()); }

        return obj; 
    }

Isn't Map the most abstract as possible in this case? How can I solve this?

Method setSomeList (NOTE! It is placed in another class! If that helps):

public void setSomeList(Map<String,MyOtherClass> l){
        for(Map.Entry<String,MyOtherClass> entrada : l.entrySet())
            someList.put(entrada.getKey(),entrada.getValue().clone());
    }

Solution

  • It is only a warning, you could simply ignore it as it doesn't prevent your application to work unless it is not the right type, if you want to remove it simply add @SuppressWarnings("unchecked") at the method definition level, as below:

    @SuppressWarnings("unchecked")
    public static MyClass initApp(){