Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
hexonxons opened this issue Feb 23, 2016 · 6 comments
Closed

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

hexonxons opened this issue Feb 23, 2016 · 6 comments

Comments

@hexonxons
Copy link

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+

@masztalski
Copy link

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

@hexonxons
Copy link
Author

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

@masztalski
Copy link

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

@ralscha
Copy link

ralscha commented Mar 1, 2016

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. 

@hexonxons
Copy link
Author

@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.

@swankjesse
Copy link
Member

Yep, that’s a good point. You should submit a pull request!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants