1

I'm trying to create an android login application using Json with MySQL Data Base but when I run it and try to login, I get the following error :

11-22 12:29:08.079: E/AndroidRuntime(731): FATAL EXCEPTION: main
11-22 12:29:08.079: E/AndroidRuntime(731): java.lang.NullPointerException
11-22 12:29:08.079: E/AndroidRuntime(731):  at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
11-22 12:29:08.079: E/AndroidRuntime(731):  at com.example.loctrans.DBConnection.DBConnection(DBConnection.java:41)
11-22 12:29:08.079: E/AndroidRuntime(731):  at com.example.loctrans.MainActivity.onClick(MainActivity.java:56)
11-22 12:29:08.079: E/AndroidRuntime(731):  at android.view.View.performClick(View.java:4084)
11-22 12:29:08.079: E/AndroidRuntime(731):  at android.view.View$PerformClick.run(View.java:16966)
11-22 12:29:08.079: E/AndroidRuntime(731):  at android.os.Handler.handleCallback(Handler.java:615)
11-22 12:29:08.079: E/AndroidRuntime(731):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-22 12:29:08.079: E/AndroidRuntime(731):  at android.os.Looper.loop(Looper.java:137)
11-22 12:29:08.079: E/AndroidRuntime(731):  at android.app.ActivityThread.main(ActivityThread.java:4745)
11-22 12:29:08.079: E/AndroidRuntime(731):  at java.lang.reflect.Method.invokeNative(Native Method)
11-22 12:29:08.079: E/AndroidRuntime(731):  at java.lang.reflect.Method.invoke(Method.java:511)
11-22 12:29:08.079: E/AndroidRuntime(731):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-22 12:29:08.079: E/AndroidRuntime(731):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-22 12:29:08.079: E/AndroidRuntime(731):  at dalvik.system.NativeStart.main(Native Method)

when I was verifying the code, I added a log.e() method to find the line with error. here is my BD connection code

public class DBConnection extends Activity{
String result =  null;
    InputStream is = null;

public String DBConnection(ArrayList<NameValuePair> nameValuePairs, String URL){

      try{
          Log.e("info error", " fase 2");
          HttpClient httpclient = new DefaultHttpClient();
          Log.e("info error", " fase 3");
          HttpPost httppost = new HttpPost(URL);
          Log.e("info error", " fase 4");
          httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
          Log.e("info error", httppost.toString());
          HttpResponse response = httpclient.execute(httppost);
          Log.e("info error", " fase 6");
          HttpEntity entity = response.getEntity();
          Log.e("info error", " fase 7");
          is = entity.getContent();
          Log.e("info error", " fase 8");
  }catch(Exception e){
      Toast.makeText(getApplicationContext(), "Error in http connection"+e.toString(), Toast.LENGTH_LONG).show();
  }
  //convert response to string
  try{
      Log.e("info error", " fase 9");
          BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
          StringBuilder sb = new StringBuilder();
          Log.e("info error", " fase 10");
          String line = null;
          while ((line = reader.readLine()) != null) {
                  sb.append(line + "\n");
          }
          is.close();
          result=sb.toString();
          Log.e("log_tag", "Cadena JSon " + result);
  }catch(Exception e){
      Toast.makeText(getApplicationContext(), "Error converting result "+e.toString(), 
              Toast.LENGTH_LONG).show();
      Log.e("log_tag", "Error converting result " + e.toString());
  }
  return result;
}

public String getResult(){
    return result;
}

}

In the logcat only there are the first three log.e message.

11-22 12:29:07.899: E/info error(731):  fase 2
11-22 12:29:07.919: E/info error(731):  fase 3
11-22 12:29:07.929: E/info error(731):  fase 4

so I think the problem is at this line : httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs) but I don't know what is the error exactly

Here is a part of Main Activity code :

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("Login" ,logi));
String URL = "http://192.168.1.10/Android/getlogin.php";          
DBConnection DBC = new DBConnection();
String result = DBC.DBConnection(nameValuePairs, URL);
0

2 Answers 2

1
java.lang.NullPointerException
  at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
  at com.example.loctrans.DBConnection.DBConnection(DBConnection.java:41)

The LogCat reports the problem is with getApplicationContext() in your Toast, but this in in the catch block. You are right the real problem is with your Http code, it is probably throwing a NetworkOnMainThread exception. (Are you using Android 3.0+?)

You need to execute potentially slow network operations in a different thread. Simply create a custom AsyncTask and move your code into doInBackground().


Why does DBConnection extend Activity? Are you using it like an Activity in code that you haven't posted?

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

1 Comment

thanks for your help i'm trying to use AsyncTask i'm using Android 4.1 for the DBConnection is only used in the code that I have posted, so i don't have to extend it from Activity.
0

Inside an Activity you should to use the keyword this where you to need a context and not getApplicationContext().

Toast.makeText(this, "Error in http connection"+e.toString(), Toast.LENGTH_LONG).show();

This error refers to this, and, like Sam says, there are a problem related to your HTTP code running inside a Thread UI. You need to use a AsyncTask to do all your HTTP job.

http://developer.android.com/reference/android/os/AsyncTask.html

3 Comments

Using this won't work either. The Toast is being called in the Activity's constructor, not onCreate() method and the Activity is being started with new DBConnection()... There are quite a few things wrong with the sara's current approach.
I'm confused because the method don't like a constructor... public String DBConnection ...
Good catch, it is a method. This is a horrible practice: method names should start with a lowercase letter and shouldn't share the same name as the class...

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.