I was trying to make login app, and it supposed to go to some fragments in bottom navigation,
when I log in, it give me this error:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.cake_ni_moskov.app.Customer.getState()' on a null object reference at com.cake_ni_moskov.app.CustomerFoodPanel.CustomerHomeFragment$1$1.onDataChange
CustomerHomeFragment.java
` public class CustomerHomeFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
RecyclerView recyclerView;
private List<UpdateDishModel> updateDishModelList;
private CustomerHomeAdapter adapter;
String State, City, Sub;
DatabaseReference dataaa, databaseReference;
SwipeRefreshLayout swipeRefreshLayout;
SearchView searchView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_customerhome, null);
getActivity().setTitle("Food On");
setHasOptionsMenu(true);
recyclerView = v.findViewById(R.id.recycle_menu);
recyclerView.setHasFixedSize(true);
Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.move);
recyclerView.startAnimation(animation);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
updateDishModelList = new ArrayList<>();
swipeRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipelayout);
swipeRefreshLayout.setOnRefreshListener(this);
swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimaryDark, R.color.green);
swipeRefreshLayout.post(new Runnable() {
@Override
public void run() {
swipeRefreshLayout.setRefreshing(true);
String userid = FirebaseAuth.getInstance().getCurrentUser().getUid();
dataaa = FirebaseDatabase.getInstance().getReference("Customer").child(userid);
dataaa.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Customer cust = dataSnapshot.getValue(Customer.class);
State = cust.getState();
City = cust.getCity();
Sub = cust.getSuburban();
customermenu();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
});
return v;
}
@Override
public void onRefresh() {
customermenu();
}
private void customermenu() {
swipeRefreshLayout.setRefreshing(true);
databaseReference = FirebaseDatabase.getInstance().getReference("FoodSupplyDetails").child(State).child(City).child(Sub);
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
updateDishModelList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
for (DataSnapshot snapshot1 : snapshot.getChildren()) {
UpdateDishModel updateDishModel = snapshot1.getValue(UpdateDishModel.class);
updateDishModelList.add(updateDishModel);
}
}
adapter = new CustomerHomeAdapter(getContext(), updateDishModelList);
recyclerView.setAdapter(adapter);
swipeRefreshLayout.setRefreshing(false);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
swipeRefreshLayout.setRefreshing(false);
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
search(newText);
return true;
}
});
}
private void search(final String searchtext) {
ArrayList<UpdateDishModel> mylist = new ArrayList<>();
for (UpdateDishModel object : updateDishModelList) {
if (object.getDishes().toLowerCase().contains(searchtext.toLowerCase())) {
mylist.add(object);
}
}
adapter = new CustomerHomeAdapter(getContext(), mylist);
recyclerView.setAdapter(adapter);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.search, menu);
MenuItem menuItem = menu.findItem(R.id.Searchdish);
searchView = (SearchView) menuItem.getActionView();
searchView.setQueryHint("Search Dish");
}
}
`
this is the class wher the "getState();" method came from
Customer.java ` public class Login extends AppCompatActivity {
TextInputLayout email, pass;
Button Signout, SignInphone;
TextView Forgotpassword;
TextView txt;
FirebaseAuth FAuth;
String em;
String pwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
try {
email = (TextInputLayout) findViewById(R.id.Lemail);
pass = (TextInputLayout) findViewById(R.id.Lpassword);
Signout = (Button) findViewById(R.id.button4);
txt = (TextView) findViewById(R.id.textView3);
Forgotpassword = (TextView) findViewById(R.id.forgotpass);
SignInphone = (Button) findViewById(R.id.btnphone);
FAuth = FirebaseAuth.getInstance();
Signout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
em = email.getEditText().getText().toString().trim();
pwd = pass.getEditText().getText().toString().trim();
if (isValid()) {
final ProgressDialog mDialog = new ProgressDialog(Login.this);
mDialog.setCanceledOnTouchOutside(false);
mDialog.setCancelable(false);
mDialog.setMessage("Logging in...");
mDialog.show();
FAuth.signInWithEmailAndPassword(em, pwd).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
mDialog.dismiss();
if (FAuth.getCurrentUser().isEmailVerified()) {
Toast.makeText(Login.this, "You are logged in", Toast.LENGTH_SHORT).show();
Intent z = new Intent(Login.this, CustomerFoodPanel_BottomNavigation.class);
startActivity(z);
finish();
} else {
ReusableCodeForAll.ShowAlert(Login.this, "", "Please Verify your Email");
}
} else {
mDialog.dismiss();
ReusableCodeForAll.ShowAlert(Login.this, "Error", task.getException().getMessage());
}
}
});
}
}
});
txt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent Register = new Intent(Login.this, Registeration.class);
startActivity(Register);
}
});
Forgotpassword.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent a = new Intent(Login.this, ForgotPassword.class);
startActivity(a);
}
});
SignInphone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Login.this, LoginPhone.class);
startActivity(intent);
}
});
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}`
and this is the login activity
Login.java `public class Login extends AppCompatActivity {
TextInputLayout email, pass;
Button Signout,SignInphone;
TextView Forgotpassword;
TextView txt;
FirebaseAuth FAuth;
String em;
String pwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
try {
email = (TextInputLayout) findViewById(R.id.Lemail);
pass = (TextInputLayout) findViewById(R.id.Lpassword);
Signout = (Button) findViewById(R.id.button4);
txt = (TextView) findViewById(R.id.textView3);
Forgotpassword=(TextView)findViewById(R.id.forgotpass);
SignInphone=(Button)findViewById(R.id.btnphone);
FAuth = FirebaseAuth.getInstance();
Signout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
em = email.getEditText().getText().toString().trim();
pwd = pass.getEditText().getText().toString().trim();
if (isValid()) {
final ProgressDialog mDialog = new ProgressDialog(Login.this);
mDialog.setCanceledOnTouchOutside(false);
mDialog.setCancelable(false);
mDialog.setMessage("Logging in...");
mDialog.show();
FAuth.signInWithEmailAndPassword(em, pwd).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
mDialog.dismiss();
if (FAuth.getCurrentUser().isEmailVerified()) {
Toast.makeText(Login.this, "You are logged in", Toast.LENGTH_SHORT).show();
Intent z = new Intent(Login.this, CustomerFoodPanel_BottomNavigation.class);
startActivity(z);
finish();
} else {
ReusableCodeForAll.ShowAlert(Login.this,"","Please Verify your Email");
}
} else {
mDialog.dismiss();
ReusableCodeForAll.ShowAlert(Login.this,"Error",task.getException().getMessage());
}
}
});
}
}
});
txt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent Register = new Intent(Login.this, Registeration.class);
startActivity(Register);
}
});
Forgotpassword.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent a=new Intent(Login.this,ForgotPassword.class);
startActivity(a);
}
});
SignInphone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(Login.this,LoginPhone.class);
startActivity(intent);
}
});
}catch (Exception e){
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
String emailpattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";
public boolean isValid() {
email.setErrorEnabled(false);
email.setError("");
pass.setErrorEnabled(false);
pass.setError("");
boolean isvalidemail=false,isvalidpassword=false,isvalid=false;
if (TextUtils.isEmpty(em))
{
email.setErrorEnabled(true);
email.setError("Email is required");
}
else {
if (em.matches(emailpattern))
{
isvalidemail=true;
}
else {
email.setErrorEnabled(true);
email.setError("Enter a valid Email Address");
}
}
if (TextUtils.isEmpty(pwd))
{
pass.setErrorEnabled(true);
pass.setError("Password is required");
}
else {
isvalidpassword=true;
}
isvalid = (isvalidemail && isvalidpassword) ? true : false;
return isvalid;
}
}'
I don't know what happened, I just tried some source code in github
https://github.com/Mufaddal5253110/Food_Delivery_Android_App_AndroidStudio_Firebase