Reputation: 2737
I have a java code file,
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;
}
}
and PropertiesList class,
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;
}
and from the activity class I call like this,
PropertyRequestService properties=new PropertyRequestService();
propertyRSList= properties.getAllPropertiesStuff();
The problem is that, near the line "Properties.add(localProperty);", I did observe that Properties variable is null and Exception is caught. There is no syntax error or logical error according to me. Did I miss anything or any wrong assignment?
This is my 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)
Please suggest me and correct me!! Thanks in advance!!
Upvotes: 1
Views: 1286
Reputation: 19294
You need to instantiate Properties
before you access it. Use:
List<PropertiesList> Properties = new ArrayList<>();
instead of:
List<PropertiesList> Properties;
BTW, according to java naming conventions, variables should start with lower case letter, so it is better to use properties
instead.
Upvotes: 4
Reputation: 68965
You have just defined a reference Properties which will point to object of type ArrayList. You also need to create the actual object using the new() operator.
List<PropertiesList> Properties = new ArrayList<>();
Then you can add your data in it.
Properties.add(localProperty);
Take care of your namings. Variable,functions must be in camel case i.e starts with a small letter and subsequent words start with capital letter. Words starting with caps are generally names of classes.
Upvotes: 2
Reputation: 25267
You have not initialized the List<Properties> Properties
. You have just created a reference to it.
List<Properties> Properties = new ArrayList<Properties>();
Upvotes: 2