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 9244457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:02:14+00:00 2026-06-18T09:02:14+00:00

I am trying to parse a json using JsonReader. While parsing I came accross

  • 0

I am trying to parse a json using JsonReader. While parsing I came accross 2 scenarios where my app crashes.
1). When JsonArray contains sets of another JsonArrays and and out of which one of it is null. I am giving a scenarios where result is a array and it has 21 more arrays in it and the last array is null.

"results": [(21)
[...(5)],-
[...(5)],-
[...(5)],-
[...(5)],-
[...(5)],-
[...(5)],-
[...(5)],-
[...(5)],-
[...(5)],-
[...(5)],-
[...(5)],-
[...(5)],-
[...(5)],-
[...(5)],-
[...(5)],-
[...(5)],-
[...(5)],-
[...(5)],-
[...(5)],-
[...(5)],-
[(0)]
],

2). When JsonArray has sets of objects in it and one of the object is null. I have attached the example for the same. Here I have source as my JsonArray which contains 21 objects and out of which 1 is null.

"source": [(21)
{...},-
{...},-
{...},-
{...},-
{...},-
{...},-
{...},-
{...},-
{...},-
{...},-
{...},-
{...},-
{...},-
{...},-
{...},-
{...},-
{...},-
{...},-
{...},-
{...},-
null
]

I am able to check when some individual element within a is null by

reader.peek()== JsonToken.NULL

Error Output in Logcat:

02-04 16:10:02.425: E/AndroidRuntime(1203): FATAL EXCEPTION: pool-1-thread-2
02-04 16:10:02.425: E/AndroidRuntime(1203): java.lang.IllegalStateException: Expected BEGIN_OBJECT but was NULL at line 1 column 327992
02-04 16:10:02.425: E/AndroidRuntime(1203):     at com.google.gson.stream.JsonReader.expect(JsonReader.java:339)
02-04 16:10:02.425: E/AndroidRuntime(1203):     at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:322)

I cant figure out how can I check whether Array or Object which I want to parse which has no name is Null or not.

Edit:

Code:

public ResultObject parse(){
    try{
        if(reader != null ){
            resultObject    =    new ResultObject();
            reader.beginObject();
            Log.e("123", "inside try ");
            while (reader.hasNext()) {
                String name = reader.nextName();
                if (name.equalsIgnoreCase("id")) {
                    if(reader.peek()== JsonToken.NULL){
                        reader.skipValue();
                        id=    0;
                        Log.e("123", "parse result --------id----------" + id);
                        resultObject.setId(id);
                    }else{
                        id= reader.nextDouble();
                        Log.e("123", "parse result -------id----------" + id);
                        resultObject.setId(id);
                    }
                } else {
                    Log.e("123", "skip value");
                    reader.skipValue();
                }
            }

            reader.endObject();
        }
    }catch(IOException e){ 
        e.printStackTrace();
    }

    return resultObject;
}

Calling function:

if (name.equalsIgnoreCase(SOURCE)) {
    if(reader.peek()== JsonToken.NULL){
        reader.skipValue();
    } else {
        resultObjectList = new ArrayList();
        resultObject    =    new ResultObject();
        reader.beginArray();
        while(reader.hasNext()){
            resultObject    =new ResultJsonUtil(reader).parse();
            if(resultObject!=null){
                resultObjectList.add(resultObject);
            }
        }

        reader.endArray();
    }
}
  • 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-18T09:02:16+00:00Added an answer on June 18, 2026 at 9:02 am

    Your code crashes at reader.beginObject();, it seems. Since there is not an object, Gson throws an exception.

    If you’re not sure, what comes next, you can test this with peek

    Returns the type of the next token without consuming it.

    You can then call the proper reader method according to the retrieved JsonToken

    if (reader.peek() != JsonToken.BEGIN_OBJECT) {
        // do something
    } else {
        // parse object
        resultObject = new ResultObject();
        reader.beginObject();
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to parse JSON in an Adobe Flex app, using http://www.mikechambers.com/blog/2006/03/28/tutorial-using-json-with-flex-2-and-actionscript-3/'>This Tutorial
Trying to parse the following json using gson in my android app but givien
I am trying to parse a JSON script from my server which contains a
I am trying to parse a json object using gson in Android application ,the
I'm using json-framework to parse some simple json in my iphone app, and it
I'm trying to parse some JSON coming from an AJAX request using jQuery. Basically,
First: this is my first time using GSON. Trying to parse some JSON, I
I'm trying to parse some json using Json.NET. The json either returns an array
I am trying to parse json using json_decode in php. It is failing for
I'm trying to parse JSON Using ASIHttpRequset I wrote this code -(void) tryASIHttpRequest{ NSString

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.