Error Message
java.lang.NoSuchMethodError: org.json.JSONArray.isEmpty()Z
My Scenario
I tried using the isEmpty() method of JSONArrayfrom the org.json JAR.
I'm using IntelliJ as my IDE. I'm not using anything close to reflection in my code. I declare a JSONArray, and after some lines trying to check if it is empty or not, however I get this Error when trying to use isEmpty().
I did work around the issue simply by using jsonArray.length() > 0, but the error message is what fascinates me. I looked into the de-compiled .class file of JSONArray, and the method isEmpty() exists, and has a public access modifier. Futhermore, InteliiJ is suggesting me I can use isEmpty() when typing the code. So what could be causing this error to occur?
Code Example
JSONArray filesJsonArray = new JSONArray();
JSONObject fileObject = new JSONObject();
fileObject.put("fileId",fileId);
fileObject.put("fileName",fileName);
fileObject.put("mimeType",mimeType);
filesJsonArray.put(fileObject);
if (!filesJsonArray.isEmpty()){ //the condition check here throws the error.
}
(Catch blocks were dismissed for brevity)
My Imports
In the class I'm using this code, this is all my imports
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.ByteBuffer;
import java.sql.*;
import java.util.Base64;
org.json.simple.JSONArraythis hasisEmpty()method.org.json.JSONArraythis one does not have the method.isEmpty()method. Maybe you have an issue with IntelliJ, have you tried compiling from the command line? Maybe check File->Project structure to see that IntelliJ uses the right libraryorg.json.JSONArrayorg.json.simple.JSONArray. You will have to handleJSONExceptionwhile putting the objects.