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

if anyone of them is null, it shouldn't even execute! but it still executes and throws an error.

Everyone was focused on the wrong problem.I solved the error: I just moved the initialization and close inside the write and read functions as follows:

 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;
}
Kais Salha
  • 33
  • 7
  • check twice , maybe the code throwing error is something else then you have posted here – Shankar Nov 06 '17 at 18:08
  • I checked many times. If I remove or change bufferedWriter.write(post_data); with anything else, the program works fine!!! The full code is here, he closed the question without helping: https://stackoverflow.com/questions/47141604/android-getting-error-using-bufferedwriter-writepost-data-after-moving-it-t?noredirect=1#comment81233198_47141604 – Kais Salha Nov 06 '17 at 18:11
  • that question is having error in `onPostExecution()` method where `string.equals` method is used read that error log and method there – Shankar Nov 06 '17 at 18:14
  • 2
    That code doesn't throw that exception. The line throwing the exception is `if(result.equals("Login successful")){` – Andy Turner Nov 06 '17 at 18:25
  • I added the stack since the write function is executed. The errors start to show then. – Kais Salha Nov 06 '17 at 18:51

1 Answers1

0

from your duplicate question you got error from the code

protected void onPostExecute(String result){

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

Here you got result = null and trying to compare it with the String "Login successful". Hope it would help you!

Leontsev Anton
  • 727
  • 7
  • 12
  • I added the stack since the write function is executed. The errors start to show when the write function is executed. – Kais Salha Nov 06 '17 at 18:48
  • watch in debugger the values of post_data and bufferedWriter in your code here bufferedWriter.write(post_data); – Leontsev Anton Nov 06 '17 at 19:24
  • I don't really know how to watch the values, but it seems that onPostExecute is executed instantly after the write instead of waiting for the doInBackground to finish which is the main reason for what is happening. – Kais Salha Nov 06 '17 at 19:58
  • Weird thing if I put it not in a separate class, it works fine but when I put it in this class it doesn't work. All I did was just move it to a class! – Kais Salha Nov 06 '17 at 20:00
  • just use "Run" tab of android studio then "Evaluate expression" to watch values. – Leontsev Anton Nov 07 '17 at 18:17