android - NullPointerException:解析XML,从ByteArrayInputStream(string.getBytes())创建InputStream

标签 android nullpointerexception xmlpullparser bytearrayinputstream

新的Android编程。

空指针发生在这一行:

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


这是我用来获取字符串的方法,该方法有效,因为我从服务器获取了正确的响应

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>


这是我用来获取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


更新:
我不确定为什么log.i消息按预期出现时,为什么我的XmlFile函数返回null。谢谢您的帮助!

最佳答案

此日志消息:

06-20 15:15:40.827:E / a(2270):XmlFile返回空字符串

显示您的XmlFile()函数返回null,并导致随后的null指针异常。

您的LOGGING实体及其为流的事实很可能意味着您随后无法再次读取它。删除LOGGING语句,您的代码将可能工作。有关HttpEntity和可重复流的讨论,请参见http://developer.android.com/reference/org/apache/http/HttpEntity.html#isRepeatable()

// This should work...
String strEntity = EntityUtils.toString(resEntity);   // read the Entity once!
Log.i("RESPONSE", strEntity);
return (strEntity);

关于android - NullPointerException:解析XML,从ByteArrayInputStream(string.getBytes())创建InputStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17222091/

相关文章:

java - getCurrentSession 在创建 Session 时返回 null

android - XmlPullParser:获取包含 XML 标签的内部文本

android ksoap调用xmlpullparser异常

java - Android KXmlParser“NoSuchMethodError:java.lang.System.arraycopy([CI [CII)V

java - 收到错误java.lang.RuntimeException:无法启动 Activity ComponentInfo

android - Android Studio 是否使用 Gradle 守护进程?

android - 在 fragment 内的异步任务中显示()进度对话框

android - Android应用程序停止

java - 使用 Java 集合排序时如何处理空值

android - 是否可以在没有音频源的情况下使用 CamcorderProfile?