Brahadeesh
Brahadeesh

Reputation: 2255

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.

Upvotes: 0

Views: 2349

Answers (3)

BarFoo
BarFoo

Reputation: 69

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.

Upvotes: 1

duffymo
duffymo

Reputation: 308733

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

Upvotes: 2

Chris Stratton
Chris Stratton

Reputation: 40337

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.

Upvotes: 2

Related Questions