tags:

views:

44

answers:

3

Trying to use StackLL method size() is returning a null pointer error. I cannot figure out why this is, as count is initialized to 0. My only guess is that I am not properly creating an instance of LinkedList.java. However, I have no idea what I should do to correct this. Any help would be greatly appreciated.

The following code is a portion of a linked list implementation for a 1st year assignment, I have stripped out a lot of the code to focus on the problem areas. I cannot change LinkedList.java.

StackLL.java

public class StackLL implements Stack
{
    // The linked list that will contain the values in the stack
    private LinkedList values;

    public int size()
    {
        return values.size();
    }
}

LinkedList.java

public class LinkedList 
{
    Node head;
    int count;

    public LinkedList ()
    {
        head = null;
        count = 0;
    }

    public int size ()
    {
        return count;
    }
}
    private class Node
    {
        int value;
        Node next;

        Node()
        {
        }

        Node (int value)
        {
            this.value = value;
        }
    }
A: 

You are not initializing values. Do this in your StackLL:

private LinkedList values =  new LinkedList();
Thanks. The StackLL template I was provided with just listed "private LinkedList values", so I figured I wouldn't need to use that syntax.
@user477830:The fact that there was no constructor defined for StackLL in the template, should make you take "that syntax" into account ;)
A: 

You never instantiate your class LinkedList.

Change this line to:

private LinkedList values = new LinkedList();
Prine
A: 

I answered this in your other question: http://stackoverflow.com/questions/4057424/first-year-prgorammer-needs-help-with-a-nullpointer-exception-in-java/4057709#4057709

Please don't ask the same question twice, especially not twice in an hour.

Coronatus