inputStream.read()null指针异常

时间:2012-01-28 08:32:31

标签: android nullpointerexception inputstream

我有时会出错,但无法理解原因:

代码:

public void newConnect()
        { try {
          urlka = new URL(myUrl);

        } catch (MalformedURLException e) {

            e.printStackTrace();
        }
          HttpURLConnection conn = null;
        try {
            conn = (HttpURLConnection) urlka.openConnection();
            conn.connect();
            Log.v("Responcse code","Response code " +conn.getResponseCode());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
           try {
             try {
                in = new BufferedInputStream(conn.getInputStream());

             } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
          result =  readStream(in); /// line 1198

           }
            finally {
             conn.disconnect();
           }

readStrem()

private String readStream(InputStream is)
        {
                    try 
                    {
                      ByteArrayOutputStream bo = new ByteArrayOutputStream();
                     int i = is.read(); /// **error was here** line 1247


                      while(i != -1) 
                      {
                        bo.write(i);

                        i = is.read();
                      }

                      return bo.toString();
                    } catch (IOException e) 
                    {
                      return ""+e;
                    }
        }
            }

logcat的:

java.lang.RuntimeException: An error occured while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:200)
    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
    at java.lang.Thread.run(Thread.java:1096)
Caused by: java.lang.NullPointerException
    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl$LocalCloseInputStream.read(HttpURLConnectionImpl.java:167)
    at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:157)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:243)
    at myApp.activity.com.myApp.readStream(myApp.java:1247)
    at myApp.activity.com.myApp.newConnect(myApp.java:1198)
    at myApp.activity.com.myApp$newRun.doInBackground(myApp.java:424)
    at myApp.activity.com.myApp$newRun.doInBackground(myApp.java:1)
    at android.os.AsyncTask$2.call(AsyncTask.java:185)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)

有时会出现此错误。但这是错误:(任何可以帮助我解决这个问题? 我无法理解为什么会出现这种错误。

问候,彼得。

1 个答案:

答案 0 :(得分:1)

有些时候可能会出现问题,连接因为in = new BufferedInputStream(conn.getInputStream());抛出错误,输入流保持为空。你也需要处理空案以摆脱这个问题。