"uses unchecked or unsafe operations" [duplicate]
Asked Answered
T

3

23

Why am I getting "uses unchecked or unsafe operations" error everytime i compile? What's wrong with the code? I copied the exact same code from this tutorial http://www.mkyong.com/java/json-simple-example-read-and-write-json/

import java.io.FileWriter;
import java.io.IOException;

import org.json.JSONArray;
import org.json.JSONObject;

public class JsonSimpleExample {
    public static void main(String[] args) {

        JSONObject obj = new JSONObject();
        obj.put("name", "mkyong.com");
        obj.put("age", new Integer(100));

        JSONArray list = new JSONArray();
        list.add("msg 1");
        list.add("msg 2");
        list.add("msg 3");

        obj.put("messages", list);

        try {
            FileWriter file = new FileWriter("c:\\test.json");
            file.write(obj.toJSONString());
            file.flush();
            file.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.print(obj);
    }
}
Tomi answered 20/5, 2014 at 1:47 Comment(2)
That's a warning, not an error. It really doesn't matter. What line is it coming from?Woods
Looks like the org.json.simple library is not generic-aware. The JSONArray class extends an unparameterized ArrayList, which is probably where the warnings are coming from.Barnabas
T
64

The uses unsafe or unchecked operations warning is displayed when you execute code which the Java compiler considers to be lacking in error-checking, or potentially unsafe in some way. However, it's a warning, not an error, and will not stop your code from compiling -- large projects will often churn out warning after warning, and you're free to determine whether they're worth taking action on or not. If you want to dig deeper into what's causing the warning to trigger, you can recompile your .java file with the syntax javac -Xlint:unchecked yourfilename.java, and the compiler will give you more verbose information as to what exactly is causing the error.

In my experience, this warning can often be caused by using something like an ArrayList without specifying the type which it should expect to hold (i.e. using ArrayList a = new ArrayList() rather than ArrayList<String> a = new ArrayList<String>()). The compiler is, in my example case, warning you that your code isn't going to do any checking for you that the values you add to it are any particular type. In a production application, it would likely be good to specify types, but in a test app, you're free to ignore the warnings if you're not concerned about them.

Thermoplastic answered 20/5, 2014 at 2:4 Comment(1)
Great answer, I had this issue when using HashMap. Is there an official source to explain these two types of operations?Solanum
J
1

You get an unchecked cast usually when you cast a generic class, for example:

// Here we have unchecked cast warning
ArrayList<String> arr = (ArrayList<String>) obj;// obj is of type Object

one way to prevent this and make the cast safe is to extend the type which is cast and then use your custom type which extends that like this:

// your class extends generic but is not generic
class MyClass extends ArrayList<String> {  }

//then change your cast like this: 
MyClass arr = (MyClass) obj;//here we have NO warning for unchecked cast
Jonathanjonathon answered 14/4, 2018 at 13:45 Comment(0)
A
0

I fix this problem by updating classpath version in project level gradle files

  classpath 'com.google.gms:google-services:4.2.0'
Alby answered 22/3, 2019 at 5:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.