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.

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

See original GitHub issue

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+

Issue Analytics

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

github_iconTop GitHub Comments

15reactions
masztalskicommented, Feb 23, 2016

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

1reaction
ralschacommented, 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. 
Read more comments on GitHub >

github_iconTop Results From Across the Web

android - Attempt to get length of null array (readDirectory())
But why do I have a readDirectory fail when the path is good? package com.example.androidexplorer; import java.io.File; import java.util.
Read more >
java.lang.NullPointerException: Attempt to get length of null ...
NullPointerException : Attempt to get length of null array at java.util.Formatter$FormatSpecifier.addZeros(Formatter.java:3505) at java.util.
Read more >
Attempt to get length of null array at org.mozilla.gecko ...
Startup Crash in java.lang.NullPointerException: Attempt to get length of null array at org.mozilla.gecko.GeckoApp$DeferredCleanupTask.run(GeckoApp.java).
Read more >
Internal NullPointerException: Attempt to read from null array ...
Fatal Exception: java.lang.NullPointerException: Attempt to read from null array at java.util.ArrayList.get(ArrayList.java:310)
Read more >
I am getting error "Attempt to get length of null array" - Treehouse
Probably there is a problem with parceling(marshalling and unmarshallng) the data. So I think the 'parcebles' array that you declared is empty ...
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