Home > Back-end >  Can't convert an object of type java.lang.String to type com.test.nmfestdemo.Info
Can't convert an object of type java.lang.String to type com.test.nmfestdemo.Info

Time:08-31

Activity code. After entering the Activity, I get an error that:

Can't convert an object of type java.lang.String to type com.test.nmfestdemo.Info

I tried to write differently, but then either nothing happened at all, or there was an error with null.

public class Account extends AppCompatActivity {
            private Button button1;
            private TextView textview1;
            private Intent i =new Intent();
        
            private String User_KEY ="User";
        private DatabaseReference mDataBase;
        private FirebaseAuth mAuth;

    private DatabaseReference mDataBaseInfo;

    private String Info_KEY ="Info";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account);
        button1 = findViewById(R.id.button1);
        textview1 = (TextView) findViewById(R.id.textview1);

        mDataBase = FirebaseDatabase.getInstance().getReference(User_KEY);
        mDataBaseInfo = FirebaseDatabase.getInstance().getReference(Info_KEY);
        getDataFromDB();

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mAuth.signOut();
                i.setClass(getApplicationContext(),MainActivity.class);
                startActivity(i);
            }
        });



    }
    public void getDataFromDB(){
        ValueEventListener vListener = new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot postSnapshot) {

                for (DataSnapshot ds : postSnapshot.getChildren()) {
                    Info info = ds.getValue(Info.class);
                    textview1.setText(String.valueOf(info));
                }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        };
        mDataBaseInfo.addValueEventListener(vListener);
    }
}

Class code

public class Info {
    public String day;


    public Info(String day) {
        this.day = day;
    }

    public String getDay() {
        return day;
    }
}

Error code and FireBase structure

CodePudding user response:

If Info_KEY variable contains a String which value that is equal to "Info", then that expected behavior, to get the following error:

Can't convert object of type java.lang.String to type com.test.nmfestdemo.Info

And this is because, under the "Info" node, there are no Info objects. There is only a single field called day, which contains the String value of "1". So there is no way you can convert a String object into an object of type Info. If you want to read the value of day field, then you should remove the loop:

public void getDataFromDB(){
    ValueEventListener vListener = new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot postSnapshot) {
            Info info = postSnapshot.getValue(Info.class);
            textview1.setText(info.getDay());
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
            Log.d("TAG", error.getMessage()); //Never ignore potential errors!
        }
    };
    mDataBaseInfo.addValueEventListener(vListener);
}
  • Related