0

I'm trying to use Firebase Database but I get that exception (in the title). First I wrote under onDataChange: Map map = dataSnapshot.getValue(Map.class); but then they asked me to use a generictypeindicator, which I did. However, I now get this new exception. How do I fix this?

Here are some important lines of code enter image description here

enter image description here

    //accessing the firebase database
    database=FirebaseDatabase.getInstance();
    //creates a database references
    databaseRef1=database.getReference().child("Ben_Ptolemy");
    databaseRef2=database.getReference().child("Ptolemy_Ben");

 sendButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String messageText = messageArea.getText().toString();

            if(!messageText.equals("")){
                Map<String, String> map = new HashMap<String, String>();
                map.put("message", messageText);
                map.put("user", "Ben");
                databaseRef1.push().setValue(map);
                databaseRef2.push().setValue(map);
                messageArea.setText("");
            }
        }
    });

    databaseRef1.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Log.d("firebase received", "true");


            GenericTypeIndicator<Map<String, String>> genericTypeIndicator = new GenericTypeIndicator<Map<String, String>>() {};
            Map<String, String> map = dataSnapshot.getValue(genericTypeIndicator );
            String message = map.get("message").toString();
            String userName = map.get("user").toString();

            if(userName.equals("Ben")){
                addMessageBox("You:-\n" + message, 1);
            }
            else{
                addMessageBox("Ptolemy" + ":-\n" + message, 2);
            }
        }
7
  • Post your Firebase data so that we can figure out your mapping Commented Apr 12, 2018 at 18:46
  • which firebase data? the console? Commented Apr 12, 2018 at 18:51
  • Your db data where you attach the listener... Commented Apr 12, 2018 at 18:51
  • Also do post the location where databaseRef1 is listening to Commented Apr 12, 2018 at 18:52
  • database=FirebaseDatabase.getInstance(); databaseRef1=database.getReference().child("Ben_Ptolemy"); databaseRef2=database.getReference().child("Ptolemy_Ben"); Commented Apr 12, 2018 at 18:53

3 Answers 3

1

So as i said in comments, this is a mapping issue.

A Map can only be used if the location is a json itself

Eg:

"Something":{
    "Name":"john"
}

So if this is the Firebase data node, you use a Map as you have used.

ok based on what you've shown you do the following

String message = dataSnapshot.child("message").getValue(String.class);

This will give you the value of message for your ben or other nodes if you attach listeners in the same way as you've currently done

Sign up to request clarification or add additional context in comments.

8 Comments

No sorry, this is what I wrote myself in the console. But when I actually send a message through the app, the message gets shown like this in the console (please see main post, I updated the picture) Thank you EDIT: added 2 pictures
Ok check the updated answer. Should give you correct values
what did you edit? the answer is the same. Wwait let me try
No man, the way I'm showing.. the way to get data is different. There's a child method in between. It looks similar ;)
I tried, but String message gets a null value. Which means it's not reading anything.
|
1

Ok, finally got this to work. I used addChildEventListener instead of addValueEventListener and replaced Map map = dataSnapshot.getValue(Map.class); with Map<String, Object> map = (Map<String, Object>) dataSnapshot.getValue(); and everything worked.

Comments

0

This error occurs generally when you set the database name and class name same! Change anyone of them. It will okay!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.