0

i'm new in here and this firebase usage in android.. i'm using android studio and this is the error log..

Process: dioobanu.yahoo.locchatting, PID: 32156
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
    at dioobanu.yahoo.locchatting.MainActivity$1.onDataChange(MainActivity.java:57)
    at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.0.5:75)
    at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.0.5:63)
    at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.0.5:55)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:7007)
    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:1404)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

i think the first error might in my MainActivity here :

reference.addValueEventListener(new ValueEventListener(){

        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            User user = dataSnapshot.getValue(User.class);
            username.setText(user.getUsername());
            if (user.getImageURL().equals("default")) {
                profile_image.setImageResource(R.mipmap.ic_launcher);
            } else {
                Glide.with(MainActivity.this).load(user.getImageURL()).into(profile_image);
            }
        }

something is wrong in line 57 with : User user = dataSnapshot.getValue(User.class);

Any suggestions please?

btw this is my data structure in firebase..

Data Structure on Firebase

Dioo B.
  • 1
  • 1
  • Possible duplicate https://stackoverflow.com/q/218384/6818446 – S. Czop Jan 29 '19 at 01:52
  • How is `User` created/initialized? Because at runtime it seems to be `null`. – Frank van Puffelen Jan 29 '19 at 02:24
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Kartik Jan 29 '19 at 02:29
  • Please add your database structure and show us how you initialized the `reference` object. Please also responde with @AlexMamo – Alex Mamo Jan 29 '19 at 08:35

2 Answers2

0

Thanks guys for your attention.. i'm very appreciated it.. i'll try all your suggestions.. i can't explain a lot.. the rest of the source code is in this vid :

https://www.youtube.com/watch?v=LyAmpfm4ndo&index=3&list=PLzLFqCABnRQftQQETzoVMuteXzNiXmnj8

i want to learn this code for autologin from that vid in chatting app, but it seems everyone included me face the same error in this vid. This is the MainActivity :

public class MainActivity extends AppCompatActivity {

CircleImageView profile_image;
TextView username;

FirebaseUser firebaseUser;
DatabaseReference reference;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle("");

    profile_image = findViewById(R.id.profile_image);
    username = findViewById(R.id.username);

    firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
    reference = FirebaseDatabase.getInstance().getReference("Users").child(firebaseUser.getUid());

    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            User user = dataSnapshot.getValue(User.class);
            username.setText(user.getUsername());
            if (user.getImageURL().equals("default")) {
                profile_image.setImageResource(R.mipmap.ic_launcher);
            } else {
                Glide.with(MainActivity.this).load(user.getImageURL()).into(profile_image);
            }
        }


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

        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case R.id.logout:
            FirebaseAuth.getInstance().signOut();
            startActivity(new Intent( MainActivity.this, StartActivity.class));
            finish();
            return true;
    }
    return false;
}

}

And this is the User.class :

public class User {

private String id;
private String username;
private String imageURL;

public User(String id, String username, String imageURL){
    this.id = id;
    this.username = username;
    this.imageURL = imageURL;
}

public User(){
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getImageURL() {
    return imageURL;
}

public void setImageURL(String imageURL) {
    this.imageURL = imageURL;
}

}

i think there is something missing in the line that i mention in my question in MainActivity...

User user = dataSnapshot.getValue(User.class);

i'm new in android program.. so many thing that i don't understand the meaning.. and sory if i'm wrong with the answer b'cuz i'm new in this stackoverflow..

Dioo B.
  • 1
  • 1
0

Since you are already on the Uid node of your database structure so you don't have to load your model to fill data.

Try this:

reference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        //User user = dataSnapshot.getValue(User.class);
        String username = dataSnapshot.child("username").getValue().toString();
        String imageUrl = dataSnapshot.child("imageURL").getValue().toString();
        username.setText(username);
        if (imageUrl.equals("default")) {
            profile_image.setImageResource(R.mipmap.ic_launcher);
        } else {
            Glide.with(MainActivity.this).load(imageUrl).into(profile_image);
        }
    }
Ujjwal Jung Thapa
  • 604
  • 2
  • 8
  • 31