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

  • Replies:6
Nivetha Viswanathan
  • 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,
param2,param3,param4;

public FingerprintAuthenticationDialogFragment(Context context) {
    this.mActivity = (MainActivity)context;
}
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Do not create a new Fragment when the Activity is re-created such as orientation changes.
    setRetainInstance(true);
    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,
            LayoutParams.MATCH_PARENT));
    RelativeLayout relativeLayout = new RelativeLayout(this.mActivity);
    param1 = new RelativeLayout.LayoutParams((int)RelativeLayout.LayoutParams.WRAP_CONTENT,
            (int)RelativeLayout.LayoutParams.WRAP_CONTENT);
    param2 = new RelativeLayout.LayoutParams((int)RelativeLayout.LayoutParams.WRAP_CONTENT,
            (int)RelativeLayout.LayoutParams.WRAP_CONTENT);
    param3 = new RelativeLayout.LayoutParams((int)RelativeLayout.LayoutParams.WRAP_CONTENT,
            (int)RelativeLayout.LayoutParams.WRAP_CONTENT);
    param4 = new RelativeLayout.LayoutParams((int)RelativeLayout.LayoutParams.WRAP_CONTENT,
            (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.setTextSize(17);
    textView.setTextColor(mColor.GRAY);
    textView.setPadding(0, 0, 0,0);
    textView.setLayoutParams(param1);

    ImageView image = new ImageView(this.mActivity);
    image.setLayoutParams(param2);
    image.setImageResource(img);

    TextView textView1 = new TextView(this.mActivity);
    textView1.setText("Touch sensor");
    textView1.setTextSize(13);
    textView1.setTextColor(mColor.GRAY);
    textView1.setPadding(0, 50, 0,0);
    textView1.setLayoutParams(param3);

    Button myButton = new Button(this.mActivity);
    myButton.setText("Cancel");
    myButton.setPadding(0, 0, 0,0);
    myButton.setLayoutParams(param4);
    myButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            getDialog().dismiss();
        }
    });

    relativeLayout.addView(textView);
    relativeLayout.addView(image);
    relativeLayout.addView(textView1);
    relativeLayout.addView(myButton);

    framelayout.addView(relativeLayout);

    return framelayout;
}
@Override
public void onResume() {
    super.onResume();
    if(mCryptoObject.toString() == null){
        System.out.println("inside if");
        return;
    }else{
        mFingerprintUiHelper.startListening(mCryptoObject);
    }

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

@Override
public void onPause() {
    super.onPause();
    mFingerprintUiHelper.stopListening();
}
@Override
public void onAttach(Activity activity) {
    super.onAttach(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 */);
    dismiss();
}
 @ Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    // TODO Auto-generated method stub
    return false;
}
 public enum Stage {
        FINGERPRINT,
        NEW_FINGERPRINT_ENROLLED,
        PASSWORD
    }

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

Reply
pentel
  • 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.

Reply
Nivetha Viswanathan
  • 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";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    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.
        Toast.makeText(this,
                "Secure lock screen hasn't set up.\n"
                        + "Go to 'Settings -> Security -> Fingerprint' to set up a fingerprint",
                Toast.LENGTH_LONG).show();
        btnSample.setEnabled(false);
        return;

    }
    if (!mFingerprintManager.hasEnrolledFingerprints()) {
        btnSample.setEnabled(false);
        // This happens when no fingerprints are registered.
        Toast.makeText(this,
                "Go to 'Settings -> Security -> Fingerprint' and register at least one fingerprint",
                Toast.LENGTH_LONG).show();
        return;
    }
    createKey();
    btnSample.setEnabled(true);
    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 {
        mKeyStore.load(null);
        SecretKey key = (SecretKey) mKeyStore.getKey(KEY_NAME,
                         null);
        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.
        tryEncrypt();
    } else {
       System.out.println("error++++++++++++++");
        // Authentication happened with backup password. Just show the confirmation message.
        //showConfirmation(null);
    }
}
 /**
 * 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());
        showConfirmation(encrypted);
    } 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) {
e.printStackTrace();
}

    try {
        mKeyGenerator = KeyGenerator.getInstance(
                          KeyProperties.KEY_ALGORITHM_AES, 
                          "AndroidKeyStore");
    } catch (NoSuchAlgorithmException | 
                    NoSuchProviderException e) {
        throw new RuntimeException(
        "Failed to get KeyGenerator instance", e);
    }
    try {
        mKeyStore.load(null);
        mKeyGenerator.init(new KeyGenParameterSpec.Builder(KEY_NAME,
                KeyProperties.PURPOSE_ENCRYPT |
                KeyProperties.PURPOSE_DECRYPT)
                .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
                .setUserAuthenticationRequired(true)
                .setEncryptionPaddings(
                     KeyProperties.ENCRYPTION_PADDING_PKCS7)
                        .build());
        mKeyGenerator.generateKey();
    } catch (InvalidAlgorithmParameterException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (CertificateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
@Override
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;
}

@Override
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);
}

}

Reply
Nivetha Viswanathan
  • Forum posts: 4

Jul 15, 2016, 8:39:36 AM via Website

Any update on this?

Reply
Marcos Garcia Duran
  • Forum posts: 29

Jul 15, 2016, 12:21:53 PM via Website

Update new version ?

Reply
Nivetha Viswanathan
  • Forum posts: 4

Jul 15, 2016, 12:41:50 PM via Website

Any update on the above thread? Any solution?

Reply
Marcos Garcia Duran
  • Forum posts: 29

Jul 25, 2016, 10:10:35 PM via Website

acces to root ?

Reply