0
    public void loadUserInformation() {
    final String mNumber = getActivity().getIntent().getStringExtra("Phone_Number");
    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference usesrRef = rootRef.child("Users").child(uid);
    ValueEventListener eventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            EditText Name = (EditText) rootView.findViewById(R.id.Name);
            ImageView profilePic = (ImageView)rootView.findViewById(R.id.profilePic);
            for(DataSnapshot postSnapshot: dataSnapshot.getChildren()){
                String name=postSnapshot.child("Name").getValue().toString();
                String email=postSnapshot.child("Email").getValue().toString();
                String status=postSnapshot.child("Status").getValue().toString();
                String quote=postSnapshot.child("Quote").getValue().toString();
                String number = postSnapshot.child("Phone_Number").getValue().toString();
                String image = dataSnapshot.child("Image").getValue().toString();

                Name.setText(name);
                Email.setText(email);
                Status.setText(status);
                Quote.setText(quote);
                Number.setText(number);

               Picasso.get().load(image).into(profilePic);

            }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Toast.makeText(getActivity(),"Error Loading UserDetails",Toast.LENGTH_LONG).show();
        }
    };
    usesrRef.addListenerForSingleValueEvent(eventListener);
}

I have this method and the database will have name,email's values but wont have values from status quote... so if i use this method it will obviously give a nullpointerexception because the data is null... how to avoid that? Like i know the data is null so how to fetch the rest of the data but i want to show the null data has NULL or something... how do i achieve this

  • Related: [https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Bö macht Blau Apr 20 '18 at 17:36

2 Answers2

0

Use Optional<String> output = service.call();

Take a look tutorial here

example from http://www.baeldung.com/java-optional --> use this link for more information

In this way, if we pass in a null reference, it does not throw an exception but rather returns an empty Optional object as though we created it with the Optional.empty API:

@Test
public void givenNull_whenCreatesNullable_thenCorrect() {
    String name = null;
    Optional<String> opt = Optional.ofNullable(name);
    assertEquals("Optional.empty", opt.toString());
}

Updated

if you don't want empty you can use default value passing

@Test
public void whenOrElseWorks_thenCorrect() {
    String nullName = null;
    String name = Optional.ofNullable(nullName).orElse("null");
    assertEquals("null", name);
}
Barry
  • 33
  • 11
0

Use snapshot method hasChild() to check if the values for quote and status exists before using them, like so:

 String status=postSnapshot.hasChild("Status") ? postSnapshot.child("Status").getValue().toString() : 'NULL';
 String quote=postSnapshot.hasChild("Quote") ? postSnapshot.child("Quote").getValue().toString() : 'NULL';
Guilherme Lemmi
  • 3,271
  • 7
  • 30
  • 30