使用 Arrays.asList 中的 get() 时的 NPE - 列表
NPE when using get() from Arrays.asList - List
我试图访问我的 Arrays.asList - 列表中的一个元素,但我得到了一个 NPE。
主要Class:
public class Game {
List<Cell> cells = null;
public Game () {
cells = new ArrayList<>();
cells.addAll(Arrays.asList(new Cell[80])); //Target: Add 80 additional list elements
}
public int getSelectedValue () {
return cells.get(10).value; //Here I get the NPE, but I need the 10th cell
}
}
子类细胞:
public class Cell {
int row;
int col;
int value;
}
如何实例化一个包含许多对象(需要 100 个)的列表并使用 get() 运算符访问这些元素?
为对象的字段请求 null 会抛出异常
您建立了一个包含 80 个槽的数组,这些槽能够指向 Cell
个对象。但是您从未创建任何 Cell
对象。所以所有 80 个槽都指向空,它们是 null
.
然后您将该空值数组添加到空 ArrayList
。仍然没有 Cell
个对象存在,所以 ArrayList
没有 Cell
个对象,只有空值。您确实创建了一个包含 80 个槽的 List
,但所有槽都是空的(空)。
然后您要求从列表的第 11 个位置(索引 10)引用对象。有第十一个插槽。但是第十一个插槽中没有对象引用。因此,您从列表中检索到了一个空值。
您尝试从该空值访问 Cell
中定义的 value
字段。但是你手头没有 Cell
对象。你手头只有一个空值。 null 没有任何意义。尝试访问一个字段(或调用一个方法)为空(无)是没有意义的。所以 Java 抛出 NullPointerException
.
验证您手头是否有对象引用而不是空值:
if( Objects.nonNull( eleventhCell ) ) {…}
使用早期访问 Java 16 定义记录,这是一种使您的 class.
的简化方法
record Cell ( int row, int col, int val ) {} ;
int initialCapacity = 80 ;
List< Cell > cells = new ArrayList<>( initialCapacity ) ;
for( int i = 1 ; i <= initialCapacity ; i ++ )
{
cells.add(
new Cell(
ThreadLocalRandom.current().nextInt( 1 , 100 ) ,
ThreadLocalRandom.current().nextInt( 1 , 100 ) ,
ThreadLocalRandom.current().nextInt( 1_000 , 10_000 )
)
);
}
Cell eleventhCell = cells.get( 10 ) ;
if( Objects.nonNull( eleventhCell ) ) // If in doubt, verify not null.
{
int eleventhVal = eleventhCell.val ;
}
顺便说一句,value
是 Java 中字段的糟糕名称。我们一直使用这个术语作为程序员。讨论“价值的价值”会很混乱。
总是在对象上调用函数之前添加空检查以避免 NPE
例如
public int getSelectedValue () {
if(cells !=null && cells.size() >0)
{
if(cells.get(10) !=null)
return cells.get(10).value;
}
else return null;
}
我试图访问我的 Arrays.asList - 列表中的一个元素,但我得到了一个 NPE。
主要Class:
public class Game {
List<Cell> cells = null;
public Game () {
cells = new ArrayList<>();
cells.addAll(Arrays.asList(new Cell[80])); //Target: Add 80 additional list elements
}
public int getSelectedValue () {
return cells.get(10).value; //Here I get the NPE, but I need the 10th cell
}
}
子类细胞:
public class Cell {
int row;
int col;
int value;
}
如何实例化一个包含许多对象(需要 100 个)的列表并使用 get() 运算符访问这些元素?
为对象的字段请求 null 会抛出异常
您建立了一个包含 80 个槽的数组,这些槽能够指向 Cell
个对象。但是您从未创建任何 Cell
对象。所以所有 80 个槽都指向空,它们是 null
.
然后您将该空值数组添加到空 ArrayList
。仍然没有 Cell
个对象存在,所以 ArrayList
没有 Cell
个对象,只有空值。您确实创建了一个包含 80 个槽的 List
,但所有槽都是空的(空)。
然后您要求从列表的第 11 个位置(索引 10)引用对象。有第十一个插槽。但是第十一个插槽中没有对象引用。因此,您从列表中检索到了一个空值。
您尝试从该空值访问 Cell
中定义的 value
字段。但是你手头没有 Cell
对象。你手头只有一个空值。 null 没有任何意义。尝试访问一个字段(或调用一个方法)为空(无)是没有意义的。所以 Java 抛出 NullPointerException
.
验证您手头是否有对象引用而不是空值:
if( Objects.nonNull( eleventhCell ) ) {…}
使用早期访问 Java 16 定义记录,这是一种使您的 class.
的简化方法record Cell ( int row, int col, int val ) {} ;
int initialCapacity = 80 ;
List< Cell > cells = new ArrayList<>( initialCapacity ) ;
for( int i = 1 ; i <= initialCapacity ; i ++ )
{
cells.add(
new Cell(
ThreadLocalRandom.current().nextInt( 1 , 100 ) ,
ThreadLocalRandom.current().nextInt( 1 , 100 ) ,
ThreadLocalRandom.current().nextInt( 1_000 , 10_000 )
)
);
}
Cell eleventhCell = cells.get( 10 ) ;
if( Objects.nonNull( eleventhCell ) ) // If in doubt, verify not null.
{
int eleventhVal = eleventhCell.val ;
}
顺便说一句,value
是 Java 中字段的糟糕名称。我们一直使用这个术语作为程序员。讨论“价值的价值”会很混乱。
总是在对象上调用函数之前添加空检查以避免 NPE
例如
public int getSelectedValue () {
if(cells !=null && cells.size() >0)
{
if(cells.get(10) !=null)
return cells.get(10).value;
}
else return null;
}