[英]Java newbie having problem with null point exception
Trying to use StackLL method size() is returning a null pointer error. 尝试使用StackLL方法size()返回空指针错误。 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.
由于count初始化为0,因此我无法弄清为什么。我唯一的猜测是我没有正确创建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.
我无法更改LinkedList.java。
StackLL.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 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:
在您的StackLL中执行以下操作:
private LinkedList values = new LinkedList();
You never instantiate your class LinkedList . 您永远不会实例化类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 我在另一个问题中回答了这个问题: 第一年的prgorammer需要Java中的nullpointer异常的帮助
Please don't ask the same question twice, especially not twice in an hour. 请不要问同样的问题两次,特别是一个小时内不要问两次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.