Facing Null Pointer Exception while attempt to invoke FingerPrint authenticate method by passing Generated cryptoObject

  • Replies:6
  • OpenNot stickiedUnanswered
  • Forum posts: 4

Jul 13, 2016 11:40:23 AM via Website

I am trying to use the native Finger Print Scanning in my google nexus 5x. I am new to this development. I do not have any idea towards this issue. can I have some Guidance here?. My Design consist of dynamic fragment which ask for the user to touch the sensor. My development IDE is android eclipse. Below is the detail of the error.

07-12 15:04:50.419: I/Process(5519): Sending signal. PID: 5519 SIG: 9
07-12 15:14:55.328: I/art(6119): Ignoring second debugger -- accepting and dropping
07-12 15:14:55.355: W/System(6119): ClassLoader referenced unknown path: /data/app/com.example.fingerprintnew-1/lib/arm64
07-12 15:14:55.550: D/OpenGLRenderer(6119): Use EGL_SWAP_BEHAVIOR_PRESERVED: true
07-12 15:14:55.597: I/Adreno(6119): QUALCOMM build : 63c06b2, I8366cd0437
07-12 15:14:55.597: I/Adreno(6119): Build Date : 12/06/15
07-12 15:14:55.597: I/Adreno(6119): OpenGL ES Shader Compiler Version: XE031.05.13.02
07-12 15:14:55.597: I/Adreno(6119): Local Branch : mybranch17112971
07-12 15:14:55.597: I/Adreno(6119): Remote Branch : quic/LA.BF64.1.2.9_v2
07-12 15:14:55.597: I/Adreno(6119): Remote Branch : NONE
07-12 15:14:55.597: I/Adreno(6119): Reconstruct Branch : NOTHING
07-12 15:14:55.603: I/OpenGLRenderer(6119): Initialized EGL, version 1.4
07-12 15:14:56.929: D/AndroidRuntime(6119): Shutting down VM
07-12 15:14:56.933: E/AndroidRuntime(6119): FATAL EXCEPTION: main
07-12 15:14:56.933: E/AndroidRuntime(6119): Process: com.example.fingerprintnew, PID: 6119
07-12 15:14:56.933: E/AndroidRuntime(6119): java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.fingerprintnew.MainActivity.onAuthentication()' on a null object reference
07-12 15:14:56.933: E/AndroidRuntime(6119): at com.example.fingerprintnew.FingerPrintHandler.onAuthenticationSucceeded(FingerPrintHandler.java:63)
07-12 15:14:56.933: E/AndroidRuntime(6119): at android.hardware.fingerprint.FingerprintManager$MyHandler.sendAuthenticatedSucceeded(FingerprintManager.java:805)
07-12 15:14:56.933: E/AndroidRuntime(6119): at android.hardware.fingerprint.FingerprintManager$MyHandler.handleMessage(FingerprintManager.java:757)
07-12 15:14:56.933: E/AndroidRuntime(6119): at android.os.Handler.dispatchMessage(Handler.java:102)
07-12 15:14:56.933: E/AndroidRuntime(6119): at android.os.Looper.loop(Looper.java:148)
07-12 15:14:56.933: E/AndroidRuntime(6119): at android.app.ActivityThread.main(ActivityThread.java:5422)
07-12 15:14:56.933: E/AndroidRuntime(6119): at java.lang.reflect.Method.invoke(Native Method)
07-12 15:14:56.933: E/AndroidRuntime(6119): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
07-12 15:14:56.933: E/AndroidRuntime(6119): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Expectation :
The very first screen is having one button when the button is tapped, the dialog should appear which should be created dynamically and should sense the fingerprint

Below is the piece of code where i am getting the error.
package com.example.fingerprintscanning;

import com.example.fingerprintscanning.MainActivity;

import com.example.fingerprintscanning.FingerprintUiHelper;

