0

this is what my logcat says: 2019-09-17 21:02:48.887 19276-19276/com.oopproject.androidbarberbooking E/AndroidRuntime: FATAL EXCEPTION: main Process: com.oopproject.androidbarberbooking, PID: 19276 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.oopproject.androidbarberbooking.Model.User.getName()' on a null object reference at com.oopproject.androidbarberbooking.Fragments.HomeFragment.setUserInformation(HomeFragment.java:152) at com.oopproject.androidbarberbooking.Fragments.HomeFragment.onCreateView(HomeFragment.java:94)

this is my home activity:

private void setUserInformation() public class HomeActivity extends AppCompatActivity {

@BindView(R.id.bottom_navigation)
BottomNavigationView bottomNavigationView;

BottomSheetDialog bottomSheetDialog;

CollectionReference userRef;

AlertDialog dialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    ButterKnife.bind(HomeActivity.this);

    userRef = FirebaseFirestore.getInstance().collection("User");
    dialog = new SpotsDialog.Builder().setContext(this).setCancelable(false).build();

    //Checked if Logged in
    if (getIntent() != null)
    {
        boolean isLogin = getIntent().getBooleanExtra(Common.IS_LOGIN,false);
        if (isLogin)
        {
            dialog.show();

            //Check if user  exist
            AccountKit.getCurrentAccount(new AccountKitCallback<Account>() {
                @Override
                public void onSuccess(Account account) {
                    if (account != null)
                    {
                        final DocumentReference currentUser = userRef.document(account.getPhoneNumber().toString());
                        currentUser.get()
                                .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                                    @Override
                                    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                                        if (task.isSuccessful())
                                        {
                                            DocumentSnapshot userSnapShot = task.getResult();
                                            if (!userSnapShot.exists())
                                            {
                                                showUpdateDialog(account.getPhoneNumber().toString());
                                            }
                                            else
                                            {
                                                //if there is already a registered user
                                                Common.currentUser = userSnapShot.toObject(User.class);
                                                bottomNavigationView.setSelectedItemId(R.id.action_home);
                                            }
                                            if (dialog.isShowing())
                                                dialog.dismiss();
                                        }
                                    }
                                });
                    }
                }

                @Override
                public void onError(AccountKitError accountKitError) {
                    Toast.makeText(HomeActivity.this, ""+accountKitError.getErrorType().getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        Fragment fragment = null;
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            if (menuItem.getItemId() == R.id.action_home) {
                fragment = new HomeFragment();
            } else if (menuItem.getItemId() == R.id.action_shop)
                fragment = new ShopFragment();


            return loadFragment(fragment);
        }
    });

}

private boolean loadFragment(Fragment fragment) {
    if (fragment != null)
    {
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,fragment)
                .commit();
        return true;
    }
    return false;
}

private void showUpdateDialog(final String phoneNumber) {

    bottomSheetDialog = new BottomSheetDialog(this);
    bottomSheetDialog.setTitle("One More Step!");
    bottomSheetDialog.setCanceledOnTouchOutside(false);
    bottomSheetDialog.setCancelable(false);
    View sheetView = getLayoutInflater().inflate(R.layout.layout_update_information,null);

    Button btn_update = (Button)sheetView.findViewById(R.id.btn_update);
    final TextInputEditText edt_name = (TextInputEditText)sheetView.findViewById(R.id.edt_name);
    final TextInputEditText edt_address = (TextInputEditText)sheetView.findViewById(R.id.edt_address);

    btn_update.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (!dialog.isShowing())
                dialog.show();

            User user = new User(edt_name.getText().toString(),
                    edt_address.getText().toString(),
                    phoneNumber);
            userRef.document(phoneNumber)
                    .set(user)
                    .addOnSuccessListener(new OnSuccessListener<Void>() {
                        @Override
                        public void onSuccess(Void aVoid) {
                            bottomSheetDialog.dismiss();
                            if (dialog.isShowing())
                                dialog.dismiss();

                            Common.currentUser = user;
                            bottomNavigationView.setSelectedItemId(R.id.action_home);

                            Toast.makeText(HomeActivity.this, "Thank You!", Toast.LENGTH_SHORT).show();
                        }
                    }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    bottomSheetDialog.dismiss();
                    if (dialog.isShowing())
                        dialog.dismiss();
                    Toast.makeText(HomeActivity.this, ""+e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
        }
    });

    bottomSheetDialog.setContentView(sheetView);
    bottomSheetDialog.show();
}

}

and my homefragment line 150-154 where Common.current user is used again:

private void setUserInformation() {
    layout_user_information.setVisibility(View.VISIBLE);
    txt_user_name.setText(Common.currentUser.getName());

}

and this is what is in my Model User java class:

package com.oopproject.androidbarberbooking.Model;

public class User {
      private String name,address,phoneNumber;

      public User() {
      }

      public User(String name, String address, String phoneNumber) {
            this.name = name;
            this.address = address;
            this.phoneNumber = phoneNumber;
      }

      public String getName() {
      return name;
      }

      public void setName(String name) {
           this.name = name;
      }

      public String getAddress() {
           return address;
      }

      public void setAddress(String address) {
           this.address = address;
      }

      public String getPhoneNumber() {
           return phoneNumber;
      }

      public void setPhoneNumber(String phoneNumber) {
           this.phoneNumber = phoneNumber;
      }
}

1 Answers1

0

From the stacktrace you can see, that your User is missing. And throw NullPointerException during usage om.oopproject.androidbarberbooking.Model.User.

Check why your implementation of the Common.currentUser returns you null. This is an issue.

GensaGames
  • 5,538
  • 4
  • 24
  • 53