Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 343 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Firebase "Failed to convert a value of type java.util.HashMap to int"

#1
At this line of my code in my `OnDataChange()` method in the `ValueEvenListener`:

int latest = dataSnapshot.getValue(Integer.class);

I'm getting a `DatabaseException` with the error `Failed to convert a value of type java.util.HashMap to int`.

However, in my database, you can take a look at the image below:

[![enter image description here][1]][1]

It is obviously not a `HashMap` but an `int`. Is this a bug or am I doing something wrong? What can I do to fix it? Why is it retrieving a `Hashmap` when the value is `int`?

**Full dataSnapshot:**

final DatabaseReference database = FirebaseDatabase.getInstance().getReference();
database.child("Campaigns").child(key).child("count");
database.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int latest = dataSnapshot.getValue(Integer.class);
button.setText(latest + "");
}

@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(context, context.getString(R.string.error) + ": " + databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});

**Database:**

Campaigns:{
-JDKKDJIIDJFIDJKDK:{
count:2432
}
}

[1]:


Reply

#2
It turns out I had to child dataSnapshot to my destination again. E.g:

int latest = dataSnapshot.child("Campaigns").child(key).child("count").getValue(Integer.class);

By default `dataSnapshot` is actually my whole database.

Reply

#3
This should work if we assume that the dataSnapshot is right.



final DatabaseReference database = FirebaseDatabase.getInstance().getReference();
database = database.child("Campaigns").child(key).child("count"); // replaced

database.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int latest = Integer.valueOf(dataSnapshot.getValue().toString()); // replaced
button.setText(latest + "");
}

@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(context, context.getString(R.string.error) + ": " + databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});

Reminder : **addValueEventListener** will run everytime there is a change in the dataSnapshot.
If you want to run it just once use **addListenerForSingleValueEvent** instead.
Reply

#4
Your `ValueEventListener` is attached to the whole database.

// This line gets a reference to the whole database
final DatabaseReference database = FirebaseDatabase.getInstance().getReference();

// This line creates a child DatabaseReference, but you don't assign
// the child to a variable
database.child("Campaigns").child(key).child("count");

// This line adds a ValueEventListener to the whole database
database.addValueEventListener(new ValueEventListener() {

What you want instead is this:

final DatabaseReference database = FirebaseDatabase.getInstance().getReference()
final DatabaseReference countRef = database.child("Campaigns").child(key).child("count")
countRef.addValueEventListener(new ValueEventListener() {
// ...
});

You can see that in the latter example the ValueEventListener is attached to the child reference, not to the root.
Reply

#5
HashMap<String, String> hashMap = (HashMap<String, String>) dataSnapshot1.getValue();
System.out.println("hehe " + hashMap);
String demo = String.valueOf(hashMap.get("sets"));
String name = String.valueOf(hashMap.get("name"));



The COde Above ^^^ Worked fine for me.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through