1

我有一个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 变量为空并且异常被捕获。根据我的说法,没有语法错误或逻辑错误。我错过了什么或任何错误的任务吗?

这是我的日志猫,

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)

请建议我并纠正我!提前致谢!!

4

3 回答 3

4

您需要Properties在访问它之前进行实例化。采用:

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

代替:

List<PropertiesList> Properties;

顺便说一句,根据java命名约定,变量应该以小写字母开头,所以最好改为使用properties

于 2013-05-22T12:27:26.937 回答
2

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

List<Properties> Properties = new ArrayList<Properties>();
于 2013-05-22T12:32:36.583 回答
2

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

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

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

Properties.add(localProperty);

注意你的命名。变量,函数必须是驼峰式,即以小写字母开头,后面的单词以大写字母开头。以大写字母开头的单词通常是类的名称。

于 2013-05-22T12:34:46.493 回答