函数调用java nullpointerexception [重复]

时间:2021-06-09 17:04:39

This question already has an answer here:

这个问题在这里已有答案:

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

我正在使用Java和eBay SDK(API)迈出第一步。

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

在此代码段中,我尝试从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, ...).

调用似乎有效,我在gcResponse对象中获得了大量结果。下一步是循环返回的类别。类别以数组形式返回。 CategoryType类型的变量ct包含一个类别。调试时,我可以看到正确填充数据的CategoryType对象(Name,Level,...)。

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

ct.leafCategory的值为“false”,表示此类别不是叶类别。

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).

但是当我尝试通过getLeafCategory()函数访问这个字段时,我得到一个Null指针异常(它被APIException-catch块捕获)。

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;'?

这是否意味着,NPE发生在leafCategory() - 函数中?但除了'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;
    }
}

1 个解决方案

#1


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.

如果KMTgcResponse.categories [i] .leafCategory是布尔基元而不是布尔对象和ct.isLeafCategory();返回null(如,API中不存在值),然后从取消装置布尔值到布尔值得到NullPointerException,因为您不能将null分配给基元。

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 

#1


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.

如果KMTgcResponse.categories [i] .leafCategory是布尔基元而不是布尔对象和ct.isLeafCategory();返回null(如,API中不存在值),然后从取消装置布尔值到布尔值得到NullPointerException,因为您不能将null分配给基元。

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