-2

I am currently trying to use TextWatcher for my application.

However, I have found difficulty in removing a certain NullPointerError, happening whenever I launch activity in concern. I've tried to use stacktrace, but I couldn't fix it. Can you please help me?

The below is my current code:

public class TransEncrypt extends AppCompatActivity {

    List<String> Bucket = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trans_encrypt);

        EditText EncryptInput = (EditText) findViewById(R.id.enter_message_field);
        EncryptInput.addTextChangedListener(new TextWatcher() {
            TextView EncryptOutput = (TextView) findViewById(R.id.encrypted_output);
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                EncryptOutput.setText(R.string.default_encrypt);
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

    }

}

And this is my stack trace:

08-22 23:47:16.434 29280-29280/com.example.denny.scrambler E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.denny.scrambler/com.example.denny.scrambler.TransEncrypt}: java.lang.NullPointerException
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2219)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
                                                                                 at android.app.ActivityThread.access$700(ActivityThread.java:159)
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                 at android.os.Looper.loop(Looper.java:137)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5419)
                                                                                 at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                 at java.lang.reflect.Method.invoke(Method.java:525)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
                                                                                 at dalvik.system.NativeStart.main(Native Method)
                                                                              Caused by: java.lang.NullPointerException
                                                                                 at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:72)
                                                                                 at android.support.v7.app.AppCompatDelegateImplV7.<init>(AppCompatDelegateImplV7.java:146)
                                                                                 at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:28)
                                                                                 at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:41)
                                                                                 at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:193)
                                                                                 at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:173)
                                                                                 at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:511)
                                                                                 at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:183)
                                                                                 at com.example.denny.scrambler.TransEncrypt$1.<init>(TransEncrypt.java:28)
                                                                                 at com.example.denny.scrambler.TransEncrypt.<init>(TransEncrypt.java:27)
                                                                                 at java.lang.Class.newInstanceImpl(Native Method)
                                                                                 at java.lang.Class.newInstance(Class.java:1130)
                                                                                 at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2210)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349) 
                                                                                 at android.app.ActivityThread.access$700(ActivityThread.java:159) 
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316) 
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                                 at android.os.Looper.loop(Looper.java:137) 
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5419) 
                                                                                 at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                 at java.lang.reflect.Method.invoke(Method.java:525) 
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187) 
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) 
                                                                                 at dalvik.system.NativeStart.main(Native Method) 
Phantômaxx
  • 36,442
  • 21
  • 78
  • 108

1 Answers1

0

Your error occurs inside the anonymous class because the Context though which you call findViewById does not exist there.

at com.example.denny.scrambler.TransEncrypt$1.<init>(TransEncrypt.java:28)

Try to move around these lines

EditText EncryptInput = (EditText) findViewById(R.id.enter_message_field);
EncryptInput.addTextChangedListener(new TextWatcher() {
    TextView EncryptOutput = (TextView) findViewById(R.id.encrypted_output);

Like this

EditText EncryptInput = (EditText) findViewById(R.id.enter_message_field);
final TextView EncryptOutput = (TextView) findViewById(R.id.encrypted_output);
EncryptInput.addTextChangedListener(new TextWatcher() {

Or, even better, make those Views as fields of the class.

OneCricketeer
  • 126,858
  • 14
  • 92
  • 185