Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用链接列表时出现NullPointerException错误_Java_Recursion_Nullpointerexception_Linked List - Fatal编程技术网

Java 使用链接列表时出现NullPointerException错误

Java 使用链接列表时出现NullPointerException错误,java,recursion,nullpointerexception,linked-list,Java,Recursion,Nullpointerexception,Linked List,我刚刚完成了这个程序的工作,并让它进行编译,但在用户输入后它会中断,并给出以下信息: 请在键盘上输入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 clas

我刚刚完成了这个程序的工作,并让它进行编译,但在用户输入后它会中断,并给出以下信息:

请在键盘上输入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);
}

} 

}
import java.io.*;
导入java.util.*;
公共类搜索{
公共静态void main(字符串argv[])引发IOException{
扫描仪标准输入=新扫描仪(System.in);
System.out.println(“请在键盘上输入0或更多值”);
节点头=buildList();
System.out.println(“当前打印列表”);
印刷品清单(标题);
System.out.println(“\n您在搜索列表中的哪个键?”);
int key=stdin.nextInt();
System.out.print(“您的密钥是”);
如果(搜索(标题、关键字))
System.out.println(“找到”);
其他的
System.out.println(“未找到”);
}
私有静态void打印列表(节点头)
{
if(head!=null)
{
System.out.print(head.getItem()+);
打印列表(head.getNext());
}
}
私有静态节点buildList()引发IOException
{
//Post:从键盘向列表中插入0个或多个数值
//使用Scanner类并返回列表的开头
扫描仪输入=新扫描仪(System.in);
节点头=空;
Node first=新节点(input.nextInt());
head.setNext(第一个);
while(input.hasNext())
{    
插入(首先,输入.nextInt());
/*
Node curr=新节点(input.nextInt());
节点prev=头部;
while(true)
{
prev=prev.getNext();
如果((int)curr.getItem()<(int)prev.getItem())
{
head.setNext(curr);
当前设置下一个(上一个);
打破
}
if(prev.getNext()==null)
{
上一次设置下一次(当前);
打破
}
}*/
}
先返回;
} 
专用静态节点插入(节点头,可比较的新值)
{
节点上一个,当前=头部;
对于(prev=null,curr=head;
curr!=null&&newValue.compareTo(curr.getItem())>0;
prev=curr,curr=curr.getNext()){}
节点newNode=新节点(newValue,curr);
如果(上一个!=null)
{
上一个设置下一个(新节点);
回流头;
}
其他的
返回newNode;
}
私有静态布尔搜索(节点头,可比较键)
{
//PRE:标题指向链接列表的前面;列表可能是
//空或非空;键是搜索的项
//POST:返回有关在中是否找到密钥的true或false
//名单
if(head==null){
返回false;}
else if(head.getItem().equals(key)){
返回true;}
否则{
返回搜索(head.getNext(),key);
}
} 
}
有什么想法吗

输出应类似于以下内容:

请在键盘上输入0或更多值

12 4-1 5 3 0 2

现在正在打印列表

-102434512 你在找什么钥匙?15 找不到你的钥匙

Node head = null;
每当你在一个空对象上调用一个方法,你就会得到一个nullPointerException给了您一个异常。所以你可以做的不是这个

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个节点)

每当你在一个空对象上调用一个方法,你就会得到一个nullPointerException给了您一个异常。所以你可以做的不是这个

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个节点)

节点头=null

上行将head作为
节点
类型的
对象引用变量
设置为
nul
l,现在调用此对象引用变量上的任何方法将导致
NullPointerException

节点头=新节点()


该行将是一种更好的方法,因为这将防止
NullPointerException

节点头=null

上行将head作为
节点
类型的
对象引用变量
设置为
nul
l,现在调用此对象引用变量上的任何方法将导致
NullPointerException

节点头=新节点()


行将是一种更好的方法,因为这将防止
NullPointerException

出现问题,您是否尝试过调试程序以查找导致问题的行?(这个问题并不难,我认为调试比直接得到答案要好)您是否尝试过调试程序以找到导致问题的行?(这个问题并不难,我认为调试比在这里直接得到答案要好)