【问题标题】:IOException e is null [duplicate]IOException e 为空 [重复]
【发布时间】:2012-09-25 15:48:13
【问题描述】:

可能重复:
Exception is NULL always

我遇到了一个关于 IOException 对象的奇怪问题,找不到解决办法。

代码如下:

try { // This isn't very important part, but maybe it has something to do with a problem
  HttpResponse response = client.execute(httpGet);
  StatusLine statusLine = response.getStatusLine();
  int statusCode = statusLine.getStatusCode();
  if (statusCode == 200) {
    HttpEntity entity = response.getEntity();
    InputStream content = entity.getContent();
    BufferedReader reader = new BufferedReader(new InputStreamReader(content));
    String line;
    while ((line = reader.readLine()) != null) {
      builder.append(line);
    }
  } else {
    Log.e(ParseJSON.class.toString(), "Failed to download file");
  }
} catch (IOException e) {
        System.out.println("I'm here in the IOException catch clause");
        e.printStackTrace(); // e is null (line 126)
} catch (Exception e) {
        e.printStackTrace();
}

程序捕获IOException,但它的对象(e)是null。这怎么可能?

编辑:堆栈跟踪:

09-25 19:35:59.438: I/System.out(31732): I'm here in the IOException catch clause
09-25 19:36:04.377: W/dalvikvm(31732): threadid=1: thread exiting with uncaught exception (group=0x40020ac0)
09-25 19:36:04.447: E/AndroidRuntime(31732): FATAL EXCEPTION: main
09-25 19:36:04.447: E/AndroidRuntime(31732): java.lang.RuntimeException: Unable to start activity ComponentInfo{uniwersytet.ekiosk/uniwersytet.ekiosk.EkioskActivity}: java.lang.NullPointerException
09-25 19:36:04.447: E/AndroidRuntime(31732):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-25 19:36:04.447: E/AndroidRuntime(31732):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-25 19:36:04.447: E/AndroidRuntime(31732):    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-25 19:36:04.447: E/AndroidRuntime(31732):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-25 19:36:04.447: E/AndroidRuntime(31732):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-25 19:36:04.447: E/AndroidRuntime(31732):    at android.os.Looper.loop(Looper.java:123)
09-25 19:36:04.447: E/AndroidRuntime(31732):    at android.app.ActivityThread.main(ActivityThread.java:4627)
09-25 19:36:04.447: E/AndroidRuntime(31732):    at java.lang.reflect.Method.invokeNative(Native Method)
09-25 19:36:04.447: E/AndroidRuntime(31732):    at java.lang.reflect.Method.invoke(Method.java:521)
09-25 19:36:04.447: E/AndroidRuntime(31732):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
09-25 19:36:04.447: E/AndroidRuntime(31732):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
09-25 19:36:04.447: E/AndroidRuntime(31732):    at dalvik.system.NativeStart.main(Native Method)
09-25 19:36:04.447: E/AndroidRuntime(31732): Caused by: java.lang.NullPointerException
09-25 19:36:04.447: E/AndroidRuntime(31732):    at uniwersytet.ekiosk.EkioskActivity.onCreate(EkioskActivity.java:126)
09-25 19:36:04.447: E/AndroidRuntime(31732):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-25 19:36:04.447: E/AndroidRuntime(31732):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-25 19:36:04.447: E/AndroidRuntime(31732):    ... 11 more

【问题讨论】:

  • 只是小费。如果你捕捉到Exception e,你不需要捕捉任何其他Exception 类,因为它们是Exception 类的孩子。
  • 你怎么知道e 为空?
  • 可以包含堆栈跟踪吗?
  • 似乎很多“不可能”的事情都可以用 android 来实现。这至少是我第二次遇到不可能发生或没有任何意义的错误。
  • 我怀疑你只是有行号问题。

标签: java android json


【解决方案1】:

-请检查您从服务器获取的response

-还请注意...UI work should be on UI thread and Non-UI work on the Non-UI thread,来自 HoneyComb 版本的 Android 已成为规则。

-请使用thread with handlerAsyncTask 处理此类耗时较长的任务。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2012-05-07
    • 2010-10-26
    • 1970-01-01
    • 2016-12-28
    相关资源
    最近更新 更多