代码之家  ›  专栏  ›  技术社区  ›  Ticherhaz FreePalestine אור מזרחי

FielBASE数据库异常:试图在空对象引用上调用虚拟方法“java.

  •  0
  • Ticherhaz FreePalestine אור מזרחי  · 技术社区  · 6 年前

    我已经到处搜索并尝试了他们的方法来调用虚拟方法,但是结果仍然是一样的。请帮帮我,我不知道我的错误在哪里。

    当我在手机上运行应用程序时,它会引发以下异常:

    尝试调用虚拟方法 “Java.Lang.StrugMyPosial.Subjix.GETUsReId()”的空值 对象引用

    @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
    
      //FirebaseAuth
            firebaseAuth = FirebaseAuth.getInstance();
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
                userID = user.getUid();
            }
            //Realtime Firebase
            firebaseDatabase = FirebaseDatabase.getInstance();
            DatabaseReference newData = firebaseDatabase.getReference();
            //Display data
            newData.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    try {
                        showData(dataSnapshot);
                    } catch (Exception e) {
                        Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG).show();
                    }
                }
                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                }
            });
    }
    

    方法ShowData()

     private void showData(DataSnapshot dataSnapshot) {
            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                studentInformation studentData = new studentInformation();
                studentData.setUserID(ds.child(userID).getValue(studentInformation.class).getUserID());
                studentData.setStudentFullName(ds.child(userID).getValue(studentInformation.class).getStudentFullName());
                studentData.setStudentID(ds.child(userID).getValue(studentInformation.class).getStudentID());
                studentData.setEmail(ds.child(userID).getValue(studentInformation.class).getEmail());
                studentData.setCampus(ds.child(userID).getValue(studentInformation.class).getCampus());
                studentData.setCourse(ds.child(userID).getValue(studentInformation.class).getCourse());
                studentData.setIntake(ds.child(userID).getValue(studentInformation.class).getIntake());
            }
        }
    

    类studentinformation()。

    package mypackage;
    
    import com.google.firebase.database.IgnoreExtraProperties;
    
    @IgnoreExtraProperties
    public class studentInformation {
    
        private String userID;
        private String studentFullName;
        private long studentID;
        private String email;
        private String campus;
        private String course;
        private String intake;
    
        studentInformation() {
        }
    
        studentInformation(String userID, String studentFullName, long studentID, String email, String campus, String course, String intake) {
            this.userID = userID;
            this.studentFullName = studentFullName;
            this.studentID = studentID;
            this.email = email;
            this.campus = campus;
            this.intake = intake;
        }
    
        public String getStudentFullName() {
            return studentFullName;
        }
    
        public void setStudentFullName(String studentFullName) {
            this.studentFullName = studentFullName;
        }
    
        public long getStudentID() {
            return studentID;
        }
    
        public void setStudentID(long studentID) {
            this.studentID = studentID;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
        public String getCampus() {
            return campus;
        }
    
        public void setCampus(String campus) {
            this.campus = campus;
        }
    
        public String getCourse() {
            return course;
        }
    
        public void setCourse(String course) {
            this.course = course;
        }
    
        public String getIntake() {
            return intake;
        }
    
        public void setIntake(String intake) {
            this.intake = intake;
        }
    
        public String getUserID() {
            return userID;
        }
    
        public void setUserID(String userID) {
            this.userID = userID;
        }
    }
    

    FireBase数据库: 我导出了它,然后从json改为txt。

    {
      "main" : {
        "home" : {
          "announcement-date" : "jkljlkjdaj",
          "campus" : {
            "unikl-miit" : {
              "course" : {
                "bcem" : {
                  "courseName" : "BCEM"
                }
              }
            },
            "unikl-mimet" : {
              "course" : {
                "bse" : {
                  "courseName" : "BSE"
                }
              }
            }
          }
        }
      },
      "student" : {
        "6LKSe5oqfwgrkEV57NXQekDvNhl1" : {
          "campus" : "UniKL MIIT",
          "course" : "Diploma In Animation",
          "email" : "hazman@gmail.com",
          "intake" : "55",
          "studentFullName" : "haMan",
          "studentID" : 52213116153,
          "userID" : "6LKSe5oqfwgrkEV57NXQekDvNhl1"
        },
        "evPFfkE1VtObUlBNz8pizSNKt662" : {
          "campus" : "UniKL MIIT",
          "course" : "Bachelor of Multimedia Technology (Hons) in Interactive Multimedia Design (BIMD)",
          "email" : "fakyou@gmshit.com",
          "intake" : "69",
          "studentFullName" : "Adam",
          "studentID" : 52213116190,
          "userID" : "evPFfkE1VtObUlBNz8pizSNKt662"
        },
        "rcUgnUBqd4Xb3AxmngE3OD1BBpd2" : {
          "campus" : "UniKL MIIT",
          "email" : "hazman45@gmail.com",
          "intake" : "52/49/4",
          "studentFullName" : "jwjajs",
          "studentID" : 52213116153,
          "userID" : "rcUgnUBqd4Xb3AxmngE3OD1BBpd2"
        },
        "z46FjWQE8TRYiJxIexMkrp6gSYv2" : {
          "campus" : "UniKL MIIT",
          "email" : "zoowake97@gmail.com",
          "intake" : "16/9/18",
          "studentFullName" : "Eisom",
          "studentID" : 52213116132,
          "userID" : "z46FjWQE8TRYiJxIexMkrp6gSYv2"
        }
      }
    }
    

    问题[更新]:

    //FirebaseAuth
            firebaseAuth = FirebaseAuth.getInstance();
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
                userID = user.getUid();
    
                DatabaseReference usersRef = FirebaseDatabase.getInstance().getReference("student");
                DatabaseReference currentUserRef = usersRef.child(userID);
                currentUserRef.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        studentData = dataSnapshot.getValue(studentInformation.class);
    
                        //studentID I delcared outside the onCreate. Does the 'studentID' store from Firebase?
                        studentFullName = studentData.getStudentFullName();//This one
    
                        textViewEmail.setText(studentData.getEmail());//Its working, display
                    }
    
                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {
                        throw databaseError.toException(); // don't ignore errors
                    }
                });
            }
            //At 'onCreate'
            textViewStudentName.setText(studentFullName); //Can I do like this?
    

    更新日期:2018年6月16日问题:

    我想从新的FireBaseDatabaseCampus中的FireBase数据库获取值。

    我做了这个,应用程序崩溃了。请帮帮我。

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //Inflate the layout for this fragment
        View viewMain = inflater.inflate(R.layout.fragment_campus, container, false);
    
        recyclerView = viewMain.findViewById(R.id.recycleViewCampus);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));//Ini penting nak display
    
        firebaseAuth = FirebaseAuth.getInstance();//Ini penting
        firebaseDatabase = FirebaseDatabase.getInstance();
    
        //So retrieve data
        userID = firebaseAuth.getUid();
        databaseReferenceStudent = firebaseDatabase.getReference().child("student").child(userID);
        databaseReferenceStudent.addValueEventListener(new ValueEventListener() { //So sini kita ambik value campus ~ UniKL MIIT
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                studentData = new studentInformation();//Ini declare
                studentData = dataSnapshot.getValue(studentInformation.class);
    
                //1. So student.getCampus is here
                databaseReferenceCampus = firebaseDatabase.getReference().child("campus").child(studentData.getCampus()).child("post");
            }
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
            }
        });
        return viewMain;
    }
    
    //On Start
    @Override
    public void onStart() {
        super.onStart();
        FirebaseRecyclerAdapter<campus, CampusViewHolder> firebaseRecyclerAdapterCampus =
                new FirebaseRecyclerAdapter<campus, CampusViewHolder>(
                        campus.class,
                        R.layout.campus_row,
                        CampusViewHolder.class,
                        databaseReferenceCampus     //So the value getCampus is here?
                ) {
                    @Override
                    protected void populateViewHolder(CampusViewHolder viewHolder, campus model, int position) {
                        viewHolder.setDetails(getContext(), model.getImage(), model.getTitle(), model.getDate(), model.getShortDescription());
                    }
                };
        //setAdapter
        recyclerView.setAdapter(firebaseRecyclerAdapterCampus);
    }
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Frank van Puffelen    6 年前

    您从根目录读取所有数据,然后遍历子目录。相反,你会希望在 student 节点:

    DatabaseReference rootRef = firebaseDatabase.getReference();
    rootRef.child("student").addValueEventListener(new ValueEventListener() {
        ...
    

    更新 :我现在感觉你只是在试图读取当前用户的数据。在这种情况下,代码是:

    if (user != null) {
        userID = user.getUid();
        DatabaseReference usersRef = FirebaseDatabase.getInstance().getReference("user");
        DatabaseReference currentUserRef = usersRef.child(userID);
        currentUserRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                studentInformation studentData = dataSnapshot.getValue(studentInformation.class)
                ...    
            }
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                throw databaseError.toException(); // don't ignore errors
            }
        });
    }