java 函数调用出现空指针异常
java nullpointerexception on function call
我正在尝试使用 Java 和 eBay SDK (API)。
在此代码片段中,我尝试从 eBay 获取类别列表。
调用似乎有效,我在 gcResponse 对象中得到了很大的结果。
下一步是遍历返回的类别。
类别在数组中返回。
CategoryType 类型的变量 ct 包含一个类别。
调试时,我可以看到 CategoryType 对象正确填充了数据(名称、级别、...)。
ct.leafCategory 的值为 'false',表明该类别不是叶类别。
但是当我尝试通过 getLeafCategory() 函数访问该字段时,我得到一个空指针异常(它被 APIException-catch 块捕获)。
现在我想知道如何才能正确访问该字段(我想要返回的只是 'false')。我无法直接访问该字段,因为它似乎是私有的。
这是否意味着 NPE 发生在 leafCategory() 函数内部?但是这个函数除了 'return leafCategory;' 还能做什么呢?
非常感谢您给我提示!
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;
}
}
如果 KMTgcResponse.categories[i].leafCategory
是 boolean
原始类型而不是 Boolean
对象并且 ct.isLeafCategory();
returns null(例如,值不存在于 API),那么你会从 Boolean
拆箱到 boolean
得到 NullPointerException,因为你不能将 null
分配给原语。
无论如何,
数组上的循环看起来很奇怪。你几乎可以做到这一点(假设这些类型匹配)
KMTgcResponse.categories[i] = arr.getCategory(i);
或者,因为您只是指的是相同的数组位置
KMTgcResponse.categories = arr;
至少,这是首选的写法
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
我正在尝试使用 Java 和 eBay SDK (API)。
在此代码片段中,我尝试从 eBay 获取类别列表。
调用似乎有效,我在 gcResponse 对象中得到了很大的结果。 下一步是遍历返回的类别。 类别在数组中返回。 CategoryType 类型的变量 ct 包含一个类别。 调试时,我可以看到 CategoryType 对象正确填充了数据(名称、级别、...)。
ct.leafCategory 的值为 'false',表明该类别不是叶类别。
但是当我尝试通过 getLeafCategory() 函数访问该字段时,我得到一个空指针异常(它被 APIException-catch 块捕获)。
现在我想知道如何才能正确访问该字段(我想要返回的只是 'false')。我无法直接访问该字段,因为它似乎是私有的。
这是否意味着 NPE 发生在 leafCategory() 函数内部?但是这个函数除了 'return leafCategory;' 还能做什么呢?
非常感谢您给我提示!
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;
}
}
如果 KMTgcResponse.categories[i].leafCategory
是 boolean
原始类型而不是 Boolean
对象并且 ct.isLeafCategory();
returns null(例如,值不存在于 API),那么你会从 Boolean
拆箱到 boolean
得到 NullPointerException,因为你不能将 null
分配给原语。
无论如何,
数组上的循环看起来很奇怪。你几乎可以做到这一点(假设这些类型匹配)
KMTgcResponse.categories[i] = arr.getCategory(i);
或者,因为您只是指的是相同的数组位置
KMTgcResponse.categories = arr;
至少,这是首选的写法
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