我对java很新,我试图在类中测试我的一些方法,但是我得到了NullPointerException
public class ArrayListTest {
private List ar;
@org.junit.Before
public void setUp() throws Exception {
List ar = new ArrayList();
}
@Test
public void testAdd() throws Exception {
System.out.println(ar);
***ar.add(33);***
错误指向ar.add(33);
java.lang.NullPointerException
at com.company.ArrayListTest.testAdd(ArrayListTest.java:23)
...a bunch of other invokes...
考虑添加方法,我找不到任何错误。
public class ArrayList implements List{
private Object[] elems;
private int nrElems;
public ArrayList() {
nrElems = 0;
elems = new Object[10];
}
public void add(Object e) {
System.out.println(e);
if (nrElems == elems.length) {
resize();
}
elems[nrElems++] = e;
}
有什么想法吗?测试类使用我自己的ArrayList
实现