0

I am trying to make an app and want to use Firebase authentication to make things easier. I wrote my code, which i will put below, and seem to have no obvious errors, but the app crashes on launch, and will not even let me access the first activity

public class MainActivity extends AppCompatActivity {

//all variables and layout elements

    private static final String TAG = "EmailPassword";

    private FirebaseAuth mAuth;
    Button logIn;
    Button changeElements;
    EditText userName;
    EditText passWord;
    EditText passWordCheck;
    TextView text;
    TextView confirmPassword;

    String username;
    String password;
    String passwordCheck;

    boolean hasAccount=false;


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

        //match variables wwith layout elements

        mAuth = FirebaseAuth.getInstance();

        logIn = (Button) findViewById(R.id.login);
        changeElements = (Button) findViewById(R.id.change_elements);
        userName = (EditText) findViewById(R.id.user);
        passWord = (EditText) findViewById(R.id.password);
        text = (TextView) findViewById(R.id.textView5);
        confirmPassword = (TextView) findViewById(R.id.textView3);


        //change between login and register
        changeElements.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (hasAccount){
                    hasAccount = false;
                    confirmPassword.setVisibility(View.VISIBLE);
                    passWordCheck.setVisibility(View.VISIBLE);

                    logIn.setText("Register");
                    changeElements.setText("Log In");
                    text.setText("Already have an account?");
                }else{
                    hasAccount = true;
                    confirmPassword.setVisibility(View.INVISIBLE);
                    passWordCheck.setVisibility(View.INVISIBLE);

                    logIn.setText("Log In");
                    changeElements.setText("Register");
                    text.setText("Don't have an account? Register here");
                }
            }
        });


        //if log-in or register button is clicked
        logIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                username = userName.toString();
                password = passWord.toString();


                //to login, 
                if (hasAccount){

                    signIn(username, password);

                }else{

                if(password!= null && !password.isEmpty()&& username!= null && !username.isEmpty() && password==passwordCheck){
                    //if password and username fields are not empty and the confirmed password matches, create account
                    userAuth(username,password);
                }else if(password!=passwordCheck){
                    passWordCheck.setError("Passwords do not match");
                }else if(username.isEmpty()||username == null){
                    userName.setError("Please enter username");
                }
                else if(password.isEmpty()||password == null){
                    passWord.setError("Please enter password");
                }


                    }
            }
        });




    }

    @Override
    protected void onStart() {
        super.onStart();
        //Check to see if user is signed in, this is firebase code
        FirebaseUser currentUser = mAuth.getCurrentUser();
        updateUi(currentUser);

    }

    public void updateUi (FirebaseUser user){

        if(user!=null){

            //if user, go to next activity

            Intent intent = new Intent(this, Parties.class);
            startActivity(intent);

        }else{

            //if no user, stay on page, clear fields and display error message


            userName.setError("Something went wrong. Please try again");
            userName.setText("");
            passWord.setText("");
            passWordCheck.setText("");


        }


    }


    //just put the firebase create user code in a method
    public void userAuth(String username, String password){
        mAuth.createUserWithEmailAndPassword(username,password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()){
                    Log.d(TAG, "createUserWithEmailSucces");
                    FirebaseUser user = mAuth.getCurrentUser();
                    updateUi(user);

                }
                else {
                    Log.w(TAG,"createUserWithEmailFailure");
                    Toast.makeText(MainActivity.this, "Authentification failed", Toast.LENGTH_SHORT).show();
                    updateUi(null);
                }
            }
        });

    }


    //same thing here for the sign in

    public void signIn(String username,String password){
        mAuth.signInWithEmailAndPassword(username, password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            // Sign in success, update UI with the signed-in user's information
                            Log.d(TAG, "signInWithEmail:success");
                            FirebaseUser user = mAuth.getCurrentUser();
                            updateUi(user);
                        } else {
                            // If sign in fails, display a message to the user.
                            Log.w(TAG, "signInWithEmail:failure", task.getException());
                            Toast.makeText(MainActivity.this, "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();
                            updateUi(null);
                        }


                    }
                });
    }

}

And here is my logcat

**

