0

My app crashes when I do any networking activities (specifically, reading text through an InputStream), but only on Android Pie. I've tried it using emulators running various API versions, but this only happens on API 28. I have also tried this on an actual phone running Pie, and it crashes there as well. The app runs fine on all other Android versions afaik.

The logcat shows this:

2018-12-09 14:59:16.917 6864-6925/<package> E/AndroidRuntime: FATAL EXCEPTION: Thread-4
    Process: <package>, PID: 6864
    java.lang.NullPointerException
        at java.io.Reader.<init>(Reader.java:78)
        at java.io.InputStreamReader.<init>(InputStreamReader.java:72)
        at <package>.<Activity class>.<method>(<Activity class>.java:304)

The statement that the stack trace leads to in my code is:

BufferedReader br = new BufferedReader(new InputStreamReader(text));

where text = con.getInputStream(); up above, which when I debugged, was returning null. con is a URLConnection that opens a connection to a particular URL. The value of con seems alright and is identical to con values on different API versions.

So, the culprit seems to be text = con.getInputStream(); which is returning normally on API 27 and below, but is returning null on Pie.

Any idea what could be causing this? Let me know if there's any more information I could provide.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Faraaz
  • 61
  • 4
  • Please write the whole piece of code from when you create the URLConnection object to when you execute the getInputStream(). We cannot be sure that all is OK until we see the full code. – emandt Dec 09 '18 at 10:43

1 Answers1

0

Okay, I figured out why the problem was only on Pie. Pie enforces HTTPS and the URL I was using was an HTTP one, which is why getInputStream() returned null on the connection.

I followed the steps here which fixed it.

Faraaz
  • 61
  • 4