Okhttp: java.lang.NullPointerException: Attempt to get length of null array

Created on 23 Feb 2016  路  6Comments  路  Source: square/okhttp

This code produces NPE:

static {
    // Init okhttp.
    OkHttpClient.Builder builder = new OkHttpClient.Builder();

    TrustManager[] trustAllCerts = { new X509TrustManager() {
        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType)
                throws CertificateException {
            // Not implemented
        }

        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType)
                throws CertificateException {
            // Not implemented
        }
    }};

    try {
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        builder.sslSocketFactory(sc.getSocketFactory());
    } catch (KeyManagementException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }

    HTTP_CLIENT = builder.build();
}

Stacktrace:

java.lang.ExceptionInInitializerError
  at ru.xseconds.xseconds.app.activity.WelcomeActivity.onResume(WelcomeActivity.java:46)
  ...
Caused by: java.lang.NullPointerException: Attempt to get length of null array
  at okhttp3.internal.tls.CertificateAuthorityCouncil.<init>(CertificateAuthorityCouncil.java:44)
  at okhttp3.OkHttpClient.<init>(OkHttpClient.java:190)
  at okhttp3.OkHttpClient.<init>(OkHttpClient.java:60)
  at okhttp3.OkHttpClient$Builder.build(OkHttpClient.java:719)
  at ru.xseconds.xseconds.net.API.<clinit>(API.java:88)
  ....

There was no error in 3.0.1 version, started to crash in 3.1+

Most helpful comment

Hey, I had also this error. I managed to fixed it by changing 'return null;' for 'return new X509Certificate[0];' in getAcceptedIssuers()

All 6 comments

Hey, I had also this error. I managed to fixed it by changing 'return null;' for 'return new X509Certificate[0];' in getAcceptedIssuers()

@masztalski Thanks, but it is still regression or there should be more detailed message about this case.

@hexonxons indeed, I have spent some time searching through forums to find out about solution.

I had the same problem. I think this is not an OkHttp issue.
When you read the javadoc for getAcceptedIssuers the method must return a non null value.

Return an array of certificate authority certificates which are trusted for authenticating peers.
Returns: 
a non-null (possibly empty) array of acceptable CA issuer certificates. 

@ralscha Maybe, but there is another doc in android-23:

/**
 * Returns the list of certificate issuer authorities which are trusted for
 * authentication of peers.
 *
 * @return the list of certificate issuer authorities which are trusted for
 *         authentication of peers.
 */

nothing about null value. And there should be more detailed exception message still, not just NPE inside the framework.

Yep, that鈥檚 a good point. You should submit a pull request!

Was this page helpful?
0 / 5 - 0 ratings