简体   繁体   中英

Java: No exception message error

Using BlueJ, and still new to 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 . And then the following assertEquals() call:

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

will fail as, x.get(4) will throw an exception, but message will be null .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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