空对象引用上的虚拟方法-Android 中的错误



我面临无法解决的空对象错误。我的应用程序编译得很好,但是当我使用"评级食物"功能时,应用程序崩溃并显示我链接的错误。我真的无法弄清楚为什么它应该是一个空对象,因为"Vorname"在用户注册时被保存。也许我想识别"Vorname"的方式是错误的吗? 如果您有任何想法,我应该改变什么,请告诉我。 谢谢,祝你有美好的一天!

如果您需要更多信息,请告诉我。

private UserProfil userProfil;

食物细节片段(只是代码的一部分(发生错误的地方((

private void showDialogRating() {
androidx.appcompat.app.AlertDialog.Builder builder=new androidx.appcompat.app.AlertDialog.Builder(getContext());
builder.setTitle("Rating Food");
builder.setMessage("Please fill in information");
View itemView=LayoutInflater.from(getContext()).inflate(R.layout.layout_rating,null);
Common.currentUser= userProfil;

RatingBar ratingBar=(RatingBar)itemView.findViewById(R.id.rating_bar);
EditText edt_comment=(EditText)itemView.findViewById(R.id.edt_comment);
builder.setView(itemView);
builder.setNegativeButton("CANCEL", (dialogInterface,i ) -> {
dialogInterface.dismiss();
});
builder.setPositiveButton("OK", (dialogInterface, i) -> {
CommentModel commentModel=new CommentModel();
commentModel.setName(Common.currentUser.getVorname());
commentModel.setUid(Common.currentUser.getUid());
commentModel.setComment(edt_comment.getText().toString());
commentModel.setRatingValue(ratingBar.getRating());
Map<String,Object> serverTimeStamp =new HashMap<>();
serverTimeStamp.put("timeStamp", ServerValue.TIMESTAMP);
commentModel.setCommentTimeStamp(serverTimeStamp);
foodDetailViewModel.setCommentModel(commentModel);
});
AlertDialog dialog=builder.create();
dialog.show();
}

用户普罗非

public class UserProfil {
public String vorname;
public String nachname;
public String strasse;
public String hnr;
public String plz;
public String stadt;
public String land;
public String uid;
public UserProfil(){
}

public UserProfil(String Vorname, String Nachname,String Strasse,String HNr,String Plz,String Stadt,String Land,String Uid) {
this.vorname = Vorname;
this.nachname = Nachname;
this.strasse= Strasse;
this.hnr= HNr;
this.plz=Plz;
this.stadt=Stadt;
this.land=Land;
this.uid=Uid;
}
public String getVorname() {
return vorname;
}
public void setVorname(String vorname) {
this.vorname = vorname;
}
public String getNachname() {
return nachname;
}
public void setNachname(String nachname) {
this.nachname = nachname;
}
public void setStrasse(String strasse) {
this.strasse = strasse;
}
public String getStrasse() {
return strasse;
}
public void setHnr(String hnr) {
this.hnr = hnr;
}
public String getHnr() {
return hnr;
}
public void setPlz(String plz) {
this.plz = plz;
}
public String getPlz() {
return plz;
}
public void setStadt(String stadt) {
this.stadt = stadt;
}
public String getStadt() {
return stadt;
}
public void setLand(String land) {
this.land = land;
}
public String getLand() {
return land;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getUid() {
return uid;
}

通用爪哇类

public class Common {
public static final String USER_PREFERENCES="Users";
public static final String POPULAR_CATERGORY_REF = "MostPopular";
public static final String BEST_DEALS_REF = "BestDeals";
public static final int DEFAULT_COLUMN_COUNT = 0;
public static final int FULL_WIDTH_COLUMN = 1;
public static final String CATEGORY_REF = "Category";
public static final String COMMENT_REF = "Comments";
public static UserProfil currentUser;
public static CategoryModel categorySelected;
public static FoodModel selectedFood;
}

日志猫错误

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.login.UserProfil.getVorname()' on a null object reference
at com.example.login.ui.fooddetail.FoodDetailFragment.lambda$showDialogRating$1$FoodDetailFragment(FoodDetailFragment.java:95)
at com.example.login.ui.fooddetail.-$$Lambda$FoodDetailFragment$Wv1d4NkSDd8GTXyopN5Pa6I5Nkk.onClick(lambda)
at androidx.appcompat.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:234)
at android.app.ActivityThread.main(ActivityThread.java:5526)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

也许你忘记初始化userProfil属性了。

测试一下:

private UserProfil userProfil = new UserProfil();

相关内容

  • 没有找到相关文章

最新更新