06-13 17:45:45.864 2366-2366/? I/art: Not late-enabling -Xcheck:jni (already on)
06-13 17:45:45.864 2366-2366/? W/art: Unexpected CPU variant for X86 using defaults: x86
06-13 17:45:45.970 2366-2366/com.example.eliott.proto_onrop W/System: ClassLoader referenced unknown path: /data/app/com.example.eliott.proto_onrop-2/lib/x86
06-13 17:45:46.037 2366-2366/com.example.eliott.proto_onrop D/FirebaseApp: com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization.
06-13 17:45:46.056 2366-2388/com.example.eliott.proto_onrop W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
06-13 17:45:46.063 2366-2366/com.example.eliott.proto_onrop I/FA: App measurement is starting up, version: 11010
06-13 17:45:46.063 2366-2366/com.example.eliott.proto_onrop I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
06-13 17:45:46.064 2366-2388/com.example.eliott.proto_onrop W/GooglePlayServicesUtil: Google Play services out of date.  Requires 11010000 but found 10298470
06-13 17:45:46.073 2366-2366/com.example.eliott.proto_onrop V/FA: Collection enabled
06-13 17:45:46.073 2366-2366/com.example.eliott.proto_onrop V/FA: App package, google app id: com.example.eliott.proto_onrop, 1:95710228681:android:5771a0cac4b901a2
06-13 17:45:46.074 2366-2366/com.example.eliott.proto_onrop I/FA: To enable faster debug mode event logging run:
                                                                    adb shell setprop debug.firebase.analytics.app com.example.eliott.proto_onrop
06-13 17:45:46.074 2366-2366/com.example.eliott.proto_onrop D/FA: Debug-level message logging enabled
06-13 17:45:46.095 2366-2366/com.example.eliott.proto_onrop V/FA: Cancelling job. JobID: 653780905
06-13 17:45:46.101 2366-2366/com.example.eliott.proto_onrop V/FA: Registered activity lifecycle callback
06-13 17:45:46.102 2366-2366/com.example.eliott.proto_onrop I/FirebaseInitProvider: FirebaseApp initialization successful
06-13 17:45:46.102 2366-2366/com.example.eliott.proto_onrop I/InstantRun: starting instant run server: is main process
06-13 17:45:46.106 2366-2393/com.example.eliott.proto_onrop V/FA: Using measurement service
06-13 17:45:46.107 2366-2393/com.example.eliott.proto_onrop V/FA: Connecting to remote service
06-13 17:45:46.107 2366-2393/com.example.eliott.proto_onrop W/GooglePlayServicesUtil: Google Play services out of date.  Requires 11010000 but found 10298470
06-13 17:45:46.108 2366-2393/com.example.eliott.proto_onrop V/FA: Using measurement service
06-13 17:45:46.108 2366-2393/com.example.eliott.proto_onrop V/FA: Connection attempt already in progress
06-13 17:45:46.184 2366-2366/com.example.eliott.proto_onrop W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
06-13 17:45:46.189 2366-2366/com.example.eliott.proto_onrop V/FA: onActivityCreated
06-13 17:45:46.248 2366-2366/com.example.eliott.proto_onrop D/AndroidRuntime: Shutting down VM


                                                                              --------- beginning of crash
06-13 17:45:46.248 2366-2366/com.example.eliott.proto_onrop E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.example.eliott.proto_onrop, PID: 2366
                                                                              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.eliott.proto_onrop/com.example.eliott.proto_onrop.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                                                                                  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:866)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
                                                                               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
                                                                                  at com.example.eliott.proto_onrop.MainActivity.updateUi(MainActivity.java:151)
                                                                                  at com.example.eliott.proto_onrop.MainActivity.onStart(MainActivity.java:130)
                                                                                  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1248)
                                                                                  at android.app.Activity.performStart(Activity.java:6679)
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2609)
                                                                                  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:866) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

** 

These are my build.gradle files, the project

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
        classpath 'com.google.gms:google-services:3.1.0'


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

And the Module

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"
    defaultConfig {
        applicationId "com.example.eliott.proto_onrop"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    testCompile 'junit:junit:4.12'

    compile 'com.google.firebase:firebase-database:11.0.0'
    compile 'com.google.firebase:firebase-core:11.0.0'
    compile 'com.google.firebase:firebase-auth:11.0.0'


}

apply plugin: 'com.google.gms.google-services'
AL.
  • 36,815
  • 10
  • 142
  • 281
Eliott
  • 3
  • 4

1 Answers1

0

This force Close due to null pointer exception!!!

In MainActivity- check your OnClickListener of Login button

}else if(password!=passwordCheck){

passWordCheck.setError("Passwords do not match");

                }else if(username.isEmpty()||username == null){

Have you referred an id for the EditText passWordCheck in OnCreate ??

Arnold Brown
  • 1,330
  • 13
  • 28