Search code examples
javanullpointerexceptionebay-api

java nullpointerexception on function call


I`m doing my first steps on using Java and the eBay SDK (API).

In this code snippet I try to get a category list from eBay.

The call seems to work and I get a large result in the gcResponse object. Next step is to loop through the returned categories. The categories are returned in an array. The variable ct of type CategoryType holds one category. When debugging I can see the CategoryType object populated with data correctly(Name, Level, ...).

The ct.leafCategory has a value of 'false' indicating that this category isn't a leaf category.

But when I try to acces this field via the getLeafCategory() function I get a Null pointer exception (it is caught by the APIException-catch block).

Now I wonder how I can access this field correctly (all I want get back would be 'false'). I cannot access the field directly, because of course it seems to be private.

Does that mean, that the NPE happens inside the leafCategory()-function? But what could the function do except 'return leafCategory;'?

Thank's a lot for giving me a hint!

    ApiContext apiContext = getContext(env, siteID);

    GetCategoriesCall call = new GetCategoriesCall(apiContext);
    GetCategoriesResponseType gcResponse = null;

    call.setParentCategories(new String[] {"3187"});

    call.setCategorySiteID(getSiteCode(siteID));

    call.setDetailLevel(new DetailLevelCodeType[] {DetailLevelCodeType.RETURN_ALL});

    try {

        call.getCategories();
        gcResponse = call.getResponse();

        CategoryArrayType arr = gcResponse.getCategoryArray();

        CategoryType ct = new CategoryType();

        KMTgcResponse.categories = new KMTCategory[arr.getCategoryLength()];

        for (int i=0; i<arr.getCategoryLength(); i++){
            ct = arr.getCategory(i);
            KMTgcResponse.categories[i] = new KMTCategory();
            KMTgcResponse.categories[i].ID = ct.getCategoryID();

            KMTgcResponse.categories[i].leafCategory = ct.isLeafCategory();   // NullPointerException here !!!
            KMTgcResponse.categories[i].level = ct.getCategoryLevel();
            KMTgcResponse.categories[i].name = ct.getCategoryName();
            KMTgcResponse.categories[i].parentID = ct.getCategoryParentID();
        }

        response.getCategoriesResponse = KMTgcResponse;
        response.rc = 1;

    } catch (ApiException e) {
        e.printStackTrace();
        response.err_msg = Common.toString(e);
        response.rc = -1;
} catch (Exception e) {
        response.err_msg = Common.toString(e);
        response.rc = -1;
    }
}

Solution

  • If KMTgcResponse.categories[i].leafCategory is boolean primitive instead of Boolean object and ct.isLeafCategory(); returns null (as in, value does not exist from API), then you get NullPointerException from unboxing the Boolean to boolean, as you cannot assign null to a primitive.

    ref: http://developer.ebay.com/devzone/javasdk-jaxb/docs/libref/com/ebay/soap/eBLBaseComponents/CategoryType.html#isLeafCategory()

    In any case,

    That loop over the array looks odd. You practically could do this (assuming those types match)

    KMTgcResponse.categories[i] = arr.getCategory(i);
    

    Or, since you're just referring to the same array positions

    KMTgcResponse.categories =  arr;
    

    At the very least, this is the preferred way to write it

    ct = arr.getCategory(i);
    KMTCategory kmtcat = new KMTCategory();
    kmtcat.ID = ct.getCategoryID();
    kmtcat.leafCategory = null == ct.isLeafCategory() || ct.isLeafCategory(); // temporary error fix 
    // other values 
    KMTgcResponse.categories[i] = kmtcat;  // set the array