使用链表NullPointerException异常错误(NullPointerException

2019-08-02 11:39发布

我刚刚完成了对这个项目的工作,并得到它来编译,但用户输入后坏,给我这样的:

请输入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)

下面是代码:

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

} 

}

有任何想法吗?

输出应类似于以下内容:

请输入在键盘0或多个值

12 4 -1 5 3 0 2

现在打印一览

您是否在寻找-1 0 2 3 4 5 12哪些关键? 15你的钥匙没有被发现

Answer 1:

Node head = null;

当你调用一个方法,一个空的对象,你得到一个nullPointerException.That就是为什么head.setNext(first); 是给你例外。 所以不是这样,你可以做

Node head = new Node();

你会避免NullPointerException异常与此有关。

根据你的要求,你应该这样做。

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

注:我假设setNext()插入在列表中相应位置的节点不能直接在头节点的下一个位置(否则你将只能得到2个节点,无论你有多少个号码时插入)



Answer 2:

Node head = null;

上述线将使 ,其是一个object reference variable类型的Nodenul升,现在调用此对象引用变量的任何方法将导致NullPointerException

Node head = new Node();

该生产线将是一个更好的办法,因为这将防止 NullPointerException



文章来源: NullPointerException Error using linked lists