0

I'm not sure why my code is crashing here, I initially thought it was because I had empty edit texts, but after implementing a way to check for that it still crashes. If the information is put into the EditText, they work properly. Any ideas?
This is supposed to be a login page, where if the information is valid goes to the BackgroundWorker class to confirm with the database. I'm quite new to the app developing scene so any help is much appreciated.

public class Login extends AppCompatActivity {
EditText emailText, passText, bookIDText;
public static String email;
AlertDialog alertDialog;

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

    emailText = (EditText) findViewById(R.id.emailTxt);
    passText = (EditText) findViewById(R.id.passTxt);
    bookIDText = (EditText) findViewById(R.id.bookIDTxt);

    Button loginBtn = (Button) findViewById(R.id.LoginBtn);
    TextView welcomeText = (TextView) findViewById(R.id.WelcomeToLibrary);
    TextView signIn = (TextView) findViewById(R.id.SignInBelow);
    TextView clickRegister = (TextView) findViewById(R.id.clickToRegister);

    Typeface openSans = Typeface.createFromAsset(getAssets(), "fonts/OpenSans-CondLight.ttf");

    welcomeText.setTypeface(openSans);
    signIn.setTypeface(openSans);
    clickRegister.setTypeface(openSans);
    loginBtn.setTypeface(openSans);
}

public void onLogin(View view) {
    if (isEmpty(emailText) || isEmpty(passText)) {
        alertDialog.setMessage("Please enter all required fields!");
        alertDialog.show();
    } else {
        email = emailText.getText().toString();
        String password = passText.getText().toString();
        String type = "login";      
        BackgroundWorker backgroundWorker = new BackgroundWorker(this);
        backgroundWorker.execute(type, email, password);
    }
}

public void openRegister(View view) {
    startActivity(new Intent(this, Register.class));
}

private boolean isEmpty(EditText etText) {
    if (etText.getText().toString().trim().length() > 0) {
        return false;
    } else {
        return true;
    }
}
}

Here is the layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/blue_background"
tools:context="com.example.hall.pchsmobilelibrary20.Login">

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="32dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:background="@drawable/white_rounded_corners"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.31">

    <TextView
        android:id="@+id/WelcomeToLibrary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Welcome to the PCHS Mobile Library!"
        android:textAlignment="center"
        android:textColor="@android:color/black"
        android:textSize="24sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/emailTxt"
        android:layout_width="215dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:ems="10"
        android:hint="Student/Staff Email"
        android:inputType="textWebEmailAddress"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.502"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/SignInBelow" />

    <EditText
        android:id="@+id/passTxt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:ems="10"
        android:hint="Password"
        android:inputType="textPassword"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/emailTxt" />

    <TextView
        android:id="@+id/clickToRegister"
        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:clickable="true"
        android:onClick="openRegister"
        android:text="Haven't already registered? Click here!"
        android:textAlignment="center"
        android:textColor="@android:color/black"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/LoginBtn" />

    <Button
        android:id="@+id/LoginBtn"
        android:layout_width="0dp"
        android:layout_height="37dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/blue_rounded_corners"
        android:onClick="onLogin"
        android:text="Login"
        app:layout_constraintEnd_toEndOf="@+id/passTxt"
        app:layout_constraintStart_toStartOf="@+id/passTxt"
        app:layout_constraintTop_toBottomOf="@+id/passTxt" />

    <TextView
        android:id="@+id/SignInBelow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Please Sign In"
        android:textAlignment="center"
        android:textColor="@android:color/black"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/WelcomeToLibrary" />
</android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>

Error Message

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.hall.pchsmobilelibrary20, PID: 5172
              java.lang.IllegalStateException: Could not execute method for android:onClick
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                  at android.view.View.performClick(View.java:6256)
                  at android.view.View$PerformClick.run(View.java:24701)
                  at android.os.Handler.handleCallback(Handler.java:789)
                  at android.os.Handler.dispatchMessage(Handler.java:98)
                  at android.os.Looper.loop(Looper.java:164)
                  at android.app.ActivityThread.main(ActivityThread.java:6541)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
               Caused by: java.lang.reflect.InvocationTargetException
                  at java.lang.reflect.Method.invoke(Native Method)
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                  at android.view.View.performClick(View.java:6256) 
                  at android.view.View$PerformClick.run(View.java:24701) 
                  at android.os.Handler.handleCallback(Handler.java:789) 
                  at android.os.Handler.dispatchMessage(Handler.java:98) 
                  at android.os.Looper.loop(Looper.java:164) 
                  at android.app.ActivityThread.main(ActivityThread.java:6541) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.AlertDialog.setMessage(java.lang.CharSequence)' on a null object reference
                  at com.example.hall.pchsmobilelibrary20.Login.onLogin(Login.java:45)
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                  at android.view.View.performClick(View.java:6256) 
                  at android.view.View$PerformClick.run(View.java:24701) 
                  at android.os.Handler.handleCallback(Handler.java:789) 
                  at android.os.Handler.dispatchMessage(Handler.java:98) 
                  at android.os.Looper.loop(Looper.java:164) 
                  at android.app.ActivityThread.main(ActivityThread.java:6541) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

Application terminated.

Faysal Ahmed
  • 6,464
  • 5
  • 23
  • 43
Carleton Hall
  • 25
  • 1
  • 6

2 Answers2

0

Please add this line in onCreate() Method

alertDialog = new AlertDialog.Builder(this).create();
Saurabh Vadhva
  • 594
  • 4
  • 11
0
bookIDText = (EditText) findViewById(R.id.bookIDTxt);

I think above code causes the error because you don't have any EditText name bookIDTxt in your layout.

Rohit5k2
  • 16,859
  • 8
  • 38
  • 56
MJ_
  • 63
  • 1
  • 1
  • 9