开发者

Java newbie having problem with null point exception

开发者 https://www.devze.com 2023-01-22 11:25 出处:网络
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 Li

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


I answered this in your other question: First year prgorammer needs help with a nullpointer exception in java

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


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

private LinkedList values =  new LinkedList();


You never instantiate your class LinkedList.

Change this line to:

private LinkedList values = new LinkedList();
0

精彩评论

暂无评论...
验证码 换一张
取 消