即使分配了值</class>,List <class>变量也返回null

时间:2013-05-22 12:25:08

标签: java android nullpointerexception

我有一个java代码文件,

public class PropertyRequestService {

static String result;
PropertiesList localProperty = new PropertiesList();
List<PropertiesList> Properties;


public List<PropertiesList> getAllPropertiesStuff() {
    JSONArray json = null;
    try {

        HttpClient client = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(
                "Given URL");
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs
                .add(Passing parameter));

        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = client.execute(httppost);

        // HttpEntity entity = response.getEntity();
        // if (entity != null) {

        /*
         * result = EntityUtils.toString(entity); Log.i("RESPONSE=",
         * result);
         */
        // List<PropertiesList> tst=(List<PropertiesList>)response;

        ArrayList<String> jsonstring = getJSONString(response);

        System.out.println("jsonString : " + jsonstring);
        json = new JSONArray(jsonstring);       

        localProperty.angle=30;
            localProperty.PropertyID =Integer.parseInt(json.getString(0));
            localProperty.Latitude =Double.parseDouble(json.getString(2));
            localProperty.Longitude = Double.parseDouble(json.getString(3));
            localProperty.PropertyPrice = Integer.parseInt(json.getString(4));
            localProperty.PropertyAddress = json.getString(1);
            localProperty.listfrom=0;

            Properties.add(localProperty);


    } catch (Exception e1) {
        e1.printStackTrace();
    }

    return Properties;
}

public ArrayList<String> getJSONString(HttpResponse response) {

    try {
        HttpEntity entity = response.getEntity();

        String result = EntityUtils.toString(entity);
        InputStream stream = new ByteArrayInputStream((result.replace("&",
                " ")).getBytes("UTF-8"));
        DocumentBuilder builder = DocumentBuilderFactory.newInstance()
                .newDocumentBuilder();

        Document doc = builder.parse(stream);

        NodeList n1 = doc.getElementsByTagName("PropertyID");
        NodeList n2 = doc.getElementsByTagName("Address");
        NodeList n3 = doc.getElementsByTagName("Lat");
        NodeList n4 = doc.getElementsByTagName("Lng");
        NodeList n5 = doc.getElementsByTagName("Price");
        Node nn1 = n1.item(0);
        Node nn2 = n2.item(0);
        Node nn3 = n3.item(0);
        Node nn4 = n4.item(0);
        Node nn5 = n5.item(0);
        String str1 = nn1.getFirstChild().getNodeValue();
        String str2 = nn2.getFirstChild().getNodeValue();
        String str3 = nn3.getFirstChild().getNodeValue();
        String str4 = nn4.getFirstChild().getNodeValue();
        String str5 = nn5.getFirstChild().getNodeValue();

        ArrayList<String> nodes = new ArrayList<String>();
        nodes.add(str1);
        nodes.add(str2);
        nodes.add(str3);
        nodes.add(str4);
        nodes.add(str5);

        System.out.println("Node value : " + nodes);
        return nodes;
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

}

和PropertiesList类,

public class PropertiesList {

 public int angle;
 public int PropertyID;
 public int PropertyPrice;
 public String PropertyAddress;
 public Double Latitude;
 public Double Longitude;
 public static int listfrom;
}

从我称之为的活动类

   PropertyRequestService properties=new PropertyRequestService();
    propertyRSList=  properties.getAllPropertiesStuff();

问题是,在“Properties.add(localProperty);”行附近,我确实发现Properties变量为null并且捕获了Exception。   根据我没有语法错误或逻辑错误。我错过了什么或错误的任务吗?

这是我的logcat,

05-22 17:51:46.192: W/dalvikvm(3317): threadid=1: thread exiting with uncaught exception (group=0x40018578)
05-22 17:51:46.684: E/AndroidRuntime(3317): FATAL EXCEPTION: main
05-22 17:51:46.684: E/AndroidRuntime(3317): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.BuildersUpdate/com.BuildersUpdate.PropertySearchTypes.CameraSearch}: java.lang.NullPointerException
05-22 17:51:46.684: E/AndroidRuntime(3317):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
05-22 17:51:46.684: E/AndroidRuntime(3317):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
05-22 17:51:46.684: E/AndroidRuntime(3317):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-22 17:51:46.684: E/AndroidRuntime(3317):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
05-22 17:51:46.684: E/AndroidRuntime(3317):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-22 17:51:46.684: E/AndroidRuntime(3317):     at android.os.Looper.loop(Looper.java:130)
05-22 17:51:46.684: E/AndroidRuntime(3317):     at android.app.ActivityThread.main(ActivityThread.java:3687)
05-22 17:51:46.684: E/AndroidRuntime(3317):     at java.lang.reflect.Method.invokeNative(Native Method)
05-22 17:51:46.684: E/AndroidRuntime(3317):     at java.lang.reflect.Method.invoke(Method.java:507)
05-22 17:51:46.684: E/AndroidRuntime(3317):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
05-22 17:51:46.684: E/AndroidRuntime(3317):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
05-22 17:51:46.684: E/AndroidRuntime(3317):     at dalvik.system.NativeStart.main(Native Method)
05-22 17:51:46.684: E/AndroidRuntime(3317): Caused by: java.lang.NullPointerException
05-22 17:51:46.684: E/AndroidRuntime(3317):     at com.BuildersUpdate.PropertySearchTypes.PropertyRequestService.getAllPropertiesStuff(PropertyRequestService.java:90)
05-22 17:51:46.684: E/AndroidRuntime(3317):     at com.BuildersUpdate.PropertySearchTypes.CameraSearch.onCreate(CameraSearch.java:200)
05-22 17:51:46.684: E/AndroidRuntime(3317):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-22 17:51:46.684: E/AndroidRuntime(3317):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)

请建议我并纠正我!在此先感谢!!

3 个答案:

答案 0 :(得分:4)

在访问之前,您需要实例化Properties。使用:

List<PropertiesList> Properties = new ArrayList<>();

而不是:

List<PropertiesList> Properties;

BTW,根据java命名约定,变量应以小写字母开头,因此最好使用properties代替。

答案 1 :(得分:2)

您尚未初始化List<Properties> Properties。您刚刚创建了它的引用。

List<Properties> Properties = new ArrayList<Properties>();

答案 2 :(得分:2)

您刚刚定义了一个引用属性,它将指向ArrayList类型的对象。您还需要使用 new()运算符创建实际对象。

List<PropertiesList> Properties = new ArrayList<>();

然后您可以在其中添加数据。

Properties.add(localProperty);

照顾你的namings 。变量,函数必须是驼峰,即以小写字母开头,后续单词以大写字母开头。以caps开头的单词通常是类的名称。