0

So , in my application , after registration it saves your personal info in firebase , with your UId (key value) , for example :

enter image description here here, in this class i am trying to get there data here :

public class featured extends AppCompatActivity
{
    FirebaseAuth auth;
    DatabaseReference db;
    String info = "";
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_page);

        auth = FirebaseAuth.getInstance();

        Toolbar bar =  (Toolbar) findViewById(R.id.my_toolbar);
        setSupportActionBar(bar);


        DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialogInterface, int i)
            {
                switch(i)
                {
                    case DialogInterface.BUTTON_NEGATIVE :
                        Toast.makeText(getApplicationContext(),"error",Toast.LENGTH_LONG).show();
                        finish();
                        break;
                    case DialogInterface.BUTTON_POSITIVE :
                        Toast.makeText(getApplicationContext(),"gratz",Toast.LENGTH_LONG).show();
                        break;
                }
            }
        };

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("esaa tqveni infoi ? : "+info)
                          .setPositiveButton("ho ra",dialogClickListener)
                          .setNegativeButton("nw",dialogClickListener)
                          .show();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.actions,menu);

        return super.onCreateOptionsMenu(menu);
    }

    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case R.id.filter :
                db = FirebaseDatabase.getInstance().getReference().child(auth.getCurrentUser().getUid());
                db.addValueEventListener(new ValueEventListener()
                {
                    public void onDataChange(DataSnapshot dataSnapshot)
                    {
                        for(DataSnapshot s : dataSnapshot.getChildren())
                        {
                            user usr = s.getValue(user.class);
                            Log.d("Value from : ",usr.getName());
                        }
                    }
                    public void onCancelled(DatabaseError databaseError)
                    {
                        Toast.makeText(getApplicationContext(), "yolo", Toast.LENGTH_LONG).show();
                    }
                });
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

here is user model :

public class user
{
    public String name;
    public String lastname;
    public String email;
    public String piradi;


    public user(String name,String lastname,String email,String piradi)
    {
        this.name = name;
        this.lastname = lastname;
        this.email = email;
        this.piradi = piradi;
    }

    public String getPiradi()
    {
        return piradi;
    }

    public void setPiradi(String piradi)
    {
        this.piradi = piradi;
    }

    public String getEmail()
    {
        return email;
    }

    public void setEmail(String email)
    {
        this.email = email;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getLastname()
    {
        return lastname;
    }

    public void setLastname(String lastname)
    {
        this.lastname = lastname;
    }
    public Map<String,Object> toMap()
    {
        HashMap<String,Object> result = new HashMap<>();
        result.put("name",name);
        result.put("lastname",lastname);
        result.put("email",email);
        result.put("piradi",piradi);

        return result;
    }
}

So after I run a code it's giving me nothing , just nullpointerexception error. Please help me :)

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • There is nothing that you can call duplicate, please read the post and after that write opinion :) –  Aug 16 '16 at 14:01

1 Answers1

0

You seem to be listening for just a single user value, so you shouldn't need a loop:

db = FirebaseDatabase.getInstance().getReference().child(auth.getCurrentUser().getUid());
db.addValueEventListener(new ValueEventListener() {
    public void onDataChange(DataSnapshot dataSnapshot) {
        user usr = s.getValue(user.class);
        Log.d("Value from : ",usr.getName());
    }
    public void onCancelled(DatabaseError databaseError) {
        Toast.makeText(getApplicationContext(), "yolo", Toast.LENGTH_LONG).show();
    }
});
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I still get nullpointer exception... –  Aug 16 '16 at 14:02
  • Have a look at the answer Arjan linked to investigate which line throws that error and what is going wrong. – Frank van Puffelen Aug 16 '16 at 14:59
  • i checked that already , i guess yesterday, if(user == null), output was null, i mean user equaled null , so idk what's a reason –  Aug 16 '16 at 17:21
  • Sorry I can't be of more help, but StackOverflow is not an ideal forum to debug your application. I recommend reducing the code further until you have the [minimum needed to reproduce the problem](http://stackoverflow.com/help/mcve). If you share just that code, we may be able to spot more. – Frank van Puffelen Aug 16 '16 at 18:13
  • I found a problem , I did a really silly mistake , as you see i was storing users by their UId in users "table" , so i forgot to add child of users there, i was directly connecting to uid child, so there was no issue with Firebase :) thx all... –  Aug 18 '16 at 14:02
  • Hi, @user5157625 can you update or show how you altered your code to access all the users with UID, please? – uplearned.com Oct 14 '20 at 22:15