0

I have seen lots of question like this but can't seem to get any of the solutions to work for me in my situation. I am using android studio, If you need any more info or files let me know and I will post.

I will post my code here first

package com.example.mick.drivers6;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;

//https://drivers6-55085.firebaseio.com/

public class MainActivity extends AppCompatActivity {
    private EditText editTextName;
    private EditText editLon;
    private EditText editLat;
    private TextView textViewPersons;
    private Button buttonSave;
    int count=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Firebase.setAndroidContext(this);

        buttonSave = (Button) findViewById(R.id.buttonSave);
        editTextName = (EditText) findViewById(R.id.editTextName);
        editLat = (EditText) findViewById(R.id.editLatitude);
        editLon = (EditText) findViewById(R.id.editLongitude);

        textViewPersons = (TextView) findViewById(R.id.textViewPersons);

        //Click Listener for button
        buttonSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Creating firebase object
                Firebase ref = new Firebase(Config.FIREBASE_URL);

                //Getting values to store
                String name = editTextName.getText().toString().trim();
                String lon = editLon.getText().toString().trim();
                String lat = editLat.getText().toString().trim();

                //Creating Person object

                Driver d = new Driver();
                //Adding values
                d.setName(name);
                d.setLat(lat);
                d.setLon(lon);

                //Storing values to firebase
                ref.setValue(d);
//                ref.child("Counter").setValue(count);
                count++;

                ref.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot snapshot) {
                        for(DataSnapshot postSnapshot : snapshot.getChildren())
                        {
                            Driver d = postSnapshot.getValue(Driver.class);
//                            int numFromDB = postSnapshot.getValue(int.class);
//                            String test = " Count is on "+numFromDB;
                            String details = "Name : "+d.getName()+"\nLatitude : "+d.getLat()+"\nLongitude : "+d.getLon();
                            textViewPersons.setText(details);
                        }
                    }

                    @Override
                    public void onCancelled(FirebaseError firebaseError) {
                        System.out.println("The read failed: " + firebaseError.getMessage());

                    }
                });
            }
        });



    }
}

Here is the Driver class

package com.example.mick.drivers6;

/**
 * Created by Mick on 13/02/2017.
 */

public class Driver {
    private String name,lat,lon;

    public Driver() {
    }

    public String getLat() {
        return lat;
    }

    public void setLat(String lat) {
        this.lat = lat;
    }

    public String getLon() {
        return lon;
    }

    public void setLon(String lon) {
        this.lon = lon;
    }

    public String getName() {
        return name;
    }

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

The data will upload to the database on firebase but then the app will crash and I get the error

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.mick.drivers6, PID: 2936
                  com.firebase.client.FirebaseException: Failed to bounce to type
                      at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:185)
                      at com.example.mick.drivers6.MainActivity$1$1.onDataChange(MainActivity.java:68)
                      at com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:45)
                      at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
                      at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:135)
                      at android.app.ActivityThread.main(ActivityThread.java:5254)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                   Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class com.example.mick.drivers6.Driver] from String value; no single-String constructor/factory method
                      at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator._createFromStringFallbacks(StdValueInstantiator.java:428)
                      at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromString(StdValueInstantiator.java:299)
                      at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromString(BeanDeserializerBase.java:1056)
                      at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:136)
                      at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:123)
                      at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
                      at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2034)
                      at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:183)
                      at com.example.mick.drivers6.MainActivity$1$1.onDataChange(MainActivity.java:68) 
                      at com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:45) 
                      at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45) 
                      at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38) 
                      at android.os.Handler.handleCallback(Handler.java:739) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:135) 
                      at android.app.ActivityThread.main(ActivityThread.java:5254) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:372) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
Application terminated.

When i click export JSON on what has been added to the database on firebase I get this

{
  "lat" : "66",
  "lon" : "85",
  "name" : "mickjj"
}

Thanks

AL.
  • 36,815
  • 10
  • 142
  • 281
AnonymousAlias
  • 1,149
  • 2
  • 27
  • 68
  • check it out http://stackoverflow.com/questions/35239210/jsonmappingexception-can-not-instantiate-value-of-type-from-integral-number-no – Emil Hotkowski Feb 14 '17 at 15:05
  • Please post the JSON (a text, no screenshot) that you have under the ref that you're trying to read from. Without that all we can say is: http://stackoverflow.com/questions/32108969/why-do-i-get-failed-to-bounce-to-type-when-i-turn-json-from-firebase-into-java/32109104#32109104 – Frank van Puffelen Feb 14 '17 at 15:20
  • Well when I click export JSON on what it has added i get this { "lat" : "66", "lon" : "85", "name" : "mickjj" } I will add to the question now – AnonymousAlias Feb 14 '17 at 15:23
  • @FrankvanPuffelen I notice you answer a lot of Firebase questions, can you help me out with this one I can't find the problem at all. – AnonymousAlias Feb 22 '17 at 18:03
  • Never mind got it sorted, thanks for the help earlier on – AnonymousAlias Feb 22 '17 at 19:09

2 Answers2

1

You are storing an instance of Driver at location ref. You are then adding a listener to that location and expecting to find children that are instances of Driver. Instead just use the snapshot value: snapshot.getValue(Driver.class).

Or if you intend to have multiple drivers, add them with unique keys: ref.push().setValue(d). Then your listener logic to scan over the children will work.

If this is new development, you should also consider using the new Firebase SDK instead of the legacy SDK. Reference the Upgrade Guide.

Bob Snyder
  • 37,759
  • 6
  • 111
  • 158
  • I tried replacing ref.child("Driver").setValue(d); with ref.push().setValue(d) just now but got the same error? I will need to have mulitple drivers. Any idea why this isnt working? – AnonymousAlias Feb 14 '17 at 16:13
  • @MickO'Gorman: The code you posted does not show `ref.child("Driver")`. If your want to include that path, use `ref.child("Driver").push().setValue(d)`. It will also be necessary then the attach the listener to `ref.child("Driver")`. – Bob Snyder Feb 14 '17 at 16:17
  • Sorry was looking at code on computer, so i replaced ref.setValue(d); with ref.child("Driver").push().setValue(d); but still have the same error – AnonymousAlias Feb 14 '17 at 16:22
  • @MickO'Gorman: See my updated comment. Add the listener to `ref.child("Driver")`. – Bob Snyder Feb 14 '17 at 16:24
  • Hey, I am sorry that didn't actually work I thought the problem was with the adding to the database but it is the 'Driver d = postSnapshot.getValue(Driver.class);' that is making the program crash, can you please offer some help this I cant find the problem at all. – AnonymousAlias Feb 22 '17 at 18:00
0

You probably need to add a default empty constructor to your Driver.class:

public Driver() {

}

This will enable reading the class from the database as you try to do in the line that causes the error.

Cuculus
  • 166
  • 1
  • 2
  • 10