-4

Its just a Caesar Cipher, and I found it online.

The app doesn't crash when I run the encrypt method, the app crashes when I start this activity.

My code:

package com.example.brend.securityreach;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class EncryptActivity extends AppCompatActivity {
    EditText thingToEncrypt = (EditText) findViewById(R.id.editText3);
    EditText offsets = (EditText) findViewById(R.id.editText4);
    String offsetting = thingToEncrypt.toString();
    int offset = Integer.valueOf(offsetting);
    EditText encryptedText = (EditText) findViewById(R.id.editText2);

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

    public void encrypt(View view) {
        char[] words = thingToEncrypt.toString().toCharArray();
        for (int i = 0; i < words.length; i++) {
            char letter = words[i];
            letter = (char) (letter + offset);
            if (letter > 'z') {
                letter = (char) (letter - 26);
            } else if (letter < 'a') {
                letter = (char) (letter + 26);
            }
            words[i] = letter;
        }
    }
}

Logcat errors:

06-19 12:49:55.587 3766-3766/com.example.brend.securityreach E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.example.brend.securityreach, PID: 3766
                                                                           java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.brend.securityreach/com.example.brend.securityreach.EncryptActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2548)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                                               at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                               at android.os.Looper.loop(Looper.java:154)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                                                                            Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
                                                                               at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:116)
                                                                               at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:147)
                                                                               at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:27)
                                                                               at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:50)
                                                                               at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:29)
                                                                               at android.support.v7.app.AppCompatDelegateImplN.<init>(AppCompatDelegateImplN.java:29)
                                                                               at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:197)
                                                                               at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:181)
                                                                               at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:521)
                                                                               at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
                                                                               at com.example.brend.securityreach.EncryptActivity.<init>(EncryptActivity.java:9)
                                                                               at java.lang.Class.newInstance(Native Method)
                                                                               at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2538)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                                                                               at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                               at android.os.Looper.loop(Looper.java:154) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

Edit

I figured it out, with try catch and changing things with variables. Thank you to all who invested time and effort into this question and showing me how to declare EditText variables and the like.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

2 Answers2

0

Please follow the Android examples more closely:

public class EncryptActivity extends AppCompatActivity {
    EditText thingToEncrypt = (EditText) findViewById(R.id.editText3); //no! don't do this

Don't use field initialisation with findViewById(int id) as you have done above.

You need to put it in onCreate(Bundle savedInstanceState):

EditText thingToEncrypt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_encrypt);
    thingToEncrypt = (EditText) findViewById(R.id.editText3);
}

Why? Views like EditText do not become available until setContentView(int layoutId) has been called. Please read the official guide to understand Activity lifecycles.

David Rawson
  • 20,912
  • 7
  • 88
  • 124
-1

The scenario might be, you have not declared your activity in AndroidManifest.xml file. kindly check it.

Provide complete details about the error, so we can direct you in correct path.

Logo
  • 1,366
  • 2
  • 11
  • 16