1

New Android programmed.

The nullpointer is happening on this line:

 String toBeConverted = XmlFile(myURL, "username@user.com",
                "password");
    InputStream is = new ByteArrayInputStream(toBeConverted.getBytes());

Here is the method I am using to get the String, which is working because I am getting the correct response from server

06-20 15:15:40.804: I/RESPONSE(2270): <?xml version="1.0" encoding="utf-8"?>
06-20 15:15:40.804: I/RESPONSE(2270): <CarObject>
06-20 15:15:40.804: I/RESPONSE(2270):   <ResultCode>0</ResultCode>
06-20 15:15:40.804: I/RESPONSE(2270):   <ResultMessage>success</ResultMessage>
06-20 15:15:40.804: I/RESPONSE(2270):   <Cardata xsi:type="ArrayOfCarStatus">
06-20 15:15:40.804: I/RESPONSE(2270):     <CarStatus> Car
06-20 15:15:40.804: I/RESPONSE(2270):       <CarID>241</CarID>
06-20 15:15:40.804: I/RESPONSE(2270):       <CarName>ASC</CarName>
06-20 15:15:40.804: I/RESPONSE(2270):     </CarStatus>
06-20 15:15:40.804: I/RESPONSE(2270):   </CarData>
06-20 15:15:40.804: I/RESPONSE(2270): </CarObject>

Here is the method I used to get the XML

public String XmlFile(String url, String username, String password) {

        try {

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);

            nameValuePairs.add(new BasicNameValuePair("user", username));
            nameValuePairs.add(new BasicNameValuePair("password", password));

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);

            HttpEntity resEntity = response.getEntity();

            Log.i("RESPONSE", EntityUtils.toString(resEntity));
            return EntityUtils.toString(resEntity);

        } catch (ClientProtocolException e) {

            Log.e("Log, ClientProtocol Exception", e.toString());
        } catch (IOException e) {
            Log.e("Log, IO Exception", e.toString());
        } catch (IllegalArgumentException e) {
            Log.e("Log, IllegalArgument Exception", e.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.e("a", "XmlFile returning null string");
        return null;

    }

LogCat:

06-20 15:15:40.819: W/System.err(2270):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-20 15:15:40.819: W/System.err(2270):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-20 15:15:40.819: W/System.err(2270):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-20 15:15:40.819: W/System.err(2270):     at android.os.Looper.loop(Looper.java:137)
06-20 15:15:40.819: W/System.err(2270):     at android.app.ActivityThread.main(ActivityThread.java:5041)
06-20 15:15:40.819: W/System.err(2270):     at java.lang.reflect.Method.invokeNative(Native Method)
06-20 15:15:40.827: W/System.err(2270):     at java.lang.reflect.Method.invoke(Method.java:511)
06-20 15:15:40.827: W/System.err(2270):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-20 15:15:40.827: W/System.err(2270):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-20 15:15:40.827: W/System.err(2270):     at dalvik.system.NativeStart.main(Native Method)
06-20 15:15:40.827: E/a(2270): XmlFile returning null string
06-20 15:15:40.827: D/AndroidRuntime(2270): Shutting down VM
06-20 15:15:40.827: W/dalvikvm(2270): threadid=1: thread exiting with uncaught exception (group=0x40ccc930)
06-20 15:15:40.835: E/AndroidRuntime(2270): FATAL EXCEPTION: main
06-20 15:15:40.835: E/AndroidRuntime(2270): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bentseytlin.maps/com.bentseytlin.maps.MainActivity}: java.lang.NullPointerException
06-20 15:15:40.835: E/AndroidRuntime(2270):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-20 15:15:40.835: E/AndroidRuntime(2270):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-20 15:15:40.835: E/AndroidRuntime(2270):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-20 15:15:40.835: E/AndroidRuntime(2270):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-20 15:15:40.835: E/AndroidRuntime(2270):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-20 15:15:40.835: E/AndroidRuntime(2270):     at android.os.Looper.loop(Looper.java:137)
06-20 15:15:40.835: E/AndroidRuntime(2270):     at android.app.ActivityThread.main(ActivityThread.java:5041)
06-20 15:15:40.835: E/AndroidRuntime(2270):     at java.lang.reflect.Method.invokeNative(Native Method)
06-20 15:15:40.835: E/AndroidRuntime(2270):     at java.lang.reflect.Method.invoke(Method.java:511)
06-20 15:15:40.835: E/AndroidRuntime(2270):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-20 15:15:40.835: E/AndroidRuntime(2270):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-20 15:15:40.835: E/AndroidRuntime(2270):     at dalvik.system.NativeStart.main(Native Method)
06-20 15:15:40.835: E/AndroidRuntime(2270): Caused by: java.lang.NullPointerException
06-20 15:15:40.835: E/AndroidRuntime(2270):     at com.bentseytlin.maps.MainActivity.onCreate(MainActivity.java:84)
06-20 15:15:40.835: E/AndroidRuntime(2270):     at android.app.Activity.performCreate(Activity.java:5104)
06-20 15:15:40.835: E/AndroidRuntime(2270):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-20 15:15:40.835: E/AndroidRuntime(2270):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-20 15:15:40.835: E/AndroidRuntime(2270):     ... 11 more

UPDATED: I am not sure why my XmlFile function is returned null when the log.i message is coming across as expected. Thanks for the help!

benzabill
  • 259
  • 1
  • 8
  • 21

1 Answers1

2

This logcat message:

06-20 15:15:40.827: E/a(2270): XmlFile returning null string

shows that your XmlFile() function is returning null, and causing the subsequent null pointer exception.

Likely the fact that your LOGGING the entity, and that it is a stream, means you can't subsequently read it again. Remove the LOGGING statement and your code will likely work. See http://developer.android.com/reference/org/apache/http/HttpEntity.html#isRepeatable() for discussion of HttpEntity and repeatable streams.

// This should work...
String strEntity = EntityUtils.toString(resEntity);   // read the Entity once!
Log.i("RESPONSE", strEntity);
return (strEntity);
CSmith
  • 13,318
  • 3
  • 39
  • 42
  • Yes yes, but I do not understand why my function is returning null. The response that is coming back is clearly not null(based on the fact that the log.i message is coming across as expected in LogCat) but on the next line it gets caught by the try-catch for some reason – benzabill Jun 20 '13 at 19:37
  • thanks, that seems to do the trick! I would upvote but i do not yet have 15 rep – benzabill Jun 20 '13 at 19:40
  • @benzabill Well, have some points from me towards your 15 total. +1 for a good "beginners" question. So many questions here do not even include the basic information. I also upvoted CSmith for you. To find the problem quickly, put a breakpoint on the first line of the function and step through it. If you don't know what that means, read a debugging Android tutorial. You will be amazed! – Simon Jun 20 '13 at 19:43
  • thanks @Simon, I have read enough questions(in my quest for android programming enlightenmen) on SO to figure out that plenty of information is needed! – benzabill Jun 20 '13 at 20:00
  • got enough rep, just came back to +1 you, @Simon – benzabill Jun 24 '13 at 15:34