import android.app.Activity;
import android.app.DialogFragment;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.hardware.fingerprint.FingerprintManager;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class FingerprintAuthenticationDialogFragment extends DialogFragment implements TextView.OnEditorActionListener {

private MainActivity mActivity;
private FingerprintManager mfingerprintManager;
private FingerprintUiHelper mFingerprintUiHelper;
private FingerprintManager.CryptoObject mCryptoObject;
private Color mColor;
private Stage mStage = Stage.FINGERPRINT;

RelativeLayout.LayoutParams param1,

public FingerprintAuthenticationDialogFragment(Context context) {
    this.mActivity = (MainActivity)context;
public void onCreate(Bundle savedInstanceState) {
    // Do not create a new Fragment when the Activity is re-created such as orientation changes.
    setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Material_Light_Dialog);
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    getDialog().setTitle("Sign In");
    int img = getResources().getIdentifier("ic_fp_40px", "drawable", this.mActivity.getPackageName());
    FrameLayout framelayout = new FrameLayout(this.mActivity);
    framelayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
    RelativeLayout relativeLayout = new RelativeLayout(this.mActivity);
    param1 = new RelativeLayout.LayoutParams((int)RelativeLayout.LayoutParams.WRAP_CONTENT,
    param2 = new RelativeLayout.LayoutParams((int)RelativeLayout.LayoutParams.WRAP_CONTENT,
    param3 = new RelativeLayout.LayoutParams((int)RelativeLayout.LayoutParams.WRAP_CONTENT,
    param4 = new RelativeLayout.LayoutParams((int)RelativeLayout.LayoutParams.WRAP_CONTENT,
    param1.setMargins(100, 50, 0, 0);
    param1.height = 500;
    param2.setMargins(100, 200, 0, 0);
    param3.setMargins(300, 200, 0, 0);
    param4.setMargins(450, 350, 0, 0);

    TextView textView = new TextView(this.mActivity);
    textView.setText("Confirm fingerprint to continue");
    textView.setPadding(0, 0, 0,0);

    ImageView image = new ImageView(this.mActivity);

    TextView textView1 = new TextView(this.mActivity);
    textView1.setText("Touch sensor");
    textView1.setPadding(0, 50, 0,0);

    Button myButton = new Button(this.mActivity);
    myButton.setPadding(0, 0, 0,0);
    myButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {



    return framelayout;
public void onResume() {
    if(mCryptoObject.toString() == null){
        System.out.println("inside if");

 public void setStage(Stage stage) {
        mStage = stage;

public void onPause() {
public void onAttach(Activity activity) {
    mActivity = (MainActivity) activity;

 * Sets the crypto object to be passed in when authenticating with fingerprint.
public void setCryptoObject(FingerprintManager.CryptoObject cryptoObject) {
    mCryptoObject = cryptoObject;

public void onAuthenticated() {
    // Callback from FingerprintUiHelper. Let the activity know that authentication was
    // successful.
    mActivity.onPurchased(true /* withFingerprint */);
 @ Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    // TODO Auto-generated method stub
    return false;
 public enum Stage {

when the control comes to this line mFingerprintUiHelper.startListening(mCryptoObject); it throws the null pointer exception.

  • Forum posts: 251

Jul 13, 2016 5:19:31 PM via Website

when the control comes to this line mFingerprintUiHelper.startListening(mCryptoObject); it throws the null pointer exception.

If the error occurs at mFingerprintUiHelper.startListening(mCryptoObject), has anything been assigned to mCryptoObject? If not, assign a value to that object.

Also, look at MainActivity.onAuthentication() and make sure those objects has a value.

  • Forum posts: 4

Jul 14, 2016 9:28:22 AM via Website

Thanks for your Quick reply. I have given my Main Activity file below. In that I have created a cryptoObject which has some value and passing that to FingerprintAuthenticationDialogFragment class (previous thread has this code). I do not know where it is going wrong, giving me the above mentioned error. Please provide me your assistance.

package com.example.fingerprintscanning;

import java.io.IOException;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import android.app.Activity;
import android.app.KeyguardManager;
import android.content.SharedPreferences;
import android.hardware.fingerprint.FingerprintManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.security.keystore.KeyGenParameterSpec;
import android.security.keystore.KeyPermanentlyInvalidatedException;
import android.security.keystore.KeyProperties;
import android.util.Base64;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Transformation;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

private static final String TAG = MainActivity.class.getSimpleName();
KeyguardManager mKeyguardManager;
FingerprintManager mFingerprintManager;
KeyStore mKeyStore;
KeyGenerator mKeyGenerator;
Cipher mCipher;
SharedPreferences mSharedPreferences;
private FingerprintManager.CryptoObject cryptoObject;
private MainActivity mActivity;

FingerprintAuthenticationDialogFragment mFragment  = new FingerprintAuthenticationDialogFragment(this);
private static final String KEY_NAME = "my_key";
private static final String DIALOG_FRAGMENT_TAG = "myFragment";
private static final String SECRET_MESSAGE = "Very secret message";

protected void onCreate(Bundle savedInstanceState) {
    Button btnSample = (Button) findViewById(R.id.btnSample);
    mKeyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
    mFingerprintManager = (FingerprintManager) getSystemService(FINGERPRINT_SERVICE);
    if (!mKeyguardManager.isKeyguardSecure()) {
        // Show a message that the user hasn't set up a fingerprint or lock screen.
                "Secure lock screen hasn't set up.\n"
                        + "Go to 'Settings -> Security -> Fingerprint' to set up a fingerprint",

    if (!mFingerprintManager.hasEnrolledFingerprints()) {
        // This happens when no fingerprints are registered.
                "Go to 'Settings -> Security -> Fingerprint' and register at least one fingerprint",
    btnSample.setOnClickListener(new View.OnClickListener() {
         @ Override
        public void onClick(View v) {
             if (initCipher()) {
                mFragment.setCryptoObject(new FingerprintManager.CryptoObject(mCipher));
                mFragment.show(getFragmentManager(), DIALOG_FRAGMENT_TAG);
public boolean initCipher() {

    try {
        mCipher = Cipher.getInstance(
                  KeyProperties.KEY_ALGORITHM_AES + "/"
                + KeyProperties.BLOCK_MODE_CBC + "/"
                + KeyProperties.ENCRYPTION_PADDING_PKCS7);
    } catch (NoSuchAlgorithmException | 
            NoSuchPaddingException e) {
        throw new RuntimeException("Failed to get Cipher", e);

    try {
        SecretKey key = (SecretKey) mKeyStore.getKey(KEY_NAME,
        mCipher.init(Cipher.ENCRYPT_MODE, key);
        return true;
    } catch (KeyPermanentlyInvalidatedException e) {
        return false;
    } catch (KeyStoreException | CertificateException 
        | UnrecoverableKeyException | IOException
            | NoSuchAlgorithmException | InvalidKeyException e) {
        throw new RuntimeException("Failed to init Cipher", e);
public void onPurchased(boolean withFingerprint) {
    if (withFingerprint) {
        // If the user has authenticated with fingerprint, verify that using cryptography and
        // then show the confirmation message.
    } else {
        // Authentication happened with backup password. Just show the confirmation message.
 * Tries to encrypt some data with the generated key in {@link #createKey} which is
 * only works if the user has just authenticated via fingerprint.
private void tryEncrypt() {
    try {
        byte[] encrypted = mCipher.doFinal(SECRET_MESSAGE.getBytes());
    } catch (BadPaddingException | IllegalBlockSizeException e) {
        Toast.makeText(this, "Failed to encrypt the data with the generated key. "
                + "Retry the purchase", Toast.LENGTH_LONG).show();
        Log.e(TAG, "Failed to encrypt the data with the generated key." + e.getMessage());
private void showConfirmation(byte[] encrypted) {

// findViewById(R.id.confirmation_message).setVisibility(View.VISIBLE);
// if (encrypted != null) {
// TextView v = (TextView) findViewById(R.id.encrypted_message);
// v.setVisibility(View.VISIBLE);
// v.setText(Base64.encodeToString(encrypted, 0 /* flags */));
// }
protected void createKey() {
try {
mKeyStore = KeyStore.getInstance("AndroidKeyStore");
} catch (Exception e) {

    try {
        mKeyGenerator = KeyGenerator.getInstance(
    } catch (NoSuchAlgorithmException | 
                    NoSuchProviderException e) {
        throw new RuntimeException(
        "Failed to get KeyGenerator instance", e);
    try {
        mKeyGenerator.init(new KeyGenParameterSpec.Builder(KEY_NAME,
                KeyProperties.PURPOSE_ENCRYPT |
    } catch (InvalidAlgorithmParameterException e) {
        // TODO Auto-generated catch block
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
    } catch (CertificateException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;

public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    return super.onOptionsItemSelected(item);