繁体   English   中英

连接到外部数据库Android由:java.lang.NullPointerException

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

我正在构建我的第一个android应用程序,此应用程序的目标是连接到外部数据库。 下面的语法给了我一个java.lang.NullPointerException错误消息。 以下是我所有的语法和logcat信息。 我究竟做错了什么?

主要活动

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信息

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)

您正在循环中将JSONObject设置为null:

JSONObject json = null;

但是我看不到您创建JSONObject的任何地方。 您应该在循环之前使用以下命令:

JSONObject json = new JSONObject();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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