나는 첫 번째 안드로이드 애플 리케이션을 만드는 과정에 있으며,이 앱의 목표는 외부 데이터베이스에 연결하는 것입니다. 아래 구문은 java.lang.NullPointerException 오류 메시지를 제공합니다. 아래는 나의 모든 구문과 logcat 정보입니다. 내가 도대체 뭘 잘못하고있는 겁니까?외부 데이터베이스에 연결 안드로이드 발생 원인 : java.lang.NullPointerException
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;
}
}
로그 캣 정보
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)
Log.e ("Entity Response :", entityResponse);'line? –