Catholic Readings
Catholic Readings

Reputation: 55

Attempt to read from null array android

I have been trying to implement a passcodeView library in my project for easier login instead of the default login feature. I have followed all the steps but I keep getting an error I have been unable to resolve. Please I need solutions.

The array keeps returning null and crashing my app but it is not null.

link

public class QuickLoginActivity extends AppCompatActivity {
    private static final String ARG_CURRENT_PIN = "current_pin";
    private PinView mPinView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quick_login);

        //Set the correct pin code.
        //REQUIRED
        mPinView = findViewById(R.id.pattern_view);
        final int[] correctPattern = new int[]{1, 2, 3, 4};
        mPinView.setPinAuthenticator(new PasscodeViewPinAuthenticator(correctPattern));


        //Build the desired key shape and pass the theme parameters.
        //REQUIRED
        mPinView.setKey(new RoundKey.Builder(mPinView)
                .setKeyPadding(R.dimen.key_padding)
                .setKeyStrokeColorResource(R.color.colorAccent)
                .setKeyStrokeWidth(R.dimen.key_stroke_width)
                .setKeyTextColorResource(R.color.colorAccent)
                .setKeyTextSize(R.dimen.key_text_size));

        //Build the desired indicator shape and pass the theme attributes.
        //REQUIRED
        mPinView.setIndicator(new CircleIndicator.Builder(mPinView)
                .setIndicatorRadius(R.dimen.indicator_radius)
                .setIndicatorFilledColorResource(R.color.colorAccent)
                .setIndicatorStrokeColorResource(R.color.colorAccent)
                .setIndicatorStrokeWidth(R.dimen.indicator_stroke_width));

        mPinView.setPinLength(4);
        mPinView.setTitle("Enter the PIN");

        mPinView.setAuthenticationListener(new AuthenticationListener() {
            @Override
            public void onAuthenticationSuccessful() {
                //User authenticated successfully.
                //Navigate to secure screens.
                startActivity(new Intent(QuickLoginActivity.this, MainActivity.class));
                finish();
            }

            @Override
            public void onAuthenticationFailed() {
                //Calls whenever authentication is failed or user is unauthorized.
                //Do something
            }
        });

    }

    private void sendToLogin() {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putIntArray(ARG_CURRENT_PIN, mPinView.getCurrentTypedPin());
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        mPinView.setCurrentTypedPin(savedInstanceState.getIntArray(ARG_CURRENT_PIN));
    }
}

StackTrace

E/UncaughtException: java.lang.NullPointerException: Attempt to read from null array
        at com.kevalpatel.passcodeview.internal.BoxKeypad.measureView(BoxKeypad.java:181)
        at com.kevalpatel.passcodeview.PinView.measureView(PinView.java:182)
        at com.kevalpatel.passcodeview.internal.BasePasscodeView.onMeasure(BasePasscodeView.java:212)
        at android.view.View.measure(View.java:20665)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:825)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:511)
        at android.view.View.measure(View.java:20665)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6320)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:214)
        at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLay

Upvotes: 1

Views: 538

Answers (1)

Andrew Churilo
Andrew Churilo

Reputation: 2309

It happens because you don't set key names. You should add:

mPinView.setKeyNames(new KeyNamesBuilder());

Upvotes: 1

Related Questions