Вопрос

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!

Это было полезно?

Решение

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);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top