doinbackground中的java.lang.NullPointerException

时间:2015-03-07 19:34:09

标签: java android

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;
}

1 个答案:

答案 0 :(得分:1)

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

您已经拥有以下代码:

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为空。