0

Got these errors in Run logcat: E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.shikh.regie, PID: 19366 com.google.firebase.database.DatabaseException: Failed to parse node with class class com.example.shikh.regie.User at com.google.android.gms.internal.zzamm.zza(Unknown Source) at com.google.android.gms.internal.zzamm.zzbt(Unknown Source) at com.google.android.gms.internal.zzamp.zzbu(Unknown Source) at com.google.firebase.database.DatabaseReference.setValue(Unknown Source) at com.example.shikh.regie.MainActivity$1.onClick(MainActivity.java:40)

Dont know whats the problem.why it is not able to parse the node

MainActivity.java

public class MainActivity extends AppCompatActivity {
private EditText name,email,phone;
private Button btn;
private DatabaseReference mDatabse;
private String name1,email1,phone1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mDatabse = FirebaseDatabase.getInstance().getReference().child("AppUsers");

    name = (EditText) findViewById(R.id.editText);
    email = (EditText) findViewById(R.id.editText2);
    phone = (EditText) findViewById(R.id.editText3);

    name1 = name.getText().toString();
    email1 = email.getText().toString();
    phone1 = phone.getText().toString();
    btn = (Button) findViewById(R.id.button);
         btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Map<String, User> users = new HashMap<String, User>();
            DatabaseReference ref=mDatabse.child("AppUsers");
            mDatabse.setValue(email1,new User(name1,email1,phone1));
            mDatabse.setValue(users);
        }
    });

}
}

User.java

package com.example.shikh.regie;


public class User {
String name,email,phone;

public User(){}

public User(String name, String email, String phone) {
    this.name = name;
    this.email = email;
    this.phone = phone;
}

public String getName() {
    return name;
}

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

public String getEmail() {
    return email;
}

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

public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}
 }
Shikhar Singh
  • 112
  • 1
  • 11
  • Possible duplicate of [com.firebase.client.FirebaseException: Failed to parse node with class class CLASS\_NAME android](http://stackoverflow.com/questions/34538330/com-firebase-client-firebaseexception-failed-to-parse-node-with-class-class-cla) – PsyGik Nov 20 '16 at 14:12
  • @PsyGik the question you linked called `updateValues()`, which this question doesn't. So while the error message is the same in both, I doubt it's the same cause. – Frank van Puffelen Nov 20 '16 at 14:21
  • No Duplicate class @PsyGik – Shikhar Singh Nov 20 '16 at 14:23

1 Answers1

1

This code will not work:

mDatabse.setValue(email1,new User(name1,email1,phone1));

The only [matching overload of setValue()](https://firebase.google.com/docs/reference/android/com/google/firebase/database/DatabaseReference.html#setValue(java.lang.Object, java.lang.Object)) is:

public Task<Void> setValue (Object value, Object priority)

Set the data and priority to the given values.

In this overload the second argument is a priority value, which has to be a primitive object.

It seems more likely that you're looking to do:

mDatabse.child("email1").setValue(new User(name1,email1,phone1));

This will set the new user as node email1 under the reference.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • i want the parent node should b named as the user's email...each user will have his own record under his parent node a his email – Shikhar Singh Nov 20 '16 at 14:29
  • For that you'd specify the email address instead of `email1` in the above snippet. Just be sure to encode the email, since `.` is not allowed in Firebase keys. The common way is to replace the `.` by a `,`. – Frank van Puffelen Nov 20 '16 at 14:46
  • Note: email addresses can contain many characters which are not allowed in a firebase key. Also using email as key does not allow users to change their email address. To solve both I store users using a UUID. On start of my application, I just load all users in memory in a hash map by email address. – bluevoid Nov 20 '16 at 15:06
  • @bluevoid The set of disallowed characters in Firebase keys is quite small and most of those cannot occur on an email address. The dot `.` is really the only one we encode as far as I know. Loading all users into memory may work when you app starts, but becomes a scalability bottleneck if/when you have thousands/millions of users. – Frank van Puffelen Nov 20 '16 at 18:14
  • Ok, it is not really bigset: not allowed in firebase fieldname: .$[]#/ Allowed in email: !#$%&'*+-/=?^_`{|}~; and [] are allowed when between quotes. So to get complete coverage you need to replace all of them. See discusion here: http://grokbase.com/t/gg/firebase-talk/154q27rryc/firebase-no-dot-allowed-in-key-name-how-to-create-an-email-index. Their solution is to urlencode the email AND replace the dot. Loading all users will become a bottleneck when more than a few thousand, that is true. – bluevoid Nov 21 '16 at 10:44
  • Actually it is storing the values..but deletes it after 5 or 6 secs.. I've used name instead of email – Shikhar Singh Nov 23 '16 at 05:33