up vote 0 down vote favorite
share [g+] share [fb]

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.

link|improve this question

what is tag or dta and where are they defined? – jondavidjohn Aug 2 '11 at 19:10
Please post what your Logcat says, What is dta? – Kumar Bibek Aug 2 '11 at 19:10
Are the objects "dta" and "tag" referencing anything? Those are the only things I can see that's not being initialized here. – DeeV Aug 2 '11 at 19:11
@jondavidjohn I have defined them as private variables in the class that contains this method. Kumar I have the relavant logcat entries in my question. DDV Please refer above – Brahadeesh Aug 2 '11 at 19:15
Could you please post the whole Logcat? The stack trace could be useful here. Which line is specified for the NullPointerException? Also, where do you initialize dta and tag? – thegrinner Aug 2 '11 at 19:20
feedback

3 Answers

up vote 2 down vote accepted

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

link|improve this answer
let me check and get back to you – Brahadeesh Aug 2 '11 at 19:16
Thank you. I got it. I had not called the constructor. Its working now. – Brahadeesh Aug 2 '11 at 19:18
By the way is there a way to check if I have received the information in the php file? – Brahadeesh Aug 2 '11 at 19:23
feedback

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.

link|improve this answer
Thank you. I got the error. – Brahadeesh Aug 2 '11 at 19:22
feedback

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.

link|improve this answer
1  
Thank you. Please refer to comments above. That was not the problem. – Brahadeesh Aug 2 '11 at 19:18
ok, glad you figured it out though. – BarFoo Aug 2 '11 at 19:19
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.