0

I'm trying to access an web API server and passing a json object to it. The problem is, I'm always getting this exception. I really don't know what's wrong with my code. Here it is:

    String email = EmailAddress.getText().toString();
    String password = Password.getText().toString();
    String url = "SOME URL";

    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);
    JSONObject loginObj = new JSONObject();

    try
    {
        loginObj.put("Username", email);
        loginObj.put("Password", password);

        // Prepare JSON to send by setting the entity
        httpPost.setEntity(new StringEntity(loginObj.toString(), "UTF-8"));

        // Set up the header types needed to properly transfer JSON
        httpPost.setHeader("Content-Type", "application/json");
        httpPost.setHeader("Accept-Encoding", "application/json");
        httpPost.setHeader("Accept-Language", "en-US");

        // Execute POST
        HttpResponse response = httpClient.execute(httpPost);

        Log.i("Response String", response.getEntity().toString());
    }
    catch(Exception e)
    {
        Log.e("Exception", e.getMessage().toString());
    }

Here's my Log:

02-02 23:06:37.080: E/AndroidRuntime(26513): FATAL EXCEPTION: main
02-02 23:06:37.080: E/AndroidRuntime(26513): java.lang.IllegalStateException: Could not execute method of the activity
02-02 23:06:37.080: E/AndroidRuntime(26513):    at android.view.View$1.onClick(View.java:3691)
02-02 23:06:37.080: E/AndroidRuntime(26513):    at android.view.View.performClick(View.java:4211)
02-02 23:06:37.080: E/AndroidRuntime(26513):    at android.view.View$PerformClick.run(View.java:17267)
02-02 23:06:37.080: E/AndroidRuntime(26513):    at android.os.Handler.handleCallback(Handler.java:615)
02-02 23:06:37.080: E/AndroidRuntime(26513):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-02 23:06:37.080: E/AndroidRuntime(26513):    at android.os.Looper.loop(Looper.java:137)
02-02 23:06:37.080: E/AndroidRuntime(26513):    at android.app.ActivityThread.main(ActivityThread.java:4898)
02-02 23:06:37.080: E/AndroidRuntime(26513):    at java.lang.reflect.Method.invokeNative(Native Method)
02-02 23:06:37.080: E/AndroidRuntime(26513):    at java.lang.reflect.Method.invoke(Method.java:511)
02-02 23:06:37.080: E/AndroidRuntime(26513):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
02-02 23:06:37.080: E/AndroidRuntime(26513):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
02-02 23:06:37.080: E/AndroidRuntime(26513):    at dalvik.system.NativeStart.main(Native Method)
02-02 23:06:37.080: E/AndroidRuntime(26513): Caused by: java.lang.reflect.InvocationTargetException
02-02 23:06:37.080: E/AndroidRuntime(26513):    at java.lang.reflect.Method.invokeNative(Native Method)
02-02 23:06:37.080: E/AndroidRuntime(26513):    at java.lang.reflect.Method.invoke(Method.java:511)
02-02 23:06:37.080: E/AndroidRuntime(26513):    at android.view.View$1.onClick(View.java:3686)
02-02 23:06:37.080: E/AndroidRuntime(26513):    ... 11 more
02-02 23:06:37.080: E/AndroidRuntime(26513): Caused by: java.lang.NullPointerException
02-02 23:06:37.080: E/AndroidRuntime(26513):    at com.example.maddiosapp.Login.userLogin(Login.java:81)
02-02 23:06:37.080: E/AndroidRuntime(26513):    ... 14 more

Any Ideas? Thanks!

Luke Villanueva
  • 2,030
  • 8
  • 44
  • 94

2 Answers2

1

It looks like you are doing network operations on UI thread, and your try catch block is catching android.os.NetworkOnMainThreadException. At least this looks like probable code from your call stack, also you are getting password/user name in the same function in which you do Network request, you should get those values on UI thread.

You can see here:

http://androidxref.com/4.4.2_r1/xref/frameworks/base/core/java/android/os/StrictMode.java#1145

that NetworkOnMainThreadException is created with no message provided, so when you call getMessage().toString(), getMessage() returns null, causing NullPointerException - as in your case.

marcinj
  • 48,511
  • 9
  • 79
  • 100
  • I think there's is a way to allow network operations on the main thread. I'm planning to use some loaders here. I think it is related to strict mode. Any ideas? – Luke Villanueva Feb 02 '14 at 15:29
  • You better switch to AsyncTask solution, since 3.0 (or so) this exception is always thrown for Network operatins done on UI thread : http://www.androiddesignpatterns.com/2012/06/app-force-close-honeycomb-ics.html – marcinj Feb 02 '14 at 15:33
0

Log.e("Exception", e.getMessage().toString()); is the line throwing an exception. The reason is easy: Catching a generic exception (in most cases) is a bad practice and should be avoided.

Each time you use try you should catch specific exceptions that might occur, in your case you're probably catching an exception that was not intended by your try statement.

More info on this here.

Community
  • 1
  • 1
nKn
  • 13,691
  • 9
  • 45
  • 62