Android: NullPointerException error

2019-08-11 20:58发布

问题:

I know it is dull to ask answers for a NullPointerException, and there are similar questions out there. However, I just cannot find a solution for my problem from the other questions.

I have 2 classes:

  1. CreateContactActivityl.java:

Passes intent of a text output to RegexOCR1.java

  1. RegexOCR1.java:

Receives the text output, pass text output into a method that is within this class

The error occurs in RegexOCR1.java as stated by the logcat:

Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.l33902.contactmanagment1512/com.example.l33902.contactmanagment.RegexOCR1}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3155)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3263)
       at android.app.ActivityThread.access$1000(ActivityThread.java:197)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1687)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:145)
       at android.app.ActivityThread.main(ActivityThread.java:6897)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
       at com.example.l33902.contactmanagment.RegexOCR1.onCreate(RegexOCR1.java:32)
       at android.app.Activity.performCreate(Activity.java:6550)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3108)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3263)
       at android.app.ActivityThread.access$1000(ActivityThread.java:197)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1687)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:145)
       at android.app.ActivityThread.main(ActivityThread.java:6897)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

I have also implemented Crashlytics for debugging as shown in the link below:

Crashlytics Results

In CreateContactActivityOCR.java:

Below are the codes that passes the intent of text output to RegexOCR1.java

private void passText(){
        //Log.i(TAG, "PassText");
        Intent intent = new Intent(this, RegexOCR1.class);
        startActivity(intent);
    }

In RegexOCR1.java:

Below are the codes that I used to receives text output and then passes in the method EmailValidator():

public class RegexOCR1 extends Activity {

    private Pattern pattern;
    private Matcher matcher;

    private String recognizedText, textToUse;
    private String mFromLang, mCurrentLang;

    private static final String EMAIL_PATTERN =
            "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
                    + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

    private static final String PHONE_PATTERN =
            "^[89]\\d{7}$";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        // Getting the path of the image from another class
        Bundle extras = this.getIntent().getExtras();
        recognizedText = extras.getString("TEXT");
        textToUse = recognizedText;

        // Getting the language used for text recognition
        mFromLang = extras.getString("LANG");
        mCurrentLang = mFromLang;
        //Log.i(TAG, mFromLang);

        EmailValidator();
    }


    public String EmailValidator() {

        Pattern pattern = Pattern.compile(EMAIL_PATTERN);
        Matcher matcher = pattern.matcher(textToUse);
        if (matcher.find()) {
            String email = textToUse.substring(matcher.start(), matcher.end());

        } else {
            // TODO handle condition when input doesn't have an email address
        }

        return textToUse;
    }

    public boolean validate(final String hex) {

        matcher = pattern.matcher(hex);
        return matcher.matches();

    }

    private void showText(){
        //Log.i(TAG, "ShowText");
        Intent intent = new Intent(this, CreateContactActivityOCR.class);
        startActivity(intent);
    }
} 

I can't figure at which process within RegexOCR1.java does the error occurs.

According to Crashlytics, it is line 32: recognizedText = extras.getString("TEXT");

However, I have used this line in CreateContactActivityOCR.java to collect the text output from another class with no error.

回答1:

private void passText(){
    //Log.i(TAG, "PassText");
    Intent intent = new Intent(this, RegexOCR1.class);
    intent.putExtra("TEXT", "your text here");
    startActivity(intent);
}


回答2:

Try following Code and And run again

    private void passText(){
    //Log.i(TAG, "PassText");
    Intent intent = new Intent(this, RegexOCR1.class);
    intent.putExtra("TEXT","YOUR STRING DATA");
    intent.putExtra("LANG","YOUR STRING DATA");
    startActivity(intent);
}