Reputation: 41
I am new to Android, i have been trying JSON parsing using PHP and MYSQL, when i run app got this error.
Error:
FATAL EXCEPTION: main Process: com.example.admin.androidjsonparsing, PID: 13399 java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116) at org.json.JSONTokener.nextValue(JSONTokener.java:94) at org.json.JSONArray.(JSONArray.java:92) at org.json.JSONArray.(JSONArray.java:108) at com.example.admin.androidjsonparsing.MainActivity.loadIntoListView(MainActivity.java:79) at com.example.admin.androidjsonparsing.MainActivity.access$000(MainActivity.java:22) at com.example.admin.androidjsonparsing.MainActivity$1GetJSON.onPostExecute(MainActivity.java:51) at com.example.admin.androidjsonparsing.MainActivity$1GetJSON.onPostExecute(MainActivity.java:38) at android.os.AsyncTask.finish(AsyncTask.java:695) at android.os.AsyncTask.access$600(AsyncTask.java:180) at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Here the code:
MainActivity.java
package com.example.admin.androidjsonparsing;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
getJSON("http://192.168.1.4/AndroidJSONParsing/getdata.php");
}
private void getJSON(final String urlWebService) {
class GetJSON extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
try {
loadIntoListView(s);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
protected String doInBackground(Void... voids) {
try {
URL url = new URL(urlWebService);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
StringBuilder sb = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String json;
while ((json = bufferedReader.readLine()) != null) {
sb.append(json + "\n");
}
return sb.toString().trim();
} catch (Exception e) {
return null;
}
}
}
GetJSON getJSON = new GetJSON();
getJSON.execute();
}
private void loadIntoListView(String json) throws JSONException {
JSONArray jsonArray = new JSONArray(json);
String[] heroes = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
heroes[i] = obj.getString("name");
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, heroes);
listView.setAdapter(arrayAdapter);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.admin.androidjsonparsing.MainActivity">
<!-- this is our listview where we will display the fetched data -->
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
Upvotes: 1
Views: 15204
Reputation: 17354
This error can also thrown (rather unexpectedly) if the URI is null or not set as in the following example
URL url = new URL(xml_file);
URLConnection conn = url.openConnection();
Error will be thrown on second line, if xml_file is empty (that is not set). Make sure it has a valid value. You can use Log.e("App", "XML File is " + xml_file);
to check it value in he logs
Upvotes: 0
Reputation: 1149
Check for the null string.
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if(s!=null){
Toast.makeText(getApplicationContext(),s,Toast.LENGTH_SHORT).show();
try {
loadIntoListView(s);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
Upvotes: 2
Reputation: 2785
The problem are ocurred because your String json
is not a valid json, maybe because it is null
or bad formated json
, then try as follow:
if (s!=null && !TextUtils.isEmpty(s)) loadIntoListView(s);
private void loadIntoListView(String json) throws JSONException {
JSONArray jsonArray = new JSONArray(json);
try {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
heroes[i] = obj.getString("name");
}
} catch(JSONException e) {
//handler json exception
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, heroes);
listView.setAdapter(arrayAdapter);
}
Upvotes: 0
Reputation:
Try this code ...
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (!TextUtils.isEmpty(s) && s!=null) {
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
try {
loadIntoListView(s);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
Upvotes: 0