简体   繁体   中英

Java newbie having problem with null point exception

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

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();

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.

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