0

I have a view (activity_SingIn) and its Presenter now in the onCreate method gives me the following error related to "singInPresenter.singInUser(v);" 2022-09-18 17:30:51.785 3811-3811/com.example.Natour21 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.Natour21, PID: 3811 java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.NaTour21.Presenter.Activity.SingInPresenter.singInUser(android.view.View)' on a null object reference at com.example.NaTour21.View.Activity.activity_singIn$2.onClick(activity_singIn.java:61)

package com.example.NaTour21.Presenter.Activity;


import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;


import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.NaTour21.View.Activity.activity_Home;
import com.google.android.gms.tasks.Task;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.material.snackbar.Snackbar;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

import android.widget.ProgressBar;


public class SingInPresenter extends AppCompatActivity {

    private EditText EmailLgn,PasswordLgn;
    String[] messaggio = {"Campi vuoti inserili"};
    private ProgressBar progressBar;


    public void singInUser(View view) {

        String email = EmailLgn.getText().toString();
        String password = PasswordLgn.getText().toString();

        FirebaseAuth.getInstance().createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {
                    progressBar.setVisibility(View.VISIBLE);

                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            home();
                        }
                    }, 3000);
                } else {
                    String error;
                    try{
                        throw task.getException();
                    }catch(Exception e){
                        error ="Errore di accesso utente";
                    }
                    Snackbar snackbar = Snackbar.make(view,error,Snackbar.LENGTH_SHORT);
                    snackbar.setBackgroundTint(Color.WHITE);
                    snackbar.setTextColor(Color.BLACK);
                    snackbar.show();
                }
            }

        });
    }

    public void home() {
        Intent intent = new Intent(this, activity_Home.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void onStart() {
        super.onStart();
        FirebaseUser UtenteCorrente = FirebaseAuth.getInstance().getCurrentUser();
        if(UtenteCorrente != null){
            home();
        }
    }
}

package com.example.NaTour21.View.Activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import com.example.NaTour21.Presenter.Activity.SingInPresenter;
import com.example.NaTour21.R;
import com.google.android.material.snackbar.Snackbar;

import android.view.View;
import android.widget.ProgressBar;

public class activity_singIn extends AppCompatActivity{

    private EditText EmailLgn,PasswordLgn;
    private Button loginLgn;
    private Button registratiLgn;
    private SingInPresenter singInPresenter;
    private ProgressBar progressbar;
    String[] messaggio = {"Campi vuoti inserirli",
                          "Accesso effettuato con successo",
                          "Formato email non valido",
                           "La password deve avere minimo 6 caratteri"};



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

        getSupportActionBar().hide();
        initializeComponent();

        registratiLgn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

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

            }
        });

        loginLgn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email = EmailLgn.getText().toString();
                String password = PasswordLgn.getText().toString();

                if (email.isEmpty() || password.isEmpty()) {
                    Snackbar snackbar = Snackbar.make(v, messaggio[0], Snackbar.LENGTH_SHORT);
                    snackbar.setBackgroundTint(Color.WHITE);
                    snackbar.setTextColor(Color.BLACK);
                    snackbar.show();
                } else if (!isValidEmail(email)) {
                    Snackbar snackbar = Snackbar.make(v, messaggio[2], Snackbar.LENGTH_SHORT);
                    snackbar.setBackgroundTint(Color.WHITE);
                    snackbar.setTextColor(Color.BLACK);
                    snackbar.show();
                }else if (!(password.length() < 6)) {
                    Snackbar snackbar = Snackbar.make(v, messaggio[3], Snackbar.LENGTH_SHORT);
                    snackbar.setBackgroundTint(Color.WHITE);
                    snackbar.setTextColor(Color.BLACK);
                    snackbar.show();
                }else{
                    Snackbar snackbar = Snackbar.make(v, messaggio[1], Snackbar.LENGTH_SHORT);
                    snackbar.setBackgroundTint(Color.WHITE);
                    snackbar.setTextColor(Color.BLACK);
                    snackbar.show();
                    singInPresenter.singInUser(v); //??????

                }
            }
       });
    }

        private boolean isValidEmail(String email) {
            return android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();
        }

        private void initializeComponent() {
            registratiLgn = findViewById(R.id.btRegistratiLgn);
            EmailLgn = findViewById(R.id.tilEmailLgn);
            PasswordLgn = findViewById(R.id.tilPasswordLgn);
            loginLgn = findViewById(R.id.btLoginLgn);
            progressbar = findViewById(R.id.progressbarLgn);
        }



}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/sfondo"
    android:id="@+id/activity_singin"
    android:clickable="true"
    android:gravity="center"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="336dp"
        android:layout_height="196dp"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="10dp">

        <TextView
            android:id="@+id/txtNomeApp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="7dp"
            android:clickable="true"
            android:fontFamily="sans-serif-medium"
            android:gravity="center"
            android:text="NaTour21"
            android:textColor="@color/white"
            android:textSize="60sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="361dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="10dp">


        <EditText
            android:id="@+id/tilEmailLgn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginTop="25dp"
            android:drawableLeft="@drawable/ic_email"
            android:ems="10"
            android:hint="Email"
            android:textColor="@color/white"
            android:inputType="textEmailAddress"
            app:backgroundTint="@color/white"/>

        <EditText
            android:id="@+id/tilPasswordLgn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginTop="10dp"
            android:drawableLeft="@drawable/ic_password"
            android:drawableRight="@drawable/ic_occhio"
            android:ems="10"
            android:hint="Password"
            android:inputType="textPassword"
            android:textColor="@color/white"
            app:backgroundTint="@color/white"/>

        <ProgressBar
            android:id="@+id/progressbarLgn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="invisible"
            app:layout_constraintTop_toBottomOf="@id/containerComponents" />

        <Button
            android:id="@+id/btLoginLgn"
            android:layout_width="232dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginTop="30dp"
            android:background="@drawable/button_login"
            android:text="Login"
            android:textColor="@color/white"
            android:textSize="16sp"
            android:textStyle="bold"/>

        <Button
            android:id="@+id/btRegistratiLgn"
            android:layout_width="232dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginTop="20dp"
            android:background="@drawable/button_registrati"
            android:text="Registrati"
            android:textColor="@color/white"
            android:textSize="16sp"
            android:textStyle="bold" />

    </LinearLayout>

</LinearLayout>
  • ok,perfett sorry – Fabiola Salomone Sep 18 '22 at 16:00
  • That's not how you should deal with asynchronous operations. You need to create a [callback](https://stackoverflow.com/questions/47847694/how-to-return-datasnapshot-value-as-a-result-of-a-method/47853774). Since you're using Java, I think that this [resource](https://medium.com/firebase-tips-tricks/how-to-create-a-clean-firebase-authentication-using-mvvm-37f9b8eb7336) will help. – Alex Mamo Sep 19 '22 at 08:36

1 Answers1

0

Dear @Fabiola Salomone if you are trying to use MVP because you said you have a presenter then i am afraid you are not doing it correctly at least according to what i know about MVP please study about it that will be better instead of me or anyone else telling you the code directly because you will not know how it works that way , for a head start you need to make an interface and presenter and inside that presenter you will make three more interfaces View Model and Presenter again and then you will initialize that presenter inside you main activity , here you are extending you presenter with appcompactActivty that itself is wrong a lot about your code is wrong its not MVP by any means so i don't how can i help you exactly with this apart from what i just said, Once you know how MVP work then me or other users can surely help you with this, Still if you need any clearance you can ask :)