Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoSuchMethodError using JSONArray in org.json

Tags:

java

json

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;
like image 328
Daniel B. Avatar asked Jan 29 '19 10:01

Daniel B.


1 Answers

If you are using spring boot, then you can try this:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.skyscreamer</groupId>
                <artifactId>jsonassert</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

It worked for me..

like image 170
curious Avatar answered Sep 22 '22 13:09

curious