[英]Android Java Null Pointer Exception
我正在嘗試連接到遠程服務器上的數據庫。
我有以下代碼,其中隱藏了私人詳細信息:
public static void connectToServer () {
Log.e(tag,"Inside connectToServer");
String result = "";
ArrayList toDB = new ArrayList();
//Assign namevalue pairs to toDB
try {
toDB.add(new BasicNameValuePair("A",dta.getA()));
toDB.add(new BasicNameValuePair("B",dta.getB()));
toDB.add(new BasicNameValuePair("C",dta.getC()));
toDB.add(new BasicNameValuePair("D",dta.getD()));
toDB.add(new BasicNameValuePair("E",dta.getE()));
toDB.add(new BasicNameValuePair("F",dta.getF()));
} catch (Exception e) {
Log.e(tag,e.toString());
}
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://mydom.com/myFolder/file.php");
httppost.setEntity(new UrlEncodedFormEntity(toDB));
}catch(Exception e){
Log.e(tag, "Error in http connection "+e.toString());
}
}
我的清單文件如下:
< uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
< uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
< uses-permission android:name="android.permission.INTERNET" />
在logcat中,我有以下內容:
Inside connectToServer
java.lang.NullPointerException
為什么會收到NullPointerException?
謝謝。
您最有可能嘗試調用未成功創建的不存在對象的方法。
沒有異常處理程序,您將獲得日志中發生錯誤的行號。
您在方法內取消引用的某些對象為null,因此它可能是eta。
嘗試實際指定ArrayList
的類型
例如ArrayList<BasicNameValuePair> toDB = new ArrayList<BasicNameValuePair>();
還應確保標記或dta實際上已在某處初始化(也許當您對它們進行delcare時,可以繼續給它們分配空字符串。例如: String tag = "";
DTA dta = new DTA();
或其他dta。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.