-2

On my JUnitTest I am getting Java.Lang.NullPointerException error, what does this mean? Is my clear() method wrong?

Here are my fields:

private static class Node<E> {

   /** The data value. */
    private E data;
    /** The link */
    private Node<E> next = null;

    /**
     * Construct a node with the given data value and link
     * @param data - The data value 
     * @param next - The link
     */
    public Node(E data, Node<E> next) {
        this.data = data;
        this.next = next;
    }

    /**
     * Construct a node with the given data value
     * @param data - The data value 
     */
    public Node(E data) {
        this(data, null);
    }
}
/*</listing>*/
// Data fields
/** A reference to the head of the list */
private Node<E> head = null;
/** The size of the list */
private int size = 0;    

This is my clear() method:

/**
 * Removes all of the elements from this list (optional operation). 
 * The list will be empty after this call returns
 * @throws UnsupportedOperationException if the clear operation is not supported by this list
 */
public void clear() {

    Node<E> temp = head;
    for(int i=0; i < size; i++){
        if(temp != null){
            temp.data = null;
            temp = temp.next;
        }
    }   
}

Here is my test method for clear():

public void testClear() {
    multipleItems.clear();
    assertEquals("", multipleItems.toString());
}

Stack trace?

java.lang.NullPointerException
at SingleLinkedList.toString(SingleLinkedList.java:196)
at SingleLinkedListTest.testClear(SingleLinkedListTest.java:93)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

java.lang.NullPointerException
at SingleLinkedList.toString(SingleLinkedList.java:196)
at SingleLinkedListTest.testClear(SingleLinkedListTest.java:93)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

toString Method: Error on line "sb.append(p.data.toString());"

public String toString() {
    StringBuilder sb = new StringBuilder("[");
    Node p = head;
    if (p != null) {
        while (p.next != null) {
            sb.append(p.data.toString());
            //sb.append(" ==> ");
            sb.append(", ");
            p = p.next;
        }
        sb.append(p.data.toString());
    }
    sb.append("]");
    return sb.toString();
}
  • multipleItems is null.. why not do a null check befor calling clear? – TheLostMind Mar 10 '14 at 06:08
  • 3
    possible duplicate of [What is a Null Pointer Exception?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception) – Brian Roach Mar 10 '14 at 06:09
  • For future reference, when asking questions about debugging exceptions, you'll want to post a stack trace and clearly identify the relevant lines in code snippets. – Jason C Mar 10 '14 at 06:11
  • okay sorry about that. – user3321427 Mar 10 '14 at 06:12
  • 1
    @WhoAmI That's one option, but if `multipleItems != null` is supposed to be an invariant, the fundamental problem would be somewhere else. There's not enough info to determine if a simple check is appropriate. – Jason C Mar 10 '14 at 06:12
  • 1
    @JasonC - I agree.. I was merely suggesting that if there was a chance of multipleItems being null at any point, its better to put a null check before calling any method on it.. Any static code analysis tool would also suggest the same. :) – TheLostMind Mar 10 '14 at 06:14
  • i'm not following what you guys mean, sorry i'm a newbie programmer. – user3321427 Mar 10 '14 at 06:17
  • @user3321427 Post the stack trace!!! – Jason C Mar 10 '14 at 06:18
  • whats the stack trace? – user3321427 Mar 10 '14 at 06:20
  • Jason I edited my post, is that the stack trace? – user3321427 Mar 10 '14 at 06:24
  • @user3321427 Yes; and given that it clearly identifies `SingleLinkedList.toString(SingleLinkedList.java:196)` (which you did not include in your snippets) as the source line, I suggest you check there. – Jason C Mar 10 '14 at 06:30
  • When I click that it goes to my toString method, which was given to me by my professor which I don't think I should be editting. – user3321427 Mar 10 '14 at 06:32
  • I put my toString method and the error is coming from sb.append(p.data.toString()); – user3321427 Mar 10 '14 at 06:34
  • @user3321427 Then one or more of `sb`, `p`, or `p.data` is `null`. Figure out which (run it in a debugger or print out their values or just examine your code closely) then find out why. I suggest you read the linked thread http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception. – Jason C Mar 10 '14 at 06:40
  • Also, there are two lines in that method that read `sb.append(p.data.toString());` -- which is why I said *clearly* identify relevant lines in the snippet. When asking others for help, please don't make them pull teeth to get information. – Jason C Mar 10 '14 at 06:42

2 Answers2

0

NullPointerExceptions hint to null objects, so multipleItems is most possible null when you call clear() on it.

Smutje
  • 17,733
  • 4
  • 24
  • 41
-1

I would suggest you have a check like

if(temp.hasNext) {
temp = temp.next
}
Hirak
  • 3,601
  • 1
  • 22
  • 33
  • 1
    I do have a check like that, thats basically my if statement, which says if temp != null, then make the data null then move one to temp.next then repeats it again and check if temp does not equal null – user3321427 Mar 10 '14 at 06:15
  • I don't see how that would help; he already checks if `temp != null`, which happens just after `temp = temp.next` when `temp.next` is `null`; so he's already prepared to handle that case. Plus, although it's not relevant to this snippet, in a different scenario that could easily break logic that relies on `temp` ending up `null` at the end of an iteration. – Jason C Mar 10 '14 at 06:16