I'm trying to read some information from an url using HttpURLConnection, however, when I try to run it I get:
java.lang.RuntimeException: Unable to start activity ComponentInfo{...}: java.lang.NullPointerException: println needs a message.
I've seen plenty examples that do just this, so what's the problem? I've double checked the url, when I copy/paste it to a web browser I get the message I'm supposed to get, no problems. I use the same url to get information to a different program (written in C++) and there it works just fine. Is there a connection setting I've missed or something?
Here's the code, the problem occurs at "InputStream stream = connection.getInputStream();":
public String download_organisations(String url){
String jsonString = "";
try {
if(!url.startsWith("http://")){
url = "http://" + url;
}
if(url.endsWith("/")){
url = url.substring(0, url.lastIndexOf("/"));
}
url = url + SystemStatic.URL_SERVICE_WEB_ORGANISATION_MANAGER;
Log.d("OrganisationManager-Android","url: " + url);
URL httpUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) httpUrl.openConnection();
connection.setConnectTimeout(10000);
InputStream stream = connection.getInputStream();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[1024];
while ((nRead = stream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
byte[] inputStreamByteArray = buffer.toByteArray();
byte[] base64 = Base64.decode(inputStreamByteArray, 0);
byte[] decrypted = CipherUtils.decrypt(base64);
jsonString = decrypted.toString();
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
Log.d("OrganisationManager-Android", "Download succeded with response: " + connection.getResponseCode());
} else {
Log.d("OrganisationManager-Android", "Download failed with response: " + connection.getResponseCode());
}
} catch (MalformedURLException e) {
Log.e("OrganisationManager-Android", e.getMessage());
} catch (IOException e) {
Log.e("OrganisationManager-Android", e.getMessage());
} catch (Exception e) {
Log.e("OrganisationManager-Android", e.getMessage());
}
return jsonString;
}
Error stack:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.organisationmanager.cin, PID: 2066
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.organisationmanager.cin/com.organisationmanager.ble.ScanningActivity}: java.lang.NullPointerException: println needs a message
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3194)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3302)
at android.app.ActivityThread.-wrap12(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1891)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7425)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
Caused by: java.lang.NullPointerException: println needs a message
at android.util.Log.println_native(Native Method)
at android.util.Log.e(Log.java:261)
at com.organisationmanager.ble.common.OrganisationManagerWebPortalCommunicationHelper.download_organisations(OrganisationManagerWebPortalCommunicationHelper.java:210)
at com.organisationmanager.ble.ScanningActivity.onCreate(ScanningActivity.java:184)
at android.app.Activity.performCreate(Activity.java:7372)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3147)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3302)
at android.app.ActivityThread.-wrap12(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1891)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7425)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)