JSONObject NoSuchMethodException- Init

1.3k Views Asked by At

I'm using reflection to write a class that will create objects (whose type I am passing as parameter Base) from the elements of JSONArray jArr, and add them to ArrayList objectList. Everything is going fine until I get to this step:

for(int i = 0; i < jArr.length(); i++) {
        objectList.add(Base.getConstructor(new Class[]{JSONObject.class}).newInstance(jArr.getJSONObject(i)));
    }

At runtime I'm getting the following error:

java.lang.NoSuchMethodException: <init> [class org.json.JSONObject]

From what I've pulled together from Google, it appears that this error occurs when you try to call a constructor with incorrect parameters, but I'm not sure why that would apply here.

Edit: Here is the full error, as requested:

06-15 18:35:55.245 1919-1919/com.example.ben.phptest W/System.err﹕ java.lang.NoSuchMethodException: [class org.json.JSONObject]
06-15 18:35:55.245 1919-1919/com.example.ben.phptest W/System.err﹕ at java.lang.Class.getConstructorOrMethod(Class.java:472)
06-15 18:35:55.255 1919-1919/com.example.ben.phptest W/System.err﹕ at java.lang.Class.getConstructor(Class.java:446)
06-15 18:35:55.255 1919-1919/com.example.ben.phptest W/System.err﹕ at com.example.ben.phptest.PHPList.(PHPList.java:58)
06-15 18:35:55.255 1919-1919/com.example.ben.phptest W/System.err﹕ at com.example.ben.phptest.MainActivity.onCreate(MainActivity.java:35)
06-15 18:35:55.255 1919-1919/com.example.ben.phptest W/System.err﹕ at android.app.Activity.performCreate(Activity.java:5231)
06-15 18:35:55.255 1919-1919/com.example.ben.phptest W/System.err﹕ at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-15 18:35:55.255 1919-1919/com.example.ben.phptest W/System.err﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-15 18:35:55.255 1919-1919/com.example.ben.phptest W/System.err﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-15 18:35:55.255 1919-1919/com.example.ben.phptest W/System.err﹕ at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-15 18:35:55.255 1919-1919/com.example.ben.phptest W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-15 18:35:55.265 1919-1919/com.example.ben.phptest W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
06-15 18:35:55.265 1919-1919/com.example.ben.phptest W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
06-15 18:35:55.265 1919-1919/com.example.ben.phptest W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5017)
06-15 18:35:55.265 1919-1919/com.example.ben.phptest W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
06-15 18:35:55.265 1919-1919/com.example.ben.phptest W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
06-15 18:35:55.265 1919-1919/com.example.ben.phptest W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-15 18:35:55.265 1919-1919/com.example.ben.phptest W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-15 18:35:55.265 1919-1919/com.example.ben.phptest W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)

(Line 58 is the heart of the loop above)

As a side note, the class I am passing in most definitely accepts a JSONObject as its one and only constructor parameter:

Person(JSONObject obj) throws JSONException
2

There are 2 best solutions below

0
On

If I understand you correctly, you have a list of objects that is being added to and your doing so by reflexively calling a constructor on a JSONObject in jArr. The NoSuchMethodException is a run time exception that occurs when you attempt to call a method at run time that either does not exist, or for which you have the wrong method signature to. My best guess would be that the constructor you are attempting to call does not take a parameter of type JSONObject. The newInstance method should take the same parameter list as the constructor.

1
On

I think you've answered your own question. There is no JSONObject constructor that takes only a JSONObject. The error is saying exactly that.

Why not change your code to:

for (int i = 0; i < jArr.length(); i++) {
        objectList.add(jArr.getJSONObject(i));
}