Java 有人能解释为什么JUnit测试会出现错误吗?
我正在写一段涉及矩阵的代码。我的ArrayMatrix代码编译时没有任何错误,但是当我尝试对代码运行JUnit测试时,我得到了一个错误。在这一点上,我不确定我是否做得对Java 有人能解释为什么JUnit测试会出现错误吗?,java,multidimensional-array,junit,Java,Multidimensional Array,Junit,我正在写一段涉及矩阵的代码。我的ArrayMatrix代码编译时没有任何错误,但是当我尝试对代码运行JUnit测试时,我得到了一个错误。在这一点上,我不确定我是否做得对 public class ArrayMatrix extends AbstractMatrix{ private double[][] elements; public ArrayMatrix(final int rows, final int columns) throws MatrixException{
public class ArrayMatrix extends AbstractMatrix{
private double[][] elements;
public ArrayMatrix(final int rows, final int columns) throws MatrixException{
// Initialise a new matrix with all the elements set to 0.0
if (rows < 0 || columns < 0) {
throw new MatrixException("Negative rows or columns are not allowed");
}
double[][] elements = new double[rows][columns];
int i,j;
for (i=0;i<rows;i++) {
for (j=0;j<columns;j++) {
elements[i][j]= 0.0;
}
}
}
public ArrayMatrix(double[][] content) throws MatrixException{
// Initialise a new matrix storing the data provided by the
// double[][] parameter. Note the data should be copied.
int rows = elements.length;
int columns = elements[0].length;
int i,j;
for (i=0;i<rows;i++) {
for(j=0;j<columns;j++) {
content[i][j] = elements[i][j];
}
}
}
public int getNumberOfRows(){
// Number of rows in matrix
int noRows = elements.length;
return noRows;
}
public int getNumberOfColumns(){
// Number of columns in matrix
int noColumns = elements[0].length;
return noColumns;
}
public double getElement(final int row, final int column) throws MatrixException{
// Return the element at the specified position or throw an exception
if (elements.length<=row) {
throw new MatrixException("Attempt to access invalid element ("+row+","+column+")");
}
if (elements[0].length<column){
throw new MatrixException("Attempt to access invalid element ("+row+","+column+")");
}
else {return elements[row][column];}
}
public void setElement(final int row, final int column, final double value) throws MatrixException{
// Set the element at the specified position or throw an exception
if (elements.length<=row) {
throw new MatrixException("Attempt to access invalid element ("+row+","+column+")");}
if (elements[0].length<column){
throw new MatrixException("Attempt to access invalid element ("+row+","+column+")");}
else {elements[row][column] = value;}
}
}
我是否错误地编写了JUnit测试 方法中的代码抛出一个
NullPointerException
。这是因为您没有在构造函数中初始化MatrixArray的元素
数组,它保持为null
,并且在尝试访问null的属性(length
)时引发异常
相反,您创建了一个新的局部变量,其double[]elements=newdouble[rows][columns]构造函数中的代码>,这可能不是预期的
将该行替换为对MatrixArray字段的引用(在构造函数中):this.elements=new double[rows][columns]代码>。现在,元素
数组已初始化,您应该能够毫无例外地访问其字段
作为旁注,您的构造函数中有一个类似的问题ArrayMatrix(double[]][])
:您也应该看看这个问题。首先,“什么错误”?导致这些“错误”的最小代码是什么?我怀疑一个问题是名为elements
的字段没有初始化,因为它在ArrayMatrix(final int rows,final int columns)
ctor中被隐藏。未测试的ArrayMatrix(double[][]内容)
也可能访问所述未初始化字段。编译期间没有错误,但是JUnit测试失败,表明第35行(getNumberOfRows部分)有错误
@Test
public void testGetNumberOfRows() throws MatrixException {
ArrayMatrix a = new ArrayMatrix(2, 2);
int output = a.getNumberOfRows();
assertEquals(2,output);