Skip to content

Null Pointer Exception in Android SDK when accessing "publish_details" array #30

@philculver

Description

@philculver

Description:

While using the ContentStack Android SDK version 3.10.0, we encountered a null pointer exception when attempting to access the "publish_details" array from a JSON object. This issue occurs when the JSON object does not contain the "publish_details" array or when the array is null. It leads to a warning in the app when trying to call the length() method on a null object.

Steps to reproduce:

  1. Integrate ContentStack Android SDK into an Android project.
  2. Fetch a JSON object that does not contain the "publish_details" array or contains a null value for it.
  3. Attempt to access and iterate through the "publish_details" array using the provided SDK code.

Expected behavior:

The SDK should handle cases where the "publish_details" array is not present or is null without throwing a null pointer exception.

Actual behavior:

A null pointer exception is thrown when trying to call the length() method on a null object, leading to warnings in the app.

Screen Shot 2023-03-16 at 4 30 56 PM

Proposed solution:

Add a null check before iterating through the publishArray. Here's a suggested modification to the SDK code:

if (publishArray != null) {
    for (int i = 0; i < publishArray.length(); i++) {
        JSONObject publishObject = publishArray.optJSONObject(i);
        Iterator<String> iterator = publishObject.keys();
        HashMap<String, Object> hashMap = new HashMap<>();
        while (iterator.hasNext()) {
            String key = iterator.next();
            hashMap.put(key, publishObject.opt(key));
        }
    }
} else {
    Log.e("ContentStack", "publish_details array not found or is null");
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions