【问题标题】:Attempt to invoke virtual method 'java.lang.String Model.Users.getName()' on a null object reference尝试在空对象引用上调用虚拟方法“java.lang.String Model.Users.getName()”
【发布时间】:2020-06-16 14:34:04
【问题描述】:

我有这个错误:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String Model.Users.getName()' on a null object reference

这是上面的代码

TextView userNameTextView = headerView.findViewById(R.id.user_profile_name);
CircleImageView profileImageView = headerView.findViewById(R.id.user_profile_image);

我在接下来的几行中得到了错误:

userNameTextView.setText(Prevalent.currentonlineUser.getName());    
Picasso.get().load(Prevalent.currentonlineUser.getImage()).placeholder(R.mipmap.profile).into(profileImageView);

我不知道为什么,因为我初始化了它们。我看到有其他帖子出现此错误,但我不知道要在我的代码中更改什么才能正常。另外,我有这个类,其中有构造函数、get 和 set 方法;

package Model;

public class Users {
    private String name="",image="";

    public Users()
    {

    }

    public Users(String name,String image) {
        this.name = name;
        this.image = image;
    }

    public String getName() {
        return name;
    }

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


    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }


}

你能帮帮我吗? 提前致谢!

【问题讨论】:

  • 你能提供完整的堆栈跟踪吗?
  • Prevalent.currentonlineUser.getName() -> Prevalent.currentonlineUser 很可能是null
  • 是的,但我该如何解决?
  • 创建一个minimal reproducible example 并在此处发布。通过构建 minimal reproducible example,您可能已经发现了错误,但如果您不这样做,您可以在此处发布的代码将使其他人能够回答该问题。
  • 可以提供你的 AndroidManifest.xml 问题可能就在那里。我遇到了同样的问题并通过更改清单文件来解决它。

标签: java android-studio


【解决方案1】:

尝试使用 Glide.with(this).load().into();方法而不是毕加索。

【讨论】:

  • OP 出现空指针异常问题,更改图像加载库无法解决问题!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-25
  • 2017-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多