[英]Java: No exception message error
使用BlueJ,對Java來說還是新手。 運行測試時出現問題,提示“沒有異常消息”。 我不知道在哪里可以查看我的代碼來解決此問題。 所以這是我到目前為止所擁有的:
主班
public class LList<X>
{
private Node<X> head;
private int length = 0;
public int size()
{
return length;
}
public void add(X item)
{
Node a = new Node();
a.setValue(item);
a.setLink(head);
head = a;
length ++;
}
public X get(int index)
{
X holder = null;
Node<X> h = head;
if(index > length)
{
throw new IndexOutOfBoundsException();
}
else
{
for(int i = 0; i < index + 1; i++)
{
h = h.getLink();
holder = h.getValue();
}
return holder;
}
}
}
下一班
public class Node<X>
{
private X value;
private Node link;
public X getValue()
{
return value;
}
public void setValue(X v)
{
value = v;
}
public void setLink(Node l)
{
link = l;
}
public Node getLink()
{
return link;
}
}
測試班
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class LListTest
@Test
public void testGet()
{
LList x = new <String>LList();
x.add("hi");
assertEquals("hi", x.get(0));
x.add("1hi");
assertEquals("hi", x.get(1));
assertEquals("1hi", x.get(0));
x.add("2hi");
assertEquals("hi", x.get(2));
assertEquals("1hi", x.get(1));
assertEquals("2hi", x.get(0));
x.add("3hi");
assertEquals("hi", x.get(3));
assertEquals("1hi", x.get(2));
assertEquals("2hi", x.get(1));
assertEquals("3hi", x.get(0));
x.add("4hi");
assertEquals("hi", x.get(4));
assertEquals("1hi", x.get(3));
assertEquals("2hi", x.get(2));
assertEquals("3hi", x.get(1));
assertEquals("4hi", x.get(0));
}
如果有任何想法,我將不勝感激,無論是解釋我的代碼在哪里存在問題,還是解釋為什么立即收到錯誤將是很棒的解釋。
當您嘗試訪問無效索引時,將引發異常:
throw new IndexOutOfBoundsException();
沒有任何消息。 因此,包裝在其中的異常消息將為null
。 然后下面的assertEquals()
調用:
assertEquals("hi", x.get(4));
將失敗,因為x.get(4)
將引發異常,但message將為null
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.