Wednesday, September 19, 2018

php - JSON Android Error- Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.getString(java.lang.String)' on a null object reference



My app is crashing and the log is showing this error:



Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.getString(java.lang.String)' on a null object reference on the line where I do:



ab = jobj.getString("title");


I'm a noob to Android development. Please help!




public class MainActivity extends ActionBarActivity {

JSONObject jobj = null;
ClientServerInterface clientServerInterface = new ClientServerInterface();
TextView textView;
String ab = "";

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textView = (TextView) findViewById(R.id.textView);
new RetrieveData().execute();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();


//noinspection SimplifiableIfStatement
if (id == R.id.action_settings)
{
return true;
}

return super.onOptionsItemSelected(item);
}


class RetrieveData extends AsyncTask
{
protected String doInBackground(String... arg0) {
jobj = clientServerInterface.makeHttpRequest("http://**.***.***.**/printresult.php");
try {
ab = jobj.getString("title");
} catch (JSONException e) {
e.printStackTrace();
}
return ab;

}

protected void onPostExecute(String ab){
textView.setText(ab);
}
}
}


Here's the other file:




public class ClientServerInterface {

static InputStream is = null;
static JSONObject jobj = null;
static String json = "";


public ClientServerInterface(){
}

public JSONObject makeHttpRequest(String url){
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
try{
HttpResponse httpresponse = httpclient.execute(httppost);
HttpEntity httpentity = httpresponse.getEntity();
is = httpentity.getContent();
}catch (ClientProtocolException e){
e.printStackTrace();
}catch (IOException e){

e.printStackTrace();
}

try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
try{
while((line = reader.readLine())!= null){
sb.append(line+"\n");

}
is.close();
json = sb.toString();
try{
jobj = new JSONObject(json);
}catch (JSONException e){
e.printStackTrace();
}
}catch(IOException e){
e.printStackTrace();

}
}catch (UnsupportedEncodingException e){
e.printStackTrace();
}
return jobj;
}
}


Maybe it is in my php file?




require_once('connection.php');

$qry = "SELECT * FROM eventdetails";
$result = mysql_query($qry);
$rows = array();
while($r = mysql_fetch_assoc($result)){
$rows[] = $r;
}

echo json_encode($rows);
mysql_close();
?>


I'm really stumped. Please help!


Answer



The error, as user1023110 pointed out, happens because the constructor is throwing an exception. In this case, I was giving the constructor a JSONArray and it giving the error that it could not be converted to JSONObject. First, I added this to the top of my ClientServerInterface class:



static JSONArray jarr = null;



Then, I added these two lines:



jarr = new JSONArray(json);
jobj = jarr.getJSONObject(0);


where I previously had this:




jobj = new JSONObject(json);


The method now returns the first JSONObject of the JSONArray.



Thanks to all your help!


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...