There is an app where I’m working on for 1 week right now. Unfortunately there is an error that is hard to solve because I really don’t know what to do right now. See below a copy of the LogCat list.
01-04 00:14:23.662: E/log_tag(3073): Error in http connectionandroid.os.NetworkOnMainThreadException
01-04 00:14:23.662: E/log_tag(3073): Error converting result java.lang.NullPointerException
01-04 00:14:23.672: D/AndroidRuntime(3073): Shutting down VM
01-04 00:14:23.672: W/dalvikvm(3073): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
01-04 00:14:23.692: E/AndroidRuntime(3073): FATAL EXCEPTION: main
01-04 00:14:23.692: E/AndroidRuntime(3073): java.lang.RuntimeException: Unable to start activity ComponentInfo{united.aristal.freewallet/united.aristal.freewallet.GroupHistory}: java.lang.NullPointerException
01-04 00:14:23.692: E/AndroidRuntime(3073): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
01-04 00:14:23.692: E/AndroidRuntime(3073): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
01-04 00:14:23.692: E/AndroidRuntime(3073): at android.app.ActivityThread.access$600(ActivityThread.java:122)
01-04 00:14:23.692: E/AndroidRuntime(3073): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
01-04 00:14:23.692: E/AndroidRuntime(3073): at android.os.Handler.dispatchMessage(Handler.java:99)
01-04 00:14:23.692: E/AndroidRuntime(3073): at android.os.Looper.loop(Looper.java:137)
01-04 00:14:23.692: E/AndroidRuntime(3073): at android.app.ActivityThread.main(ActivityThread.java:4340)
01-04 00:14:23.692: E/AndroidRuntime(3073): at java.lang.reflect.Method.invokeNative(Native Method)
01-04 00:14:23.692: E/AndroidRuntime(3073): at java.lang.reflect.Method.invoke(Method.java:511)
01-04 00:14:23.692: E/AndroidRuntime(3073): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-04 00:14:23.692: E/AndroidRuntime(3073): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-04 00:14:23.692: E/AndroidRuntime(3073): at dalvik.system.NativeStart.main(Native Method)
01-04 00:14:23.692: E/AndroidRuntime(3073): Caused by: java.lang.NullPointerException
01-04 00:14:23.692: E/AndroidRuntime(3073): at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
01-04 00:14:23.692: E/AndroidRuntime(3073): at org.json.JSONTokener.nextValue(JSONTokener.java:94)
01-04 00:14:23.692: E/AndroidRuntime(3073): at org.json.JSONArray.<init>(JSONArray.java:87)
01-04 00:14:23.692: E/AndroidRuntime(3073): at org.json.JSONArray.<init>(JSONArray.java:103)
01-04 00:14:23.692: E/AndroidRuntime(3073): at united.aristal.freewallet.GroupHistory.retrieveGroupInfo(GroupHistory.java:295)
01-04 00:14:23.692: E/AndroidRuntime(3073): at united.aristal.freewallet.GroupHistory.onCreate(GroupHistory.java:95)
01-04 00:14:23.692: E/AndroidRuntime(3073): at android.app.Activity.performCreate(Activity.java:4465)
01-04 00:14:23.692: E/AndroidRuntime(3073): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-04 00:14:23.692: E/AndroidRuntime(3073): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
01-04 00:14:23.692: E/AndroidRuntime(3073): ... 11 more
Copy of the script where the current problem exists:
/********************************************************************
* RETRIEVING GROUP INFO *
*********************************************************************/
public void retrieveGroupInfo(){
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("link");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch(Exception e){
Log.e("log_tag", "Error in http connection"+e.toString());
}
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line="0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n\n");
}
is.close();
result = sb.toString();
}
catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
try{
jArray = new JSONArray(result);
JSONObject json_data=null;
for(int i = 0; i < jArray.length(); i++){
json_data = jArray.getJSONObject(i);
retrievedId.add(json_data.getInt("id"));
String getDate = recoverDate(json_data.getString("date"));
String getTime = json_data.getString("time");
String getCategory = json_data.getString("category");
String getObjectName = json_data.getString("object_name");
String getObjectVal = json_data.getString("object_value");
String getValType = json_data.getString("value_type");
//String getUsername = json_data.getString("username");
historyItems.add(
getCategory + "\t: " + getObjectName + "\n" +
"Price\t: € " + getObjectVal + "\n" +
"Date\t: " + getDate + " " + getTime);
historyValueType.add(getValType);
}
ListView lv = (ListView) findViewById(R.id.listViewMenu);
lv.setAdapter(fancyAdapter);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
alertdialogUpdateDelete(position);
//showToast(historyItem.get(position).toString());
}
});
}
catch(JSONException e1){
//Toast.makeText(getBaseContext(), "No Record Found." ,Toast.LENGTH_LONG).show();
}
catch (ParseException e1) {
e1.printStackTrace();
}
}
I’m really frustrated right now and stuck for a couple hours, so please help me. I’m not sure but I think that there is some problem with a thread or something? Thanx for helping me out here.
Your code is trying to make a network connection on the main thread. This is not allowed in Android; that’s why you are receiving an
android.os.NetworkOnMainThreadExceptionwhen you try to connect. After you catch the exception and log it, your code still tries to use it, which is causing the following problems. (You should probably report failure and skip trying to read the connection if you catch an exception when making the connection.)The solution is to move your network activities to a worker thread. An AsyncTask is a nice way to do this. Read the guide topic Processes and Threads and the blog entry Painless threading for more info.