-2

Whenever i run the app, enter the name and press the submit button the app crashes.Here's the code

 public class MainActivity extends AppCompatActivity {
 int x, sum=0;
 String str,str2;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    final Button chk = findViewById(R.id.check);
    final Button sub = findViewById(R.id.submit);
    final EditText name = findViewById(R.id.name);
    chk.setEnabled(false);

    sub.setOnClickListener(
            new View.OnClickListener() {
                public void onClick(View view) {
                    x = str.length();
                    str = name.getText().toString();
                    if (x >= 4 && x <= 10)
                        chk.setEnabled(true);
                }
            });

And here's the xml code for the submit button

     <Button
     android:id="@+id/submit"
     style="@style/Widget.AppCompat.Button.Colored"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_marginBottom="8dp"
     android:layout_marginEnd="8dp"
     android:layout_marginStart="8dp"
     android:layout_marginTop="8dp"
     android:text="@string/submit"
     app:layout_constraintBottom_toBottomOf="parent"
     app:layout_constraintEnd_toEndOf="parent"
     app:layout_constraintStart_toStartOf="parent"
     app:layout_constraintTop_toTopOf="parent" />

I'm a beginner and it's my first app so please excuse me if you find it a naïve question. The following is the logcat message:

12-11 22:55:20.678 4277-4277/com.example.shantanu.namerank D/AndroidRuntime: Shutting down VM

12-11 22:55:20.702 4277-4277/com.example.shantanu.namerank E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.shanu.namerank, PID: 4277
java.lang.NullPointerException: Attempt to invoke virtual method 'int'java.lang.String.length()' on a null object reference

at com.example.shanu.namerank.MainActivity$1.onClick(MainActivity.java:32)

at android.view.View.performClick(View.java:5610)

at android.view.View$PerformClick.run(View.java:22265)

at android.os.Handler.handleCallback(Handler.java:751)

at android.os.Handler.dispatchMessage(Handler.java:95)

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:866)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Shantanu
  • 43
  • 1
  • 4

3 Answers3

0

Looks like str variable is null because you didn't initialize it, but please post your logcat output to confirm it.

Cristian
  • 370
  • 3
  • 8
0
x = str.length();
str = name.getText().toString();

these lines are disordered. Put the first line second and second line first.

Bertram Gilfoyle
  • 9,899
  • 6
  • 42
  • 67
0

The problem is in the onClick() function:

public void onClick(View view) {
    x = str.length();  // length of 'str' calculated first
    str = name.getText().toString();  // 'str' initialized later
    if (x >= 4 && x <= 10)
        chk.setEnabled(true);
}

You are calculating length of str before initializing it.

The code should be:

public void onClick(View view) {
    str = name.getText().toString();
    x = str.length();
    if (x >= 4 && x <= 10)
        chk.setEnabled(true);
}

First, initialize the varible str and then calculate the length of str.

Hope that helps :)

Amrit Singh
  • 143
  • 1
  • 7