Java中的类实例can';不能测试为空
我正在尝试对一个空指针异常进行故障排除,该异常使我无法填充数组适配器。我将其追溯到创建该类实例的那一点,为了尝试进行故障排除,我创建了以下toast:Java中的类实例can';不能测试为空,java,android,nullpointerexception,getter,Java,Android,Nullpointerexception,Getter,我正在尝试对一个空指针异常进行故障排除,该异常使我无法填充数组适配器。我将其追溯到创建该类实例的那一点,为了尝试进行故障排除,我创建了以下toast: private void showSplits() { // populate the split line Split s = new Split(); s = mTransaction.getSplit(); int duration = Toast.LENGTH_SHORT; if (s != n
private void showSplits() {
// populate the split line
Split s = new Split();
s = mTransaction.getSplit();
int duration = Toast.LENGTH_SHORT;
if (s != null) {
Toast toast = Toast.makeText(getActivity(), s.getCategory(), duration);
Toast toast2 = Toast.makeText(getActivity(), s.getDescription(), duration);
Toast toast3 = Toast.makeText(getActivity(), s.getAmount(), duration);
toast.show();
toast2.show();
toast3.show();
} else {
Toast toast4 = Toast.makeText(getActivity(), "S was null.", duration);
toast4.show();
}
。。。。
}
运行时,该方法的结果是整齐地打印出拆分的每个字段的内容,然后整齐地打印出“S为null”。我已经验证了我只调用了一次该方法,所以我看不出S如何既为null又不为null
这是课程的细节
public class Split implements Serializable {
private static final long serialVersionUID = 1L;
private static final String JSON_CATEGORY = "category";
private static final String JSON_AMOUNT = "amount";
private static final String JSON_DESCRIPTION = "description";
private UUID mId;
private String mCategory;
private String mAmount;
private String mDescription;
public Split(String category, String amount, String description) {
mCategory = category;
mAmount = amount;
mDescription = description;
}
public Split() {
mId = UUID.randomUUID();
mCategory = "none";
}
public Split(JSONObject json) throws JSONException {
if (json.has(JSON_CATEGORY)) {
mCategory = json.getString(JSON_CATEGORY);
}
if (json.has(JSON_AMOUNT)) {
mAmount = json.getString(JSON_AMOUNT);
}
if (json.has(JSON_DESCRIPTION)) {
mDescription = json.getString(JSON_DESCRIPTION);
}
}
public JSONObject toJSON() throws JSONException {
JSONObject json = new JSONObject();
// if (mCategory != null) {
json.put(JSON_CATEGORY, mCategory);
// }
// if (mAmount != null) {
json.put(JSON_AMOUNT, mAmount);
// }
// if (mDescription != null) {
json.put(JSON_DESCRIPTION, mDescription);
// }
return json;
}
// Factory method to convert an array of JSON objects into a list of objects
// User.fromJson(jsonArray);
public static ArrayList<Split> fromJson(JSONArray jsonObjects) {
ArrayList<Split> splits = new ArrayList<Split>();
for (int i = 0; i < jsonObjects.length(); i++) {
try {
splits.add(new Split(jsonObjects.getJSONObject(i)));
} catch (JSONException e) {
e.printStackTrace();
}
}
return splits;
}
public String getCategory() {
return mCategory;
}
public void setCategory(String category) {
mCategory = category;
}
public String getAmount() {
return mAmount;
}
public void setAmount(String amount) {
mAmount = amount;
}
public String getDescription() {
return mDescription;
}
public void setDescription(String description) {
mDescription = description;
}
公共类拆分实现可序列化{
私有静态最终长serialVersionUID=1L;
私有静态最终字符串JSON_CATEGORY=“CATEGORY”;
私有静态最终字符串JSON_AMOUNT=“AMOUNT”;
私有静态最终字符串JSON_DESCRIPTION=“DESCRIPTION”;
私有UUID-mId;
私有字符串mCategory;
私有字符串mAmount;
私有字符串描述;
公共拆分(字符串类别、字符串金额、字符串描述){
mCategory=类别;
mAmount=金额;
mddescription=描述;
}
公开拆分(){
mId=UUID.randomuid();
mCategory=“无”;
}
公共拆分(JSONObject json)抛出JSONException{
if(json.has(json_类别)){
mCategory=json.getString(json_类别);
}
if(json.has(json_金额)){
mAmount=json.getString(json\U金额);
}
if(json.has(json_说明)){
mddescription=json.getString(json_描述);
}
}
公共JSONObject toJSON()抛出JSONException{
JSONObject json=新的JSONObject();
//if(mCategory!=null){
put(json_类别,McCategory);
// }
//if(mAmount!=null){
put(json_金额,mAmount);
// }
//如果(MDDescription!=null){
json.put(json_描述,mDescription);
// }
返回json;
}
//方法将JSON对象数组转换为对象列表
//fromJson(jsonArray);
来自JSON的公共静态ArrayList(JSONArray JSONObject){
ArrayList splits=新的ArrayList();
for(int i=0;i
}
据我所知,showSplits在我正在处理的片段中只调用过一次 在第一行中,您将s变量引用到一个新的分割对象,在第二行中,您将s的引用更改为mTransaction.getSplit()。如果对象是通过mTransaction返回的。getSplit()为null,则s将引用null。表示s的值将为空
Split s = new Split();
s = mTransaction.getSplit();
您的mTransaction.getSplit()可能返回空值,尝试调试它是
s
是“薛定谔猫”的缩写吗?首先,您为s
指定了一个变量(这是一个可怕的变量名称,但这不是代码复查,所以我离题了),然后在下一行中更改s
的内容。其次,mTransaction
在哪里定义?它可能是空的吗?我看不出你的代码有什么问题,所以看起来showspits
实际上被调用了不止一次。你是如何证实它不是?也许在showspits
的开头添加另一个吐司来验证…嘿,s
是一个比toast2
,toast3
等更糟糕的名字,如果你已经知道你有toast
@ajb@DavidWallace对不起,我想这有点模糊。。。我指的是他关于s
是否可以同时为空和不为空的问题。这不是对变量名的注释。这并不能回答问题。如果getSplit()
返回null,那么为什么它会显示toast
,toast2
,toast3
?您是否也可以显示片段的代码,这可能有助于解决问题。我确实对分割对象的许多赋值排序错误。幸运的是,这里没有量子难题。