简体   繁体   中英

Connect To External Database Android Caused by: java.lang.NullPointerException

I'm in the process of building my first android app and my goal of this app is to connect to an external database. The syntax below is giving me a java.lang.NullPointerException error message. Below is all of my syntax and logcat information. What am I doing wrong?

MainActivity

package com.example.connecttoexternaldatabase;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.support.v7.app.ActionBarActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

private TextView responseTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    responseTextView = (TextView) findViewById(R.id.responseTextView);

    new GetAllCustomerTask().execute(new ApiConnector());
}

public void setTextToTextView(JSONArray jsonArray){
    String theTextToDisplayOnPhone = "";
    for(int i=0; i<jsonArray.length(); i++){
        JSONObject json = null;
        try{
            json = jsonArray.getJSONObject(i);
            theTextToDisplayOnPhone = theTextToDisplayOnPhone +
                    "Name : " +json.getString("FirstName")+" "+ json.get("LastName")+"\n"+
                    "Age : "+json.getInt("Age")+"\n"+
                    "Mobile Using : "+json.getString("Mobile")+"\n\n";
        } catch (JSONException e){
            e.printStackTrace();
        }
    }

    responseTextView.setText(theTextToDisplayOnPhone);
}

private class GetAllCustomerTask extends AsyncTask<ApiConnector, Long, JSONArray>{

    @Override
    protected JSONArray doInBackground(ApiConnector... params) {
        // is it executed on the background thread
        return params[0].GetAllCustomers();
    }

    @Override
    protected void onPostExecute(JSONArray jsonArray){
        // it is executed on the main thread
        setTextToTextView(jsonArray);
    }

}

}

ApiConnector

package com.example.connecttoexternaldatabase;

import java.io.IOException;
import java.net.HttpURLConnection;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;

import android.util.Log;

public class ApiConnector {

    public JSONArray GetAllCustomers(){
        String url = "http://localhost/app/getAllCustomers.php";

        HttpEntity httpEntity = null;

        try{
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            httpEntity = httpResponse.getEntity();

        } catch (ClientProtocolException e){
            e.printStackTrace();
        } catch (IOException e){
            e.printStackTrace();
        }

        JSONArray jsonArray = null;

        if(httpEntity != null){
            try{
                String entityResponse = EntityUtils.toString(httpEntity);
                Log.e("Entity Response: ", entityResponse);
                jsonArray = new JSONArray(entityResponse);
            } catch (JSONException e){
                e.printStackTrace();
            } catch (IOException e){
                e.printStackTrace();
            }
        }

        return jsonArray;
    }
}

Logcat Information

01-03 13:59:19.599: E/AndroidRuntime(762): FATAL EXCEPTION: main
01-03 13:59:19.599: E/AndroidRuntime(762): java.lang.NullPointerException
01-03 13:59:19.599: E/AndroidRuntime(762):  at com.example.connecttoexternaldatabase.MainActivity.setTextToTextView(MainActivity.java:31)
01-03 13:59:19.599: E/AndroidRuntime(762):  at com.example.connecttoexternaldatabase.MainActivity$GetAllCustomerTask.onPostExecute(MainActivity.java:58)
01-03 13:59:19.599: E/AndroidRuntime(762):  at com.example.connecttoexternaldatabase.MainActivity$GetAllCustomerTask.onPostExecute(MainActivity.java:1)
01-03 13:59:19.599: E/AndroidRuntime(762):  at android.os.AsyncTask.finish(AsyncTask.java:417)
01-03 13:59:19.599: E/AndroidRuntime(762):  at android.os.AsyncTask.access$300(AsyncTask.java:127)
01-03 13:59:19.599: E/AndroidRuntime(762):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
01-03 13:59:19.599: E/AndroidRuntime(762):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-03 13:59:19.599: E/AndroidRuntime(762):  at android.os.Looper.loop(Looper.java:130)
01-03 13:59:19.599: E/AndroidRuntime(762):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-03 13:59:19.599: E/AndroidRuntime(762):  at java.lang.reflect.Method.invokeNative(Native Method)
01-03 13:59:19.599: E/AndroidRuntime(762):  at java.lang.reflect.Method.invoke(Method.java:507)
01-03 13:59:19.599: E/AndroidRuntime(762):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:870)
01-03 13:59:19.599: E/AndroidRuntime(762):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
01-03 13:59:19.599: E/AndroidRuntime(762):  at dalvik.system.NativeStart.main(Native Method)

You are setting JSONObject to null in your loop:

JSONObject json = null;

But I don't see anywhere where you create your JSONObject. You should have this before your loop:

JSONObject json = new JSONObject();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM