[英]inputStream.read() null pointer exception
I have sometimes error but can't understand why : 我有时会出错,但不明白为什么:
Code : 代码:
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 () 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: 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)
This error is sometimes. 有时会出现此错误。 But it's error :( Can any help me solve this problem ? I can't undestand why this error. 但这是错误的:(有什么可以帮助我解决这个问题吗?我无法理解为什么会出现此错误。
Regards, Peter. 问候,彼得。
Some time there may be problem, with connectivity due to which in = new BufferedInputStream(conn.getInputStream()); 有时可能会出现连接问题,这是由于新连接导致的。BufferedInputStream(conn.getInputStream()); throws error, and input stream remain null. 引发错误,并且输入流保持为空。 YOu need to handle null case also to get rid of the issue. 您还需要处理空值情况以摆脱此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.