<android>如何抛出NullPointerException

时间:2017-11-06 18:06:19

标签: android

if(post_data != null && bufferedWriter != null)
    {
        bufferedWriter.write(post_data);
    }

如果其中任何一个为空,它甚至不应该执行!但它仍然执行并抛出错误。

每个人都专注于错误的问题。我解决了错误:我只是移动了初始化并关闭了写入和读取功能,如下所示:

 public void write(String post_data) throws IOException {
    outputStream = httpURLConnection.getOutputStream();
    bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
    bufferedWriter.write(post_data);
    bufferedWriter.flush();
    bufferedWriter.close();
    outputStream.close();
}

public String read() throws IOException {
    inputStream = httpURLConnection.getInputStream();
    bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
    String result="", line;

    while((line = bufferedReader.readLine()) != null){
        result += line;
    }
    bufferedReader.close();
    inputStream.close();
    return result;
}

1 个答案:

答案 0 :(得分:0)

从您的重复问题中,您从代码中得到错误

protected void onPostExecute(String result){

    if(result.equals("Login successful")){...

这里你得到了result = null并尝试将它与String“Login successful”进行比较。希望它能帮到你!