0

i have wrriten a code and getting error in 06-30 23:55:00.702 2499-2499/com.example.parthtiwari.trace E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.parthtiwari.trace, PID: 2499 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null

my code is

import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.auth.AuthResult;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class MainActivity extends AppCompatActivity implements 

View.OnClickListener {
    private FirebaseAuth fb;
    private DatabaseReference databaseReference;
    private EditText editTextEmail;
    private EditText editTextPassword;
    private EditText editTextName, mobile_number,vehicle_number,editTextAddress;
    private Button buttonSignup;
    private TextView textViewSignin;
    private ProgressDialog progressDialog;

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

        if(fb.getCurrentUser() != null){
            finish();
            startActivity(new Intent(getApplicationContext(),welcome.class));
        }

        databaseReference = FirebaseDatabase.getInstance().getReference();
        editTextAddress = (EditText) findViewById(R.id.Address);
        editTextName = (EditText) findViewById(R.id.name);
        mobile_number = (EditText) findViewById(R.id.mobile);
        vehicle_number = (EditText) findViewById(R.id.vehicle);
        editTextEmail = (EditText) findViewById(R.id.Email);
        editTextPassword = (EditText) findViewById(R.id.Password);
        buttonSignup = (Button) findViewById(R.id.Signup);
        textViewSignin = (TextView) findViewById(R.id.Signin);
        progressDialog = new ProgressDialog(this);
        FirebaseUser user = fb.getCurrentUser();
        buttonSignup.setOnClickListener(this);
        textViewSignin.setOnClickListener(this);
    }

    private void registerUser() {
        String email = editTextEmail.getText().toString().trim();
        String password = editTextPassword.getText().toString().trim();
        if (TextUtils.isEmpty(email)) {
            Toast.makeText(this, "Please enter email", 
Toast.LENGTH_LONG).show();
            return;
        }

        if (TextUtils.isEmpty(password)) {
            Toast.makeText(this, "Please enter password", 
Toast.LENGTH_LONG).show();
            return;
        }
        progressDialog.setMessage("Registering Please Wait...");
        progressDialog.show();
        fb.createUserWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            Toast.makeText(MainActivity.this, "Successfully registered", Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(MainActivity.this, "Registration Error", Toast.LENGTH_LONG).show();
                        }
                        progressDialog.dismiss();

                    }
                });
        saveUserInformation();
    }

    private void saveUserInformation() {
        String name = editTextName.getText().toString().trim();
        String mob = mobile_number.getText().toString().trim();
        String vehicle = vehicle_number.getText().toString().trim();
        String add = editTextAddress.getText().toString().trim();
        //creating a userinformation object
        userinfo userInformation = new userinfo(name, mob, vehicle, add);
        //getting the current logged in user
        FirebaseUser user = fb.getCurrentUser();
        databaseReference.child(user.getUid()).setValue(userInformation);
        //displaying a success toast
        Toast.makeText(this, "Information Saved...", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onClick(View view) {
        if(view == buttonSignup){
            registerUser();

        }
        if(view == textViewSignin){
            startActivity(new Intent(this, login.class));

        }
    }
}
Jen Person
  • 7,356
  • 22
  • 30
Parth Tiwari
  • 466
  • 8
  • 23

1 Answers1

0

Try moving saveUserInformation() into your onCompletionListener.

Looks like saveUserInformation() is called before registration is completed.

 fb.createUserWithEmailAndPassword(email, password).addOnSuccessListener(new OnSuccessListener<AuthResult>() {
                @Override
                public void onSuccess(AuthResult authResult) {
         saveUserInformation(authResult);
   Toast.makeText(MainActivity.this, "Successfully 
  registered",Toast.LENGTH_LONG).show();
  progressDialog.dismiss(); }).addOnFailureListener(new OnFailureListener() { 
 @Override
  public void onFailure(@NonNull Exception e) {
 Toast.makeText(MainActivity.this, "Registration Error", 
 Toast.LENGTH_LONG).show();
  progressDialog.dismiss();
            });

and change

saveUserInformation(AuthResult authResult)

authResult.getUid();
Enividmk
  • 41
  • 1
  • 4
  • how saveUserInformation() will into onCompletionListener. could you please make changes in the code – Parth Tiwari Jun 30 '17 at 20:14
  • if (task.isSuccessful()) { Toast.makeText(MainActivity.this, "Successfully registered", Toast.LENGTH_LONG).show(); saveUserInformation() ; } else { Toast.makeText(MainActivity.this, "Registration Error", Toast.LENGTH_LONG).show(); } – Enividmk Jun 30 '17 at 20:20
  • sir i am not getting you – Parth Tiwari Jun 30 '17 at 20:22
  • You are trying to "saveUserInformation()" before you get results from fb.createUserWithEmailAndPassword(email, password).addOnCompleteListener... So, you get an exception when calling user.getUID because user is null. Instead call saveUserInformation() after task.isSuccessful(). – Enividmk Jun 30 '17 at 20:29
  • now i am able to registar but in database i am not able to save data but message is poping Information saved ?? – Parth Tiwari Jun 30 '17 at 20:31
  • help me i have made the changes now i am able to registar but the value are not in database but message is coming data inserted – Parth Tiwari Jun 30 '17 at 20:39
  • double-check your userinfo constructor, make sure its values aren't null. – Enividmk Jun 30 '17 at 20:48
  • public class userinfo { public String name; public String mob; public String vehicle; public String address; public String pass; public userinfo(){ } public userinfo(String name, String mob, String vehicle, String address) { this.name = name; this.mob = mob; this.vehicle = vehicle; this.address = address; } } – Parth Tiwari Jun 30 '17 at 20:59
  • public class userinfo { public String name; public String mob; public String vehicle; public String address; public String pass; public userinfo(){ } public userinfo(String name, String mob, String vehicle, String address) { this.name = name; this.mob = mob; this.vehicle = vehicle; this.address = address; } } – Parth Tiwari Jun 30 '17 at 21:00
  • public class userinfo { public String name; public String mob; public String vehicle; public String address; public String pass; public userinfo(){ } public userinfo(String name, String mob, String vehicle, String address) { this.name = name; this.mob = mob; this.vehicle = vehicle; this.address = address; } } sir this is my java class of userinfo – Parth Tiwari Jun 30 '17 at 21:00
  • Try using .addOnSuccessListener(new OnSuccessListener() instead of .addOnCompleteListener(this, new OnCompleteListener()... OnSuccesListener returns a AuthResult object from which you can get the current user info. – Enividmk Jun 30 '17 at 21:33
  • You should make sure your variables aren't null; especially user.getUid() – Enividmk Jun 30 '17 at 21:41
  • sir could you please illustrate as my java class code is public class userinfo { public String name; public String mob; public String vehicle; public String address; public String pass; public userinfo(){ } public userinfo(String name, String mob, String vehicle, String address) { this.name = name; this.mob = mob; this.vehicle = vehicle; this.address = address; } } could please make canges pleae sir – Parth Tiwari Jun 30 '17 at 21:49
  • sir could you please sole the problem also https://stackoverflow.com/questions/44863682/unable-to-retrieve-data-from-firebase-database-in-android – Parth Tiwari Jul 01 '17 at 17:41
  • https://stackoverflow.com/questions/44865530/why-i-am-not-able-to-reterive-all-the-user-details-from-database-in-firebase sir please look in this – Parth Tiwari Jul 01 '17 at 21:25