I'm working on an app that uses a Web API via HttpGet and it works fine when 
part of the onCreate section.
In order to make the code more portable and visible to all the activities I 
thought it would be wise to create a helper class.
I copied the code to a new class and now I seem to be getting a 
NullPointerException on the DefaultHttpClient.execute(HttpGet); call...

The line in the class code that seems to cause the NPE is:
          String responseBody = client.execute(get, responseHandler);

Here is my code in the calling activity:

        MyHelper uh = new MyHelper();

        JSONObject login = uh.queryHost(this, "user", "pass", "query");

Here is my class code:

public class MyHelper {

private static String hostURL = "http://api.host.com";;
private static String apiURL = "/v3/";
private static String apiKey = "?key=**********";
 private ProgressDialog pDialog = null;
private DefaultHttpClient client = null;
 public MyHelper() {
// TODO Auto-generated constructor stub
}
 public JSONObject queryHost(Context passedContext, String username,String 
password, String query){
     try {
     
     pDialog = ProgressDialog.show(passedContext, "Please Wait..", 
"Connecting to Host",true);
     
     HttpGet get = new HttpGet(hostURL+apiURL+query+apiKey);
     get.addHeader("Authorization", "Basic " + 
getCredentials(username,password));
     
     ResponseHandler<String> responseHandler = new BasicResponseHandler();
     
     //I've tried both of these below with the same result
     String responseBody = client.execute(get, responseHandler);
       HttpResponse resp = client.execute(get);

     //JSONObject response = new JSONObject(responseBody);
       //JSONObject userInfo = 
response.getJSONObject("results").getJSONObject("user");
     
     Log.i("Log", "Response: " + "TEST"); //response);
     //Log.i("Log", "JSON: " + userInfo);
     
     return new JSONObject(); //response;
     
     } catch (Exception e) {
     
     //pDialog.dismiss();
     AlertDialog.Builder dialog = new AlertDialog.Builder(passedContext);
        dialog.setTitle("Connection Error");
        dialog.setMessage("Unable To Connect To Host, Check Your 
Username/Password & Try Again");
        dialog.setNeutralButton("Ok", null);
        dialog.create().show();
     Log.e("Err", "ERROR: ", e);
     
     }
     
     return new JSONObject();
     
    }
    
    public String getCredentials(String username, String password){
     
     return (Base64.encodeBytes((username + ":" + convertToMD5(password) 
).getBytes()));
     
    }
    
    public String convertToMD5(String s){
     try {
     
     //Create the MD5
     MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
     digest.update(s.getBytes());
     byte messageDigest[] = digest.digest();
     
     //Create Hex
     StringBuffer hexString = new StringBuffer();
     for (int i=0; i<messageDigest.length; i++)
     hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
     return hexString.toString();
     
     } catch (NoSuchAlgorithmException e) {
     
     e.printStackTrace();
     
     }
     
     return null;
     
    }

}

Here is the NPE:

java.lang.NullPointerException
     at com.myapp.MyHelper.queryHost(MyHelper.java:48)
     at com.myapp.Startup.onCreate(Startup.java:22)
     at 
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
     at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
     at 
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
     at android.os.Handler.dispatchMessage(Handler.java:99)
     at android.os.Looper.loop(Looper.java:123)
     at android.app.ActivityThread.main(ActivityThread.java:4363)
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:521)
     at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
     at dalvik.system.NativeStart.main(Native Method)

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to