-2

I'm trying to create an app that takes informations from user and then puts the data on Firebase Realtime Database. I can store data without problem but when i try retrieve data it does not appear in the TextView.

This is my code

public class UserProfileActivity extends AppCompatActivity {

// Creating button.
Button logout;

// Creating TextView.
TextView userEmailShow, text, text2, text3, text4, text5, text6, text7;

// Creating FirebaseAuth.
FirebaseAuth firebaseAuth;
TextView ShowDataTextView;
String NameHolder, NumberHolder;
// Creating FirebaseAuth.
FirebaseUser firebaseUser;
Firebase firebase;
public static final String Firebase_Server_URL = "https://gakusei-
go.firebaseio.com/";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_profile);

    // Assigning ID's to button and TextView.
    logout = (Button) findViewById(R.id.logout);
    userEmailShow = (TextView) findViewById(R.id.user_email);
    text = (TextView)findViewById(R.id.textView);
    text2 = (TextView)findViewById(R.id.textView2);
    text3 = (TextView)findViewById(R.id.textView3);
    text4 = (TextView)findViewById(R.id.textView4);
    text5 = (TextView)findViewById(R.id.textView5);
    text6 = (TextView)findViewById(R.id.textView6);
    text7 = (TextView)findViewById(R.id.textView7);



    // Adding FirebaseAuth instance to FirebaseAuth object.
    firebaseAuth = FirebaseAuth.getInstance();

    // On activity start check whether there is user previously logged in or not.
    if (firebaseAuth.getCurrentUser() == null) {

        // Finishing current Profile activity.
        finish();

        // If user already not log in then Redirect to LoginActivity .
        Intent intent = new Intent(UserProfileActivity.this, LoginActivity.class);
        startActivity(intent);

        // Showing toast message.
        Toast.makeText(UserProfileActivity.this, "Please log in to continue", Toast.LENGTH_LONG).show();

    }

    // Adding firebaseAuth current user info into firebaseUser object.
    firebaseUser = firebaseAuth.getCurrentUser();

    // Getting logged in user email from firebaseUser.getEmail() method and set into TextView.
    userEmailShow.setText("Welcome " + firebaseUser.getEmail());
    firebase.child("Student").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot MainSnapshot) {

                Student student = MainSnapshot.getValue(Student.class);

                // Adding name and phone number of student into string that is coming from server.

                    String pname = student.getPname();
                    String pnum =  student.getPhonenumber();
                    String sname = student.getStudentname();
                    String email = student.getEmail();
                    String dorm =  student.getDorm();
                    String password = student.getPassword();
                    String clas = student.getStudentclass();

                    text.setText(pname);
                    text2.setText(pnum);
                    text3.setText(sname);
                    text4.setText(email);
                    text5.setText(dorm);
                    text6.setText(password);
                    text7.setText(clas);
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {
            System.out.println("Data Access Failed" + firebaseError.getMessage());
        }
    });

When i build the apk, it crashed saying

NullPointerException.

I searched this on how to solve it but didn't work. Below is my database.

enter image description here

aaa
  • 857
  • 4
  • 25
  • 46
goodson64
  • 9
  • 1
  • can you update the error log?? and your model class Student – Benjith Mathew Dec 15 '17 at 03:37
  • The `NullPointerException` should typically indicate clearly what line in your code causes it. From there it's a matter of tracing why the thing is `null` and fixing that. See https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – Frank van Puffelen Dec 15 '17 at 03:42

2 Answers2

3

You have to change these lines

@Override
    public void onDataChange(DataSnapshot MainSnapshot) {

            Student student = MainSnapshot.getValue(Student.class);

            // Adding name and phone number of student into string that is coming from server.

                String pname = student.getPname();
                String pnum =  student.getPhonenumber();
                String sname = student.getStudentname();
                String email = student.getEmail();
                String dorm =  student.getDorm();
                String password = student.getPassword();
                String clas = student.getStudentclass();

                text.setText(pname);
                text2.setText(pnum);
                text3.setText(sname);
                text4.setText(email);
                text5.setText(dorm);
                text6.setText(password);
                text7.setText(clas);
    }

by this

@Override
    public void onDataChange(DataSnapshot MainSnapshot) {

         for(DataSnapshot datasnapShot:MainSnapshot.getChildren()){
            Student student = datasnapShot.getValue(Student.class);

            // Use yore logic here... Use ArrayList<Student> or anything to  Store  the details

                String pname = student.getPname();
                String pnum =  student.getPhonenumber();
                String sname = student.getStudentname();
                String email = student.getEmail();
                String dorm =  student.getDorm();
                String password = student.getPassword();
                String clas = student.getStudentclass();

                text.setText(pname);
                text2.setText(pnum);
                text3.setText(sname);
                text4.setText(email);
                text5.setText(dorm);
                text6.setText(password);
                text7.setText(clas);
               }            
    }
Benjith Mathew
  • 1,211
  • 1
  • 11
  • 23
0

If you are trying to show details related to a particular user,first save the details in database using a unique key (uid of user) instead of pushId. You can do it like this...

//Get user object
  FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
  if(user != null) {
    //Save data in database
    Student mStudent = new Student(......)  //Your Student Object
     FirebaseDatabase.getInstance().getReference().child("Student").child(user.getUid()).setValue(mStudent);
  }

Now you can get data for a particular user like this...

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if(user != null) {
FirebaseDatabse.getInstance().getReference().child("Student").child(user.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot mainSnapshot) {

            Student student = mainSnapshot.getValue(Student.class);

            // Adding name and phone number of student into string that is coming from server.

                String pname = student.getPname();
                String pnum =  student.getPhonenumber();
                String sname = student.getStudentname();
                String email = student.getEmail();
                String dorm =  student.getDorm();
                String password = student.getPassword();
                String clas = student.getStudentclass();

                text.setText(pname);
                text2.setText(pnum);
                text3.setText(sname);
                text4.setText(email);
                text5.setText(dorm);
                text6.setText(password);
                text7.setText(clas);
    }

    @Override
    public void onCancelled(FirebaseError firebaseError) {
        System.out.println("Data Access Failed" + firebaseError.getMessage());
    }
});
}

In your code you are adding a child event listener to 'firebase' which you have not initialised anywhere that is why NullPointerException is thrown...

Amit Joshi
  • 126
  • 1
  • 7