gpt4 book ai didi

doinbackground 中的 java.lang.NullPointerException

转载 作者:行者123 更新时间:2023-12-01 11:49:12 24 4
gpt4 key购买 nike

enter image description here这是java中的代码,我进行了调试,它停止在apPhoto[i].id = Integer.parseInt(us.getPropertyAsString(0));处,getproperty返回null:

@Override
protected ApartmentPhoto[] doInBackground(Void... arg0) {

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);
PropertyInfo pi = new PropertyInfo();
pi.setNamespace(NAMESPACE);
pi.setName("email");
pi.setValue(Global.Email);
pi.setType(String.class);
request.addProperty(pi);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
try {
HttpTransportSE androidHttpTransport = new HttpTransportSE(
Global.URL);
androidHttpTransport.call(SOAP_ACTION1, envelope);

SoapObject result11 = (SoapObject) envelope.getResponse();
if (result11 == null)
return null;
else {
ApartmentPhoto[] apPhoto = new ApartmentPhoto[result11.getPropertyCount()];
for (int i = 0; i < apPhoto.length; i++) {
SoapObject us = (SoapObject) result11.getProperty(i);

apPhoto[i] = new ApartmentPhoto();
apPhoto[i].id = Integer.parseInt(us.getPropertyAsString(0));
apPhoto[i].image = us.getPropertyAsString(3);
}
return apPhoto;
}
} catch (Exception e) {

e.printStackTrace();

return null;

}
}
}


private class ApartmentPhoto {
public String email1;
public int id;
public String image;
}

最佳答案

您的 SoapObject 可能不为 null,但您在 us.getPropertyAsString(0) 处指向的索引可能指向 null 值。请确保您的对象包含您认为它包含的属性。

您已经拥有以下代码:

for (int i = 0; i < apPhoto.length; i++) {
SoapObject us = (SoapObject) result11.getProperty(i);

apPhoto[i] = new ApartmentPhoto();
apPhoto[i].id = Integer.parseInt(us.getPropertyAsString(0));
apPhoto[i].image = us.getPropertyAsString(3);
}

只需在声明 us 后添加以下循环作为快速测试:

for(int j = 0; j < whateverYourCeilingIs; j++) {
System.out.println(us.getPropertyAsString(j));
}

我想你会发现索引 0 为空。

关于doinbackground 中的 java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28919062/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com