0

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.

5
  • what is tag or dta and where are they defined? Commented Aug 2, 2011 at 19:10
  • Please post what your Logcat says, What is dta? Commented Aug 2, 2011 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. Commented Aug 2, 2011 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 Commented Aug 2, 2011 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? Commented Aug 2, 2011 at 19:20

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

let me check and get back to you
Thank you. I got it. I had not called the constructor. Its working now.
By the way is there a way to check if I have received the information in the php file?
2

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.

Comments

1

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.

1 Comment

Thank you. Please refer to comments above. That was not the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.