开发者

Android Java Null Pointer Exception

I am trying to connect to a database on a remote server.

I have the following code with private details masked:


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());
        }
    }

My manifest file is as follows:

< 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" />

In logcat I have the following :

Inside connectToServer

java.lang.NullPointerException

Why am I getting a NullPointerException ?

Thank you.


Some object you dereference inside the method is null, so it's probably eta.


You are most likely trying to call a method of a non-existent object that you were not successful in creating.

Without the exception handler, you would get the line number where the error occurs in the logs.


Try actually specifying the type of the ArrayList

for example ArrayList<BasicNameValuePair> toDB = new ArrayList<BasicNameValuePair>();

also make sure tag or dta are actually initialized somewhere (maybe when you delcare them you can go ahead and assign them the empty string. for example: String tag = ""; or DTA dta = new DTA(); or whatever dta is.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