Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8049681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:51:06+00:00 2026-06-05T06:51:06+00:00

Im trying to do the following: UserFunctions uf = new UserFunctions(); JSONArray json =

  • 0

Im trying to do the following:

UserFunctions uf = new UserFunctions();
    JSONArray json = uf.getAllFreebies();
    System.out.println(json + "blah1"); //Here im able to retrieve the whole JSONArray.

try{    
System.out.println("1");
    for (int i = 1; i <json.length(); i++) {
        System.out.println("2");
            jo = json.optJSONObject(i); //Im getting null value here
            System.out.println(jo + "blah2"); //Here im getting null

…}

Im printing out my JSONArray using following lines.

JSONArray json = uf.getAllFreebies();
System.out.println(json + "blah1"); 

Now on the following lines when i tried to retrieve the JSONObject from the JSONArray and tried to print it out,it prints out “null” instead of the JSONObject at the particular index.

jo = json.optJSONObject(i);
System.out.println(jo + "blah2"); 

Can anyone pls tell me what am i doing wrong?I mean how can i get a null for JSONOBject when my JSONArray is not null?

Thank You.

Following is my JSONArray logcat:

05-31 21:02:57.156: I/System.out(318): [["viking","Art","Potrait","Potrait","Good","im giving out potrait 7325697","176 Fiat Ave","Iselin","New Jersey","USA","08830","2012-05-27"],["n00b","Books","Novels","Novels","Good","Im giving out novels 7325697","b9 maa krupa","petlad","Gujarat","India","388450","2012-05-27"],["n00b","Computers","laptop","laptop giveaway","Good","Im giving out laptop if you are interested than pls call on 7325697","B9 Ma  Krupa","Petlad","Gujarat","India","388450","2012-05-27"],["mista","Cameras & Photos","Camera","Camera GiveAway","Very good","im giving out camera .its kodak .pls email me on mista@gmail.com","Mista Lee elm street","seoul","Korea","South Korea","ha8 9sd","2012-05-27"],["panda","Gaming Consoles","XBOX","XBOX 360","Very Good","Im giving out xbox 360.if you are interested please email me on panda@gmail.com","435 Carmen Rd,","Buffalo","New York","USA","14226","2012-05-27"],["viking","Cameras & Photos","Camera","Kodak Camera","Good","Kodak Camera giveaway.Pls call on 732397","","Iselin","New Jersey","USA","08830","2012-05-28"],["viking","Books","Novels","Novel GA","Good","Novel give away.call on 7325697.","","Iselin","New Jersey","USA","08830","2012-05-28"],["viking","Automotive","Car","Car GiveAway","Good","Im giving out car.if you are interested pls call 7323697.","176 Fiat Ave","Iselin","New Jersey","USA","08830","2012-05-29"],["viking","Collectibles","Medallions","Medallion GA","Very Good","Im giving out medallion.if inetrested pls call 732697","176 Fiat Ave,","Iselin","New Jersey","USA","08830","2012-05-29"],["viking","Clothing & Accessories","cloths","cloths giveaway","Good","im giving out cloths if you are interested pls call on 735697","176 Fiat Ave,","Iselin","New jersey","USA","08830","2012-05-29"],["viking","Books","Novel","Novel GA","Good","pls call 735697","435 carmen rd","buffalo","ny","usa","14226","2012-05-29"],["viking","Books","TextBook","CHemistry 101","GOod","pls call 735697","176 fiat ave","iselin","new jersey","usa","08830","2012-05-29"],["mista","Books","Notebook","Notbook","Good","im giving out notebbok if you are interested pls call 48374288423","elm street","seaoul","na","South Korea","jfjafjk","2012-05-29"]]blah1

logcat output when trying to print out JSONOBject at index i

05-31 21:02:57.156: I/System.out(318): nullblah2
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T06:51:08+00:00Added an answer on June 5, 2026 at 6:51 am

    The JSON [[...], [...], [...]] does not contain JSON-objects, but it does have some JSON-arrays.

    Thus optJSONObject finds a JSON-array (where it expected a JSON-object) and returns null because it is of the incorrect type. (opt is short for optional.)

    Use optJSONArray (note Array and not Object). Alternatively, use getJSONArray, which will throw a JSONException on failure.

    Happy coding.


    Note that JSON keeps a strict distinction between Objects, Arrays, and the various Values. The concept of an Array being a special (sub)kind of Object (in the JSON sense) exists in JavaScript but does not necessarily extend to other JSON implementations. For instance, the JSONArray class has no relationship with the JSONObject class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying following code String s1 = ß.cfg; System.out.println (s.toUpperCase()); output I am
I was trying following example: class BaseClass { public void methodA(Class<?> cl) { System.out.println(Base.methodA());
I'm trying the following: CREATE TABLE Table1 ( RecordNo autonumber, --error here! PersonId varchar(50),
I'm trying the following: $(#button_feedback).click(function () { $.ajax({ type: POST, contentType: application/json; charset=utf-8, url:
I am trying the following: public partial class PopUps : System.Web.UI.Page { public string
I am trying the following: boost::shared_ptr< tcp::socket > socket(new tcp::socket( *io_service)); boost::bind(&function, *socket); //
I am trying following stored proc to insert attendance of whole class(multiple student) at
Trying the following code doesn't seem to work out for me quite as planned:
I have been trying following: <beans xmlns=http://www.springframework.org/schema/beans xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:context=http://www.springframework.org/schema/context xmlns:jee=http://www.springframework.org/schema/jee xmlns:mvc=http://www.springframework.org/schema/mvc xmlns:p=http://www.springframework.org/schema/p xmlns:util=http://www.springframework.org/schema/util ................
I am trying following, 1. Get response using Ajax from the users selection which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.