0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

How would i go about fixing it so I don't have it coming back with null?
@Hovering First of all, why would you test assertEquals() on an invalid index? You know that will throw an exception, and since you expect that, you should assert for an exception.
I used the same test code before changing code from String and everything was working as intended. I didn't change anything in the test class and it comes back with the error? Why would it do that?
Thanks Rohit, it took me awhile to understand the difference between my test code while using strings and noticed when i changed my code over to generics i had changed the code to make it incorrect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.