函数调用上的java nullpointerexception
我正在做使用Java和eBay SDK(API)的第一步 在这个代码片段中,我尝试从eBay获取一个类别列表 调用似乎有效,我在gcResponse对象中得到了一个很大的结果。 下一步是循环返回的类别。 类别以数组的形式返回。 CategoryType类型的变量ct包含一个类别。 调试时,我可以看到CategoryType对象正确地填充了数据(名称、级别等) ct.leaftCategory的值为“false”,表示该类别不是叶类别 但是当我试图通过getLeafCategory()函数访问此字段时,我得到一个空指针异常(它被APIException catch块捕获) 现在我想知道如何才能正确访问此字段(我只想返回“false”)。我不能直接访问该字段,因为它看起来当然是私有的 这是否意味着NPE发生在leafCategory()函数中?但是,除了“return-leaftcategory;”,函数还能做什么呢 谢谢你给我一个提示函数调用上的java nullpointerexception,java,nullpointerexception,ebay-api,Java,Nullpointerexception,Ebay Api,我正在做使用Java和eBay SDK(API)的第一步 在这个代码片段中,我尝试从eBay获取一个类别列表 调用似乎有效,我在gcResponse对象中得到了一个很大的结果。 下一步是循环返回的类别。 类别以数组的形式返回。 CategoryType类型的变量ct包含一个类别。 调试时,我可以看到CategoryType对象正确地填充了数据(名称、级别等) ct.leaftCategory的值为“false”,表示该类别不是叶类别 但是当我试图通过getLeafCategory()函数访问此字
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;
}
}
ApiContext ApiContext=getContext(env,siteID);
GetCategoriesCall=新建GetCategoriesCall(apiContext);
GetCategoriesResponseType gcResponse=null;
call.setParentCategories(新字符串[]{“3187”});
setCategorySteId(getSiteCode(siteID));
call.setDetailLevel(新的DetailLevelCodeType[]{DetailLevelCodeType.RETURN_ALL});
试一试{
call.getCategories();
gcResponse=call.getResponse();
CategoryArray类型arr=gcResponse.getCategoryArray();
CategoryType ct=新的CategoryType();
kmtgresponse.categories=新的kmt类别[arr.getCategoryLength()];
对于(int i=0;iIf
kmtgresponse.categories[i]。leafCategory
是boolean
原语,而不是boolean
对象和ct.isLeafCategory();
返回null(如中所示,API中不存在值),然后将布尔值
解装箱到布尔值
,得到NullPointerException,因为您不能将null
赋值给原语
参考:
无论如何
数组上的循环看起来很奇怪。您实际上可以这样做(假设这些类型匹配)
或者,因为您只是指相同的数组位置
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
显示堆栈跟踪,请获取空指针异常(它被APIException catch块捕获:否,这是不可能的。
catch(APIException e)
将不会捕获NullPointerException。请始终发布您所询问的异常的完整堆栈跟踪。什么是CategoryType?我猜,存在被抑制的异常!有关解决问题的详细信息,您必须使用“资源尝试”。您的API可能没有填充CategoryType的每个属性,或者没有填充用期望的属性类型填充它。我会在循环的顶部打印整个CategoryType实例。是的,这就是问题所在。我忽略了getLeafCategory()返回布尔值而不是布尔值。现在,你能告诉一个初学者如何将布尔空值转换为原始布尔假值吗?大多数情况下,只需更新KMTCategory
相应的值即可
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