how to authenticate and save data in firebase at same time using android studio

i have wrriten a code and getting error in
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

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));

}
}
}

Show Accepted Answer

Guest Quick Reply (No URL, BBcode or HTML)

Last post by jwolff
2 hours ago
Last post by kstreich
1 hour ago
Last post by xkihn
52 minutes ago
Last post by mdaugherty
1 hour ago
Last post by zboncak.elisha
2 hours ago
Last post by maximo.dibbert
1 hour ago
Cm9
Started by xschowalter
Last post by wsanford
3 hours ago
Last post by ebahringer
1 hour ago
Last post by dalton56
4 hours ago