Android app crashes with error
AndroidRuntime: Process: com.example.myapplication, PID: 30895
AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to read from null array
AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(Unknown Source:621)
AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(Unknown Source:47)
AndroidRuntime: at android.app.servertransaction.LaunchActivityItem.execute(Unknown Source:63)
AndroidRuntime: at android.app.servertransaction.TransactionExecutor.executeCallbacks(Unknown Source:77)
AndroidRuntime: at android.app.servertransaction.TransactionExecutor.execute(Unknown Source:73)
AndroidRuntime: at android.app.ActivityThread$H.handleMessage(Unknown Source:43)
AndroidRuntime: at android.os.Handler.dispatchMessage(Unknown Source:19)
AndroidRuntime: at android.os.Looper.loop(Unknown Source:242)
AndroidRuntime: at android.app.ActivityThread.main(Unknown Source:101)
AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(Unknown Source:11)
AndroidRuntime: at com.android.internal.os.ZygoteInit.main(Unknown Source:275)
AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to read from null array
AndroidRuntime: at com.example.myapplication.MainActivity.prepareData(MainActivity.java:70)
AndroidRuntime: at com.example.myapplication.MainActivity.onCreate(MainActivity.java:35)
AndroidRuntime: at android.app.Activity.performCreate(Unknown Source:19)
AndroidRuntime: at android.app.Activity.performCreate(Unknown Source:1)
AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Unknown Source:3)
AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(Unknown Source:376)
I checked the MainActivity.java line 70 it was a function which did a https network request i did add
android:networkSecurityConfig="@xml/network_security_config
in the manifest along with
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
This is the function which causes nullpointer exception
public static String GET(String url) {
StringBuilder result = new StringBuilder();
HttpURLConnection.setFollowRedirects(true);
try {
URL request_url = new URL(url);
HttpURLConnection http_conn = (HttpURLConnection) request_url.openConnection();
http_conn.setConnectTimeout(100000);
http_conn.setReadTimeout(100000);
http_conn.setInstanceFollowRedirects(true);
http_conn.setRequestMethod("GET");
http_conn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36");
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(http_conn.getInputStream()))) {
for (String line; (line = reader.readLine()) != null; ) {
result.append(line);
}
}
return result.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
this is my xml/network_security_config
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" />
<certificates src="user" />
</trust-anchors>
</base-config>
</network-security-config>
this only happens in some devices when i tested it in emulator it didnt crash
CodePudding user response:
The reason for the crash is in the error log,
AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to read from null array
You are trying to access an array that isn't populated yet and is null, which throws an exception and crashes the app, better handle that exception through try catch to prevent this.
Edit 2:
Base on discussion in comments it seems like you aren't getting data from the site due to not able to connect to site reason been certificates problem , to by pass this completely which I strongly advise against is by disabling the SSL checking,
Code:
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
}
};
// Install the all-trusting trust manager
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (Exception e) {
}
// Now you can access an https URL without having the certificate in the truststore
try {
URL url = new URL("https://hostname/index.html");
} catch (MalformedURLException e) {
}
In bottom most try code you can use your function that you have created to called the site and test it.
Courtesy to @Yishai Answer