java 使用链表的 NullPointerException 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13282270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NullPointerException Error using linked lists
提问by TheNameHobbs
I just finished up working on this program and got it to compile but it breaks after user input and gives me this:
我刚刚完成了这个程序的工作并让它编译,但它在用户输入后中断并给了我这个:
Please input 0 or more values at keyboard 12 4 3 2 1
请在键盘上输入 0 个或多个值 12 4 3 2 1
Exception in thread "main" java.lang.NullPointerException
at Search.buildList(Search.java:41)
at Search.main(Search.java:10)
Here is the code:
这是代码:
import java.io.*;
import java.util.*;
public class Search {
public static void main(String argv[]) throws IOException {
Scanner stdin = new Scanner(System.in);
System.out.println("Please input 0 or more values at keyboard");
Node head = buildList();
System.out.println("Now printing list");
printList(head);
System.out.println("\nWhat key in list are you searching for? ");
int key = stdin.nextInt();
System.out.print("Your key was ");
if (search(head, key))
System.out.println("found.");
else
System.out.println("not found.");
}
private static void printList(Node head)
{
if (head != null)
{
System.out.print(head.getItem() + " ");
printList(head.getNext());
}
}
private static Node buildList() throws IOException
{
// Post : Inserts 0 or more numerical values from keyboard into list
// using the Scanner class and returns head of list
Scanner input = new Scanner(System.in);
Node head = null;
Node first = new Node(input.nextInt());
head.setNext(first);
while(input.hasNext())
{
insert(first, input.nextInt());
/*
Node curr = new Node(input.nextInt());
Node prev = head;
while (true)
{
prev = prev.getNext();
if ((int)curr.getItem() < (int)prev.getItem())
{
head.setNext(curr);
curr.setNext(prev);
break;
}
if (prev.getNext() == null)
{
prev.setNext(curr);
break;
}
}*/
}
return first;
}
private static Node insert(Node head, Comparable newValue)
{
Node prev, curr = head;
for (prev = null, curr = head;
curr != null && newValue.compareTo(curr.getItem()) > 0;
prev = curr, curr = curr.getNext() ) {}
Node newNode = new Node(newValue, curr);
if (prev != null)
{
prev.setNext(newNode);
return head;
}
else
return newNode;
}
private static boolean search(Node head, Comparable key)
{
// PRE: head points to the front of linked list; list may be
// empty or non-empty; key is item searching for
// POST: returns true or false regarding whether key is found in
// list
if (head == null){
return false;}
else if (head.getItem().equals(key)){
return true;}
else {
return search(head.getNext(), key);
}
}
}
Any ideas?
有任何想法吗?
The output should be similar to the following:
输出应类似于以下内容:
Please input 0 or more values at keyboard
请在键盘上输入 0 个或多个值
12 4 -1 5 3 0 2
12 4 -1 5 3 0 2
Now printing list
正在打印列表
-1 0 2 3 4 5 12 What key are you searching for? 15 Your key was not found
-1 0 2 3 4 5 12 你在寻找什么键?15 未找到您的密钥
回答by Bhavik Shah
Node head = null;
whenever you call a method on a null object you get an nullPointerException.That is why head.setNext(first);
is giving you exception. so instead of this you can do
每当您在空对象上调用方法时,您都会得到一个 nullPointerException。这就是为什么head.setNext(first);
给您异常。所以你可以做的不是这个
Node head = new Node();
you will avoid NullPointerException with this.
您将避免使用 NullPointerException。
According to your requirement you should do this.
根据您的要求,您应该这样做。
private static Node buildList() throws IOException
{
// Post : Inserts 0 or more numerical values from keyboard into list
// using the Scanner class and returns head of list
Scanner input = new Scanner(System.in);
Node head = null;
Node first = new Node(input.nextInt());
head=first; //assigning the first value to head
while(input.hasNext())
{
insert(first, input.nextInt());
head.setNext(first);//insert the node in the list
}
return first;
}
Note:I am assuming that setNext()inserts the node in the list at appropriate location not directly in the next position of head node (otherwise you will get only 2 nodes no matter how many numbers you insert)
注意:我假设setNext()将节点插入列表中的适当位置,而不是直接插入头节点的下一个位置(否则无论插入多少个数字,您都只会得到 2 个节点)
回答by Kumar Vivek Mitra
Node head = null;
Node head = null;
The above line will make the headwhich is an object reference variable
of type Node
to nul
l, now calling any methodon this object reference variable will lead to NullPointerException
.
上述线将使头这是一种object reference variable
类型的Node
,以nul
升,现在调用任何方法对这个对象引用变量将导致NullPointerException
。
Node head = new Node();
Node head = new Node();
The line will be a better approach as this will preventthe NullPointerException
.
该生产线将是一个更好的办法,因为这会阻止的NullPointerException
。