提问人:dee 提问时间:8/9/2023 最后编辑:Frank van Puffelendee 更新时间:8/9/2023 访问量:48
检索 Parcelable 对象“java.lang.Class”、“java.lang.Object.getClass()”时出现空指针异常,即使该对象存在于数据库中
Null pointer exception when retrieving Parcelable Object 'java.lang.Class java.lang.Object.getClass()' even though said object exists inside Database
问:
嗨,当我尝试从数据库中检索数据时,我的应用程序突然返回空指针。我用模型输入数据,然后用相同的模型检索它,但它抛出空指针异常,说一个对象是空的,即使它存在。
问题发生在模型对象内部,而不是活动文件内部。
这是出现问题的日记模型:
public class Diary implements Parcelable {
@SerializedName("ID")
@Expose
private String ID;
@SerializedName("ID Anak")
@Expose
private String IDAnak;
@SerializedName("Nama Anak")
@Expose
private String NamaAnak;
//Other Declarations
.
.
.
@SerializedName("Anak")
@Expose
private Anak Anak;
//Other declarations
.
.
.
protected Diary(Parcel parcel){
this.ID = parcel.readString();
this.TanggalDiary = parcel.readString();
this.IDAnak = parcel.readString();
this.IDKamar = parcel.readString();
this.NIA = parcel.readString();
this.NamaAnak = parcel.readString();
**this.Anak = parcel.readParcelable(Anak.getClass().getClassLoader()); //Here is where the problem is**
this.NamaKamar = parcel.readString();
this.IDAyah = parcel.readString();
this.NamaAyah = parcel.readString();
this.IDIbu = parcel.readString();
this.NamaIbu = parcel.readString();
this.Sarapan = parcel.readString();
this.Susu = parcel.readString();
this.SnackPagi = parcel.readString();
this.MakanSiang = parcel.readString();
this.TidurSiang = parcel.readString();
this.SnackSore = parcel.readString();
this.MakanSore = parcel.readString();
this.BAB = parcel.readString();
this.MinumObat = parcel.readString();
this.Catatan = parcel.readString();
this.Photo = parcel.readString();
this.PhotoURL = parcel.readString();
}
@Override
public void writeToParcel(Parcel parcel, int i){
parcel.writeString(getID());
parcel.writeString(getTanggalDiary());
parcel.writeString(getIDAnak());
parcel.writeString(getIDKamar());
parcel.writeString(getNIA());
parcel.writeString(getNamaAnak());
parcel.writeParcelable(getAnak(), i);
parcel.writeString(getNamaKamar());
parcel.writeString(getIDAyah());
parcel.writeString(getNamaAyah());
parcel.writeString(getIDIbu());
parcel.writeString(getNamaIbu());
parcel.writeString(getSarapan());
parcel.writeString(getSusu());
parcel.writeString(getSnackPagi());
parcel.writeString(getMakanSiang());
parcel.writeString(getTidurSiang());
parcel.writeString(getSnackSore());
parcel.writeString(getMakanSore());
parcel.writeString(getBAB());
parcel.writeString(getMinumObat());
parcel.writeString(getCatatan());
parcel.writeString(getPhoto());
parcel.writeString(getPhotoURL());
}
public Diary() {
}
public Diary(String ID,String TanggalDiary, String IDAnak, String IDKamar,String NIA, String NamaAnak, Anak Anak, String NamaKamar,String NamaKamarSearch,
String IDAyah, String NamaAyah, String NamaAyahSearch, String IDIbu, String NamaIbu, String NamaIbuSearch,
String Sarapan, String Susu, String SnackPagi, String MakanSiang, String TidurSiang, String SnackSore,
String MakanSore,String BAB, String MinumObat, String Catatan, String Photo, String PhotoURL, String CreatedBy, String CreatedIP, String CreatedPosition,
String CreatedDate, String ModifiedBy, String ModifiedIP, String ModifiedPosition, String ModifiedDate,
boolean IsActive) {
this.ID = ID;
this.TanggalDiary = TanggalDiary;
this.IDAnak = IDAnak;
this.IDKamar = IDKamar;
this.NIA = NIA;
this.NamaAnak = NamaAnak;
this.Anak = Anak;
this.NamaKamar = NamaKamar;
this.NamaKamarSearch = NamaKamarSearch;
this.IDAyah = IDAyah;
this.NamaAyah = NamaAyah;
this.NamaAyahSearch = NamaAyahSearch;
this.IDIbu = IDIbu;
this.NamaIbu = NamaIbu;
this.NamaIbuSearch = NamaIbuSearch;
this.Sarapan = Sarapan;
this.Susu = Susu;
this.SnackPagi = SnackPagi;
this.MakanSiang = MakanSiang;
this.TidurSiang = TidurSiang;
this.SnackSore = SnackSore;
this.MakanSore = MakanSore;
this.BAB = BAB;
this.MinumObat = MinumObat;
this.Catatan = Catatan;
this.Photo = Photo;
this.PhotoURL = PhotoURL;
this.CreatedBy = CreatedBy;
this.CreatedIP = CreatedIP;
this.CreatedPosition = CreatedPosition;
this.CreatedDate = CreatedDate;
this.ModifiedBy = ModifiedBy;
this.ModifiedIP = ModifiedIP;
this.ModifiedPosition = ModifiedPosition;
this.ModifiedDate = ModifiedDate;
this.IsActive = IsActive;
}
public String getID() {
return ID;
}
public void setID(String ID) {
this.ID = ID;
}
public String getTanggalDiary() {return TanggalDiary; }
public void setTanggalDiary(String tanggalDiary) { this.TanggalDiary = tanggalDiary; }
public String getIDAnak() { return IDAnak; }
public void setIDAnak(String idAnak) { this.IDAnak = idAnak; }
public String getIDKamar() {return IDKamar; }
public void setIDKamar(String idKamar) { this.IDKamar = idKamar; }
public String getNIA() {return NIA;}
public void setNIA(String nia) { this.NIA = nia; }
public String getNamaAnak() {
return NamaAnak;
}
public void setNamaAnak(String NamaAnak) {
this.NamaAnak = NamaAnak;
}
public Anak getAnak() {return Anak;}
public void setAnak(Anak Anak) {this.Anak = Anak;}
public String getNamaAnakSearch() { return NamaAnakSearch; }
public void setNamaAnakSearch(String NamaAnakSearch) { this.NamaAnakSearch = NamaAnakSearch; }
public String getIDAyah() {return IDAyah;}
public void setIDAyah(String IDAyah) {this.IDAyah = IDAyah; }
public String getNamaAyah() {return NamaAyah;}
public void setNamaAyah(String NamaAyah) {this.NamaAyah = NamaAyah;}
public String getNamaAyahSearch() {return NamaAyahSearch;}
public void setNamaAyahSearch(String NamaAyahSearch) {this.NamaAyahSearch = NamaAyahSearch;}
public String getIDIbu() {return IDIbu; }
public void setIDIbu(String IDIbu) {this.IDIbu = IDIbu;}
public String getNamaIbu() {return NamaIbu = NamaIbu;}
public void setNamaIbu(String NamaIbu) {this.NamaIbu = NamaIbu;}
public String getNamaIbuSearch() {return NamaIbuSearch ;}
public void setNamaIbuSearch(String NamaIbuSearch) {this.NamaIbuSearch = NamaIbuSearch;}
public String getNamaKamar() {
return NamaKamar;
}
public void setNamaKamar(String NamaKamar) {
this.NamaKamar = NamaKamar;
}
public String getNamaKamarSearch() { return NamaKamarSearch; }
public void setNamaKamarSearch(String NamaKamarSearch) { this.NamaKamarSearch = NamaKamarSearch; }
public String getSarapan() {return Sarapan;}
public void setSarapan(String Sarapan) { this.Sarapan = Sarapan; }
public String getSusu() {return Susu;}
public void setSusu(String Susu) {this.Susu = Susu;}
public String getSnackPagi() { return SnackPagi; }
public void setSnackPagi(String SnackPagi) { this.SnackPagi = SnackPagi; }
public String getMakanSiang() { return MakanSiang; }
public void setMakanSiang(String MakanSiang) { this.MakanSiang = MakanSiang; }
public String getTidurSiang() { return TidurSiang; }
public void setTidurSiang(String TidurSiang) {this.TidurSiang = TidurSiang;}
public String getSnackSore() { return SnackSore; }
public void setSnackSore(String snackSore) { this.SnackSore = snackSore; }
public String getMakanSore() { return MakanSore; }
public void setMakanSore(String MakanSore) { this.MakanSore = MakanSore; }
public String getBAB() { return BAB; }
public void setBAB(String bab) {this.BAB = bab; }
public String getMinumObat() {return MinumObat;}
public void setMinumObat(String MinumObat) {this.MinumObat = MinumObat; }
public String getCatatan() { return Catatan; }
public void setCatatan(String catatan) { this.Catatan = catatan;}
public String getPhoto() {
return Photo;
}
public void setPhoto(String Photo) {
this.Photo = Photo;
}
public String getPhotoURL() {
return PhotoURL;
}
public void setPhotoURL(String PhotoURL) {
this.PhotoURL = PhotoURL;
}
public String getCreatedBy() {
return CreatedBy;
}
public void setCreatedBy(String CreatedBy) {
this.CreatedBy = CreatedBy;
}
public String getCreatedIP() {
return CreatedIP;
}
public void setCreatedIP(String CreatedIP) {
this.CreatedIP = CreatedIP;
}
public String getCreatedPosition() {
return CreatedPosition;
}
public void setCreatedPosition(String CreatedPosition) {
this.CreatedPosition = CreatedPosition;
}
public String getCreatedDate() {
return CreatedDate;
}
public void setCreatedDate(String CreatedDate) {
this.CreatedDate = CreatedDate;
}
public String getModifiedBy() {
return ModifiedBy;
}
public void setModifiedBy(String ModifiedBy) {
this.ModifiedBy = ModifiedBy;
}
public String getModifiedIP() {
return ModifiedIP;
}
public void setModifiedIP(String ModifiedIP) {
this.ModifiedIP = ModifiedIP;
}
public String getModifiedPosition() {
return ModifiedPosition;
}
public void setModifiedPosition(String ModifiedPosition) {
this.ModifiedPosition = ModifiedPosition;
}
public String getModifiedDate() {
return ModifiedDate;
}
public void setModifiedDate(String ModifiedDate) {
this.ModifiedDate = ModifiedDate;
}
public Boolean getIsActive() {
return IsActive;
}
public void setIsActive(Boolean IsActive) {
this.IsActive = IsActive;
}
@Override
public int describeContents(){ return 0;}
public static final Parcelable.Creator<Diary> CREATOR = new Parcelable.Creator<Diary>() {
@Override
public Diary createFromParcel(Parcel parcel) { return new Diary(parcel); }
@Override
public Diary[] newArray(int i) { return new Diary[i ]; }
};
@Override
public String toString() {
return this.NamaAnak; // What to display in the Spinner list.
}
}
这是 Logcat :
Throwable caught in onActivityCreated: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference: m.ki.onActivityCreated(:com.google.android.gms.dynamite_measurementdynamite@[email protected] (150400-0):44)
2023-08-09 06:44:15.606 5193-5193 AndroidRuntime com.example.sekolahadmin E FATAL EXCEPTION: main
Process: com.example.sekolahadmin, PID: 5193
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sekolahadmin/com.example.sekolahadmin.Presentation.Diary.DetailDiaryActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3598)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3770)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2202)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:236)
at android.app.ActivityThread.main(ActivityThread.java:8096)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:620)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1011)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
at com.example.sekolahadmin.Model.Diary.<init>(Diary.java:161)
at com.example.sekolahadmin.Model.Diary$1.createFromParcel(Diary.java:468)
at com.example.sekolahadmin.Model.Diary$1.createFromParcel(Diary.java:466)
at android.os.Parcel.readParcelable(Parcel.java:3284)
at android.os.Parcel.readValue(Parcel.java:3177)
at android.os.Parcel.readArrayMapInternal(Parcel.java:3575)
at android.os.BaseBundle.initializeFromParcelLocked(BaseBundle.java:292)
at android.os.BaseBundle.unparcel(BaseBundle.java:236)
at android.os.Bundle.getParcelable(Bundle.java:1001)
at android.content.Intent.getParcelableExtra(Intent.java:8288)
at com.example.sekolahadmin.Presentation.Diary.DetailDiaryActivity.initIntent(DetailDiaryActivity.java:89)
at com.example.sekolahadmin.Presentation.Diary.DetailDiaryActivity.onCreate(DetailDiaryActivity.java:51)
at android.app.Activity.performCreate(Activity.java:8142)
at android.app.Activity.performCreate(Activity.java:8114)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1308)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3571)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3770)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2202)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:236)
at android.app.ActivityThread.main(ActivityThread.java:8096)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:620)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1011)
我是否也应该包括我的 Anak.java 模型?
我不知道这里有什么问题,有问题的对象在那里,但 Android 认为它是空的
请帮助启发我
起初我认为问题出在 Anak .java Model 中,所以我修复了它的对象,但它抛出了同样的错误,我能做的最好的事情就是从日记中删除 Anak 对象并用不同的方式替换它,但如果可能的话,我想用 getParcelable 做对
答:
-1赞
dee
8/9/2023
#1
我想通了。
事实证明,我写了错误的语法,因为它应该是而不是readParcelable()
readParcelable(Anak.class.getClassLoader());
readParcelable(Anak.getClass().getClassLoader());
所以本质上它是(你的类).class.getClassLoader();
我希望这能帮助其他抛出此错误的人
评论
0赞
user207421
8/9/2023
如果您按照 27 年来被普遍接受的约定对变量和所有其他变量使用小写字母,则不会出现此问题。Anak
0赞
Diego Borba
8/11/2023
你是初学者的事实是可以理解的,但(不幸的是)Stack Overflow 对初学者来说可能非常残酷。SO 是有关计算机编程的信息库,因此问题必须与任何人相关,而不是专门针对您。我知道,自学非常困难,因为我也这样做,但你说:“互联网是我唯一可以问的地方,因为我的朋友不是程序员”,所以你可能需要与其他程序员建立联系并与他们交谈,而不是简单地在 Stack Overflow 上提问。
0赞
Diego Borba
8/11/2023
问题不在于问,而在于在哪里以及如何做,如果你愿意,我会随时提供帮助!:)
评论