-1

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;
    }
}
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
  • Show the stacktrace, please – OneCricketeer Nov 20 '16 at 14:26
  • 3
    *I get a Null pointer exception (it is caught by the APIException-catch block*: no, that is impossible. `catch (ApiException e)`will not catch NullPointerException. Post the complete stack trace of the exception you're asking about, always. – JB Nizet Nov 20 '16 at 14:27
  • What is CategoryType? – brummfondel Nov 20 '16 at 14:27
  • I guess, there is suppressed exception ! for more info http://stackoverflow.com/questions/7849416/what-is-a-suppressed-exception To solve your problem you've to use the try with resources . – bilelovitch Nov 20 '16 at 14:31
  • It appears that your API is either not populating every attribute of CategoryType or is not populating it with the expected attribute type. I would print the entire CategoryType instance at the top of the loop. – MikeJRamsey56 Nov 20 '16 at 14:35
  • Correct, the exception is caught by another catch-block (I edited the code) and the exception is like this:java.lang.NullPointerException at at.km_tools.KMTEbay.EbaySrv.getCategories(EbaySrv.java:121) at at.km_tools.KMTEbay.KMTEbay.main(KMTEbay.java:132) – Martin Kaltenböck Nov 20 '16 at 14:39

1 Answers1

1

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 
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • YES, that was the problem. I overlooked that getLeafCategory() Returns Boolean instead of boolean. Now can you tell a bloody beginner how to convert the Boolean null-value to a primitive boolean false value? – Martin Kaltenböck Nov 20 '16 at 14:47
  • For the most part, just update `KMTCategory` corresponding values – OneCricketeer Nov 20 '16 at 14:49