简体   繁体   English

Java:无异常消息错误

[英]Java: No exception message error

Using BlueJ, and still new to java. 使用BlueJ,对Java来说还是新手。 I'm having issues when I run my test it comes back stating there is "no exception message". 运行测试时出现问题,提示“没有异常消息”。 I don't know where to look through my code to fix this problem. 我不知道在哪里可以查看我的代码来解决此问题。 So here is what I have so far: 所以这是我到目前为止所拥有的:

Main class 主班

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;
        }

    }
}

Next Class 下一班

 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;
    }    
}

Test Class 测试班

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));
    }

If there are any ideas I'd greatly appreciate it, whether thats explaining where in my code the issue lies or an explanation on why im getting the error would be awesome. 如果有任何想法,我将不胜感激,无论是解释我的代码在哪里存在问题,还是解释为什么立即收到错误将是很棒的解释。

When you try to access invalid index, you throw an exception: 当您尝试访问无效索引时,将引发异常:

throw new IndexOutOfBoundsException();

without any message. 没有任何消息。 So, the exception message wrapped in that will be null . 因此,包装在其中的异常消息将为null And then the following assertEquals() call: 然后下面的assertEquals()调用:

assertEquals("hi", x.get(4));

will fail as, x.get(4) will throw an exception, but message will be null . 将失败,因为x.get(4)将引发异常,但message将为null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM