22

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);
    }
}
SilverNak
  • 3,283
  • 4
  • 28
  • 44
user3589718
  • 439
  • 2
  • 4
  • 10
  • 3
    That's a warning, not an error. It really doesn't matter. What line is it coming from? – Anubian Noob May 20 '14 at 01:52
  • 2
    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. – Jim Garrison May 20 '14 at 03:25

3 Answers3

63

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.

Sam Hanley
  • 4,707
  • 7
  • 35
  • 63
  • Great answer, I had this issue when using `HashMap`. Is there an official source to explain these two types of operations? – Rafs Mar 01 '23 at 10:14
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
ygngy
  • 3,630
  • 2
  • 18
  • 29
0

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

  classpath 'com.google.gms:google-services:4.2.0'
Adnan Yousaf
  • 317
  • 1
  • 6