尝试在空对象引用上调用虚拟方法'java.lang.String'

时间:2019-11-07 12:43:29

标签: java android google-cloud-firestore

我创建了一个Firebase Firestore,并通过注册页面在集合用户中插入了一些数据。一切正常,但是当我在Profile Activity中取回数据时,首先我创建了一个类型为User(我创建的模型)的新变量mUser,但是如果我在“ for”之外使用该变量mUser,数据库中的文档,它为空。

能否请您看看并告诉我如何正确获取?预先谢谢你!

public class ProfileActivity extends AppCompatActivity {

    //Firebase
    FirebaseAuth mAuth;
    FirebaseFirestore db;
    StorageReference mStorageRef;
    StorageTask mUploadTask;

    //Layout

    ImageView profilePic;
    TextView fullnameView;
    TextView rateView;
    TextView addressView;

    //Vars

    User mUser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);
        BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_nav);
        Menu menu = bottomNavigationView.getMenu();
        MenuItem menuItem = menu.getItem(1);
        menuItem.setChecked(true);
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                switch (menuItem.getItemId())
                {
                    case R.id.nav_home:
                        Intent h = new Intent(ProfileActivity.this, MainActivity.class);
                        startActivity(h);
                        overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
                        finish();

                        break;
                    case R.id.nav_profile:

                            Intent p = new Intent(ProfileActivity.this, ProfileActivity.class);
                            startActivity(p);
                            overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
                            finish();


                        break;
                    case R.id.nav_settings:
                        Intent s = new Intent(ProfileActivity.this, SettingsActivity.class);
                        startActivity(s);
                        overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
                        finish();

                        break;
                }
                return false;
            }
        });


        mAuth = FirebaseAuth.getInstance();
        db = FirebaseFirestore.getInstance();
        mStorageRef = FirebaseStorage.getInstance().getReference("users-images");

        fullnameView = findViewById(R.id.fullnameview);
        addressView = findViewById(R.id.addressview);
        profilePic = findViewById(R.id.profilePicView);
        rateView = findViewById(R.id.rate);


        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
        if(user != null)
        {
            db.collection("Users").whereEqualTo("email", user.getEmail())
                    .get()
                    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                        @Override
                        public void onComplete(@NonNull Task<QuerySnapshot> task) {
                            if(task.isSuccessful())
                            {
                                for(QueryDocumentSnapshot documentSnapshot: task.getResult()) {

                                           String fullname = documentSnapshot.get("fullname").toString();
                                           String address = documentSnapshot.get("address").toString();
                                           String city =  documentSnapshot.get("city").toString();
                                           String state =  documentSnapshot.get("state").toString();
                                           String country = documentSnapshot.get("country").toString();
                                           String phone = documentSnapshot.get("phone").toString();
                                           String zipcode = documentSnapshot.get("zipcode").toString();
                                           String type = documentSnapshot.get("type").toString();
                                           String rate = documentSnapshot.get("rate").toString();
                                           Boolean isActivated = documentSnapshot.getBoolean("isActivated");
                                           List<Card> cards= (List<Card>) documentSnapshot.get("cards");
                                           List<Comments> comments = (List<Comments>) documentSnapshot.get("comments");
                                           List<Documents> documents = (List<Documents>) documentSnapshot.get("documents");
                                           String profilePic = documentSnapshot.get("profilePic").toString();
                                           String email = documentSnapshot.get("email").toString();
                                           String password = documentSnapshot.get("password").toString();


                                           mUser = new User(
                                                   fullname,
                                                   address,
                                                   city,
                                                   state,
                                                   country,
                                                   phone,
                                                   zipcode,
                                                   type,
                                                   rate,
                                                   isActivated,
                                                   cards,
                                                   comments,
                                                   documents,
                                                   profilePic,
                                                   email,
                                                   password

                                           );


                                }
                            }
                        }
                    });
        }



            fullnameView.setText(mUser.getFullname());
            addressView.setText(mUser.getAddress());




    }
}


此后我得到

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.****.Models.User.getFullname()' on a null object reference
        at com.example.***.ProfileActivity.onCreate(ProfileActivity.java:161)

因为我在

之外使用了mUser
for(QueryDocumentSnapshot documentSnapshot: task.getResult()) {
...
}

如何获取数据并在“用于”之外使用数据?

谢谢!

0 个答案:

没有答案