0

I already searched everywhere and tried their methods on how to invoke a virtual method but the result is still the same. Please help me, I don't know where my mistake is.

When I run my app on a phone, it throws the following Exception:

Attempt to invoke virtual method 'java.lang.Stringmypackage.studentInformation.getUserID()' on a null object reference

@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) { } }); } 

Method 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()); } } 

Class 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 Database: I export it and I changed from json to 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" : "[email protected]", "intake" : "55", "studentFullName" : "haMan", "studentID" : 52213116153, "userID" : "6LKSe5oqfwgrkEV57NXQekDvNhl1" }, "evPFfkE1VtObUlBNz8pizSNKt662" : { "campus" : "UniKL MIIT", "course" : "Bachelor of Multimedia Technology (Hons) in Interactive Multimedia Design (BIMD)", "email" : "[email protected]", "intake" : "69", "studentFullName" : "Adam", "studentID" : 52213116190, "userID" : "evPFfkE1VtObUlBNz8pizSNKt662" }, "rcUgnUBqd4Xb3AxmngE3OD1BBpd2" : { "campus" : "UniKL MIIT", "email" : "[email protected]", "intake" : "52/49/4", "studentFullName" : "jwjajs", "studentID" : 52213116153, "userID" : "rcUgnUBqd4Xb3AxmngE3OD1BBpd2" }, "z46FjWQE8TRYiJxIexMkrp6gSYv2" : { "campus" : "UniKL MIIT", "email" : "[email protected]", "intake" : "16/9/18", "studentFullName" : "Eisom", "studentID" : 52213116132, "userID" : "z46FjWQE8TRYiJxIexMkrp6gSYv2" } } } 

Questions[Update]:

//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? 

Updated 16/06/2018 QUESTION:

I want to make the value from Firebase Database inside new firebaseDatabaseCampus.

I made this one, and apps crashed. Please help me.

@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); } 
3
  • 1
    It looks like ds.child(userID) does not contain a studentInformation.class. For more help, update your question to include a snippet of the JSON (as text, no screenshots please). You can get this by clicking the "Export JSON" link in your Firebase Database console. Commented Jun 12, 2018 at 3:46
  • @FrankvanPuffelen done updated. Commented Jun 12, 2018 at 3:52
  • @FrankvanPuffelen hello Sir, can you check my latest question in here? Thank you so much! Commented Jun 16, 2018 at 4:51

1 Answer 1

2

You're reading all data from the root and then iterating over the children. Instead you'll want to read one level lower, in the student node:

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

Update: I now get the feeling you're just trying to read the data of the current user. In that case, the code is:

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 } }); } 
Sign up to request clarification or add additional context in comments.

4 Comments

I have tried it, but the result still the same. same exception. there is no changes.
Yes! its working! I have a question, can I use the value from public void onDataChange() , outside from it. You can check my question, I updated it.
By the time your textViewStudentName.setText(studentFullName) runs, the studentFullName hasn't been set yet. See stackoverflow.com/questions/50434836/…
Which one would you prefer? This one or that one you gave it to me?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.