1. Download our Official Android App: Forums for Android!

Apps Null Pointer Exception In Method Boolean.Org.Json.Jsonobject Do in background async task

Discussion in 'Android Development' started by ahmedsa, Jun 22, 2016.

  1. ahmedsa

    ahmedsa Lurker
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    3
    Joined:
    Jun 7, 2016

    Jun 7, 2016
    3
    0
    15
    Male
    I designed app to make order for pizza food restaurant
    I show userid,menu id ,address,longtiude,latitude then press on button make order
    Error show after I press button and found in async task doin background
    It give me fatal exception error
    An error occurred while executing doInBackground()
    and it show to me two lines have proplem
    AddNewOrder.doInBackground(Summary.java:113)
    AddNewOrder.doInBackground(Summary.java:77)
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean org.json.JSONObject.getBoolean(java.lang.String)' on a null object reference
    Code (Text):
    1.  
    2. package com.pizza_final_project_app.pizza_final_project;
    3. import android.app.Activity;
    4. import android.app.ProgressDialog;
    5. import android.content.Intent;
    6. import android.content.SharedPreferences;
    7. import android.os.AsyncTask;
    8. import android.preference.PreferenceManager;
    9. import android.support.v7.app.AppCompatActivity;
    10. import android.os.Bundle;
    11. import android.view.View;
    12. import android.widget.AdapterView;
    13. import android.widget.Button;
    14. import android.widget.TextView;
    15. import android.widget.Toast;
    16.  
    17. import org.apache.http.NameValuePair;
    18. import org.apache.http.message.BasicNameValuePair;
    19. import org.json.JSONException;
    20. import org.json.JSONObject;
    21.  
    22. import java.util.ArrayList;
    23. import java.util.List;
    24.  
    25. public class Summary extends Activity {
    26. SharedPreferences preferences;
    27. private ProgressDialog pDialog;
    28. //JSONParser jsonParser = new JSONParser();
    29. boolean errorFound;
    30. TextView textaddress;
    31. TextView textlongtiude;
    32. TextView textlatitude;
    33. TextView text2;
    34. TextView textuser;
    35. Button btnorder;
    36. private static final String TAG_SUCCESS = "success";
    37. private static final String TAG = Summary.class.getSimpleName();
    38.  
    39. @Override
    40. protected void onCreate(Bundle savedInstanceState) {
    41. super.onCreate(savedInstanceState);
    42. setContentView(R.layout.activity_summary);
    43. text2=(TextView)findViewById(R.id.textView3);
    44. textaddress=(TextView)findViewById(R.id.textaddress);
    45. textuser=(TextView)findViewById(R.id.textView4);
    46. textlongtiude=(TextView)findViewById(R.id.textlongtiude);
    47. textlatitude=(TextView)findViewById(R.id.textlatitude);
    48. Intent i = getIntent();
    49. String id = i.getStringExtra("Data4");
    50. String address = i.getStringExtra("Data8");
    51. String longtiude = i.getStringExtra("Data9");
    52. String latitude = i.getStringExtra("Data10");
    53. text2.setText(id);
    54. textaddress.setText(address);
    55. textlongtiude.setText(longtiude);
    56. textlatitude.setText(latitude);
    57. preferences= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    58.  
    59. String email=preferences.getString("email","");
    60. textuser.setText(email);
    61. pDialog = new ProgressDialog(this);
    62. pDialog.setCancelable(false);
    63. btnorder=(Button)findViewById(R.id.button2);
    64. btnorder.setOnClickListener(new View.OnClickListener() {
    65. @Override
    66. public void onClick(View v)
    67. {
    68. addOrder(textuser.getText().toString().trim(), text2.getText().toString().trim(),
    69. textaddress.getText().toString().trim(), textlongtiude.getText().toString().trim(),
    70. textlatitude.getText().toString().trim());
    71.  
    72. }
    73. });
    74.  
    75.  
    76. }
    77. //line 77
    78. class AddNewOrder extends AsyncTask<String, String, String> {
    79.  
    80. @Override
    81. protected void onPreExecute() {
    82. super.onPreExecute();
    83. pDialog = new ProgressDialog(Summary.this);
    84. pDialog.setMessage("Making Order...");
    85. pDialog.setIndeterminate(false);
    86. pDialog.setCancelable(true);
    87. pDialog.show();
    88. }
    89.  
    90. protected String doInBackground(String... args) {
    91. final JSONParser jsonParser = new JSONParser();
    92. // Building Parameters
    93. List<NameValuePair> params = new ArrayList<NameValuePair>();
    94. params.add(new BasicNameValuePair("userID", args[0]));
    95. params.add(new BasicNameValuePair("menuID", args[1]));
    96. params.add(new BasicNameValuePair("address", args[2]));
    97. params.add(new BasicNameValuePair("longitude", args[3]));
    98. params.add(new BasicNameValuePair("latitude", args[4]));
    99.  
    100.  
    101. JSONObject jObj = jsonParser.makeHttpRequest(AppConfig.URL_Order,
    102. "POST", params);
    103.  
    104. try {
    105. //Line 113
    106. boolean error = jObj.getBoolean("error");
    107. if (!error) {
    108.  
    109. errorFound=false;
    110. } else {
    111. errorFound=true;
    112.  
    113. }
    114. } catch (JSONException e) {
    115. e.printStackTrace();
    116. }
    117.  
    118. return null;
    119. }
    120. protected void onPostExecute(String file_url) {
    121. if (pDialog.isShowing()) {
    122. pDialog.dismiss();
    123. if (!errorFound){
    124. Toast.makeText(getApplicationContext(), "User successfully registered. Try login now!", Toast.LENGTH_LONG).show();
    125.  
    126.  
    127. Intent intent = new Intent(
    128. Summary.this,
    129. FinalResult.class);
    130. startActivity(intent);
    131. finish();
    132. }
    133. else{
    134. Toast.makeText(getApplicationContext(), "An Error occoured!", Toast.LENGTH_LONG).show();
    135.  
    136. }
    137. }
    138. }
    139.  
    140. }
    141. */
    142. private void addOrder(final String userID, final String menuID, final String address,
    143. final String longitude, final String latitude) {
    144.  
    145. new AddNewOrder().execute(userID, menuID, address, longitude, latitude);
    146.  
    147. }
    148.  
    149. }
    150.  

    Class AppConfig
    Code (Text):
    1.  
    2. package com.pizza_final_project_app.pizza_final_project;
    3.  
    4. public class AppConfig {
    5.  
    6. public static String URL_Order = "http://xxxxx.";
    7.  
    json parser
    Code (Text):
    1.  
    2. package com.pizza_final_project_app.pizza_final_project;
    3. import android.util.Log;
    4. import org.apache.http.HttpEntity;
    5. import org.apache.http.HttpResponse;
    6. import org.apache.http.NameValuePair;
    7. import org.apache.http.client.ClientProtocolException;
    8. import org.apache.http.client.entity.UrlEncodedFormEntity;
    9. import org.apache.http.client.methods.HttpGet;
    10. import org.apache.http.client.methods.HttpPost;
    11. import org.apache.http.client.utils.URLEncodedUtils;
    12. import org.apache.http.impl.client.DefaultHttpClient;
    13. import org.json.JSONException;
    14. import org.json.JSONObject;
    15.  
    16. import java.io.BufferedReader;
    17. import java.io.IOException;
    18. import java.io.InputStream;
    19. import java.io.InputStreamReader;
    20. import java.io.UnsupportedEncodingException;
    21. import java.util.List;
    22.  
    23. public class JSONParser {
    24.  
    25. static InputStream is = null;
    26. static JSONObject jObj = null;
    27. static String json = "";
    28.  
    29. // constructor
    30. public JSONParser() {
    31.  
    32. }
    33.  
    34. // function get json from url
    35. // by making HTTP POST or GET mehtod
    36. public JSONObject makeHttpRequest(String url, String method,
    37. List<NameValuePair> params) {
    38.  
    39. // Making HTTP request
    40. try {
    41.  
    42. // check for request method
    43. if(method == "POST"){
    44. // request method is POST
    45. // defaultHttpClient
    46. DefaultHttpClient httpClient = new DefaultHttpClient();
    47. HttpPost httpPost = new HttpPost(url);
    48. httpPost.setEntity(new UrlEncodedFormEntity(params));
    49.  
    50. HttpResponse httpResponse = httpClient.execute(httpPost);
    51. HttpEntity httpEntity = httpResponse.getEntity();
    52. is = httpEntity.getContent();
    53.  
    54. }else if(method == "GET"){
    55. // request method is GET
    56. DefaultHttpClient httpClient = new DefaultHttpClient();
    57. String paramString = URLEncodedUtils.format(params, "utf-8");
    58. url += "?" + paramString;
    59. Log.d("data",url);
    60. HttpGet httpGet = new HttpGet(url);
    61.  
    62. HttpResponse httpResponse = httpClient.execute(httpGet);
    63. HttpEntity httpEntity = httpResponse.getEntity();
    64. is = httpEntity.getContent();
    65. }
    66.  
    67.  
    68. } catch (UnsupportedEncodingException e) {
    69. e.printStackTrace();
    70. } catch (ClientProtocolException e) {
    71. e.printStackTrace();
    72. } catch (IOException e) {
    73. e.printStackTrace();
    74. }
    75.  
    76. try {
    77. BufferedReader reader = new BufferedReader(new InputStreamReader(
    78. is, "iso-8859-1"), 8);
    79. StringBuilder sb = new StringBuilder();
    80. String line = null;
    81. while ((line = reader.readLine()) != null) {
    82. sb.append(line + "\n");
    83. }
    84. is.close();
    85. json = sb.toString();
    86. Log.d("json","data is "+json);
    87.  
    88. } catch (Exception e) {
    89. Log.e("Buffer Error", "Error converting result " + e.toString());
    90. }
    91. try {
    92. jObj = new JSONObject(json);
    93. } catch (JSONException e) {
    94. Log.e("JSON Parser", "Error parsing data " + e.toString());
    95. }
    96.  
    97. // return JSON String
    98. return jObj;
    99.  
    100. }
    101. }
    102.  
    103.  
     

    Advertisement

  2. HeavensSentSword

    HeavensSentSword Well-Known Member
    Rank:
    None
    Points:
    93
    Posts:
    134
    Joined:
    Jan 19, 2016

    Jan 19, 2016
    134
    36
    93
    Male
    That means that there is no JSON object that you are receiving.
    Are you getting anything for the Json object at all? Put a breakpoint there and see the value of it
     
  3. ahmedsa

    ahmedsa Lurker
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    3
    Joined:
    Jun 7, 2016

    Jun 7, 2016
    3
    0
    15
    Male
    After break point i get : Error parsing data org.json.JSONException: End of input at character 0 of
    so that what i modify to solve error
     
  4. HeavensSentSword

    HeavensSentSword Well-Known Member
    Rank:
    None
    Points:
    93
    Posts:
    134
    Joined:
    Jan 19, 2016

    Jan 19, 2016
    134
    36
    93
    Male
    So basically your http calls are not getting anything in response I think. I will have to look at your code a little closer. Give me a few.
     
  5. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,326
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,326
    10,464
    1,988
    Male
    Software developer
    South West of England
    I agree it sounds like the server is sending nothing back in the response. In which case the problem lies with the server side code and the OP needs to debug that end.
     
  6. HeavensSentSword

    HeavensSentSword Well-Known Member
    Rank:
    None
    Points:
    93
    Posts:
    134
    Joined:
    Jan 19, 2016

    Jan 19, 2016
    134
    36
    93
    Male
    Why are you using a Post instead of a GET. If you want something useful from the Server I always use GET. Post sends stuff to the server and returns just a OK unless the server sends stuff back. I would try a GET instead of a Post
     

Share This Page

Loading...