Search code examples
androidjsonprimitive-types

cannot invoke getjsonobject(int) on the primitive type int


Can someone help me on this...

I'm just trying to make these lines of codes work. At menuitemArray.length().getJSONObject(i) it gives me the error, "cannot invoke getjsonobject(int) on the primitive type int"

Here's the code :

private void parse(TextView txtResult) throws Exception{
    jObject = new JSONObject(xResult);

    JSONArray menuitemArray = jObject.getJSONArray("message");
    String sret = "";
    for (int i = 0; i < menuitemArray.length(); i++){
        sret +=menuitemArray.length().getJSONObject(i).getString("username") + " : ";

        System.out.println(menuitemArray.getJSONObject(i)
            .getString("username"));

        System.out.println(menuitemArray.getJSONObject(i)
            .getString("subject"));

        sret +=menuitemArray.getJSONObject(i)
            .getString("subject") + "\n";
    }
    txtResult.setText(sret);
}

And yeah, I'm kinda new to this. Any help would be greatly appreciated.


Solution

  • Change

     sret +=menuitemArray.length().getJSONObject(i).getString("username") + " : ";
    

    to

     sret +=menuitemArray.getJSONObject(i).getString("username") + " : ";
    

    Actually sret is an String variable. And you're trying to get username from the length of menuitemArray.