![](/img/trans.png)
[英]Spring Data jpa (hibernate) nullpointer Exception when adding List of Entities
[英]Nullpointer exception when adding to list
我正在嘗試遍歷對象列表,以找出哪些對象在互相影響。 我現在用的是checkedGladiators
列表的東西來比較,以防止它檢查已經分配到戰斗的角斗士,因為每個s
即尚未分配將圍繞它們構建其整個作戰。 當前,我得到一個NullPointerException
,因此我使用了一些測試文本來證明它listContains(checkedGladiators,s)
發生在listContains(checkedGladiators,s)
。 我在前面添加了零件。 現在,“ Null”和“ Null Changed”之間也出現了問題,這對我來說毫無意義。
for (Gladiator s : gladiators){
if (checkedGladiators == null) {
System.out.println("Null");
combat1.add(s);
checkedGladiators.add(s);
System.out.println("Null Changed");
}
if (listContains(checkedGladiators,s)) {
// if gladiator is already in a combat do nothing
} else { // if he isn't
}
}
listContains類:
public boolean listContains(List<Gladiator> List, Gladiator search) {
for (Gladiator p : List) {
if (p.equals(search)) {
return true;
}
}
return false;
}
有誰知道為什么會這樣? 謝謝
編輯1:
public class Ai {
private List<Gladiator> gladiators;
private List<List<Gladiator>> combatsList;
private List<Gladiator> checkedGladiators;
private List<Gladiator> combat1;
private List<Gladiator> combat2;
private List<Gladiator> combat3;
private List<Gladiator> combat4;
private List<Gladiator> combat5;
private List<Gladiator> combat6;
private List<Gladiator> combat7;
private List<Gladiator> combat8;
private List<Gladiator> guardList;
private List<Gladiator> advanceList;
private List<Gladiator> retreatList;
int totalCombats = 0; // total combats going on
我已經在類中初始化了列表變量。
您忘記創建checkedGladiators對象。
因此,在循環之前創建對象為:
List <Gladiators> checkGladiators = new ArrayList <Gladiators>();
然后,在您的循環中,而不是測試checkGladiators == null ...
測試checkGladiators.isEmpty()。
if(checkedGladiators ==null)
為true,並且您要向其中添加一些內容,
它肯定會throw a NullPointerException
,因為您對null進行操作
謝謝阿比
為什么需要做所有這一切? 為什么這還不夠?
// This has your data in it.
List<Gladiators> gladiators = new ArrayList<Gladiators>();
// Obviously some attributes, including a unique key or name.
// MUST override equals and hashcode properly
Gladiator g = new Gladiator();
if (gladiators.contains(g)) {
// do something here.
}
NullPointerException
是最容易解決的問題之一。 在調試打開的情況下在IDE中運行代碼,並在堆棧跟蹤表明發生異常的地方放置一個斷點。 您將很快找出為什么您認為不應為空的某些東西違反了您的假設。
當checkedGladiators
為null
您嘗試將其添加(好像它是一個List / Collection / etc)。
...
if (checkedGladiators == null) {
...
checkedGladiators.add(s); // <-- You handle 'checkedGladiators'
// <-- as an instaciated, when it is 'null'
改為這樣做:
...
if (checkedGladiators == null) {
...
checkedGladiators = new ArrayList<...>(); // <-- instanciate appropriately
checkedGladiators.add(s);
...
與其使用System.out.println
進行檢查, System.out.println
使用StackTrace
或類似eclipse
的debugger
。 我會明確指出。
if (checkedGladiators == null) {
System.out.println("Null");
combat1.add(s);
checkedGladiators.add(s); --> checkedGladiators is null here. Here null pointer exception will occur.
System.out.println("Null Changed");
}
您檢查checkGladiators是否為null,然后在其上調用一個方法:
if (checkedGladiators == null) { // <-- null!!
checkedGladiators.add(s); // <-- null pointer exception.
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.