question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

sslSocketFactory NullPointException

See original GitHub issue
OkHttpClient.Builder mBuilder=mBuilder = new OkHttpClient.Builder();
mBuilder.sslSocketFactory(createSSLSocketFactory());
mBuilder.hostnameVerifier(new TrustAllHostnameVerifier());
mBuilder.build();
/**
 * 默认信任所有的证书
 * TODO 最好加上证书认证,主流App都有自己的证书
 *
 * @return
 */
@SuppressLint("TrulyRandom")
private static SSLSocketFactory createSSLSocketFactory() {

    SSLSocketFactory sSLSocketFactory = null;

    try {
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, new TrustManager[]{new TrustAllManager()},
                new SecureRandom());
        sSLSocketFactory = sc.getSocketFactory();
    } catch (Exception e) {
    }

    return sSLSocketFactory;
}

private static class TrustAllManager implements X509TrustManager {
    @Override
    public void checkClientTrusted(X509Certificate[] chain, String authType)
            throws CertificateException {
    }

    @Override
    public void checkServerTrusted(X509Certificate[] chain, String authType)

            throws CertificateException {
    }

    @Override
    public X509Certificate[] getAcceptedIssuers() {
        return null;
    }
}

private static class TrustAllHostnameVerifier implements HostnameVerifier {
    @Override
    public boolean verify(String hostname, SSLSession session) {
        return true;
    }
}

this will cause NullPointException in 3.1.2,but it’s ok in 3.0.1

Caused by: java.lang.NullPointerException: Attempt to get length of null array
    at okhttp3.internal.tls.RealTrustRootIndex.<init>(RealTrustRootIndex.java:31)
    at okhttp3.internal.Platform.trustRootIndex(Platform.java:97)
    at okhttp3.internal.Platform$Android.trustRootIndex(Platform.java:271)
    at okhttp3.OkHttpClient.<init>(OkHttpClient.java:189)
    at okhttp3.OkHttpClient.<init>(OkHttpClient.java:60)
    at okhttp3.OkHttpClient$Builder.build(OkHttpClient.java:718)

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

32reactions
swankjessecommented, Feb 23, 2016

Change your TrustAllManager to do this:


    @Override
    public X509Certificate[] getAcceptedIssuers() {
        return new X509Certificate[0];
    }
0reactions
yangjunjincommented, May 18, 2021

bug bug 崩溃

Read more comments on GitHub >

github_iconTop Results From Across the Web

NullPointerException when creating an SSL-Socket
createSocket(SSLSocketFactory.java:375) W/System.err( 2328): at ... point where the NullPointerException can come from is because sslParameters is null .
Read more >
PM14425: SSLSOCKETFACTORY.CREATESOCKET ... - IBM
NullPointerException at com.ibm.websphere.ssl.protocol.SSLSocketFactory.createSocket(SSL SocketFactory.java:431) FFDC reports the following message ...
Read more >
nullpointer at SSLSocketFactory.createSocket ... - CodeRanch
I am attempting to make a HTTPS POST call from my Websphere application. Locally on my machine the below code works but when...
Read more >
Websphere SSL socket implementation can return null for ...
SSLSocketFactory. Exception: com.mongodb.MongoException: java.lang.NullPointerException. at com.mongodb.connection.
Read more >
1918144 – Null pointer exception on ssl connection
getDefault(SSLSocketFactory.java:123) at java.base/javax.net.ssl.HttpsURLConnection.getDefaultSSLSocketFactory(HttpsURLConnection.java:335) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found