0

このプログラムの作業を終えてコンパイルしましたが、ユーザー入力後に壊れて、次のように表示されます。

キーボードで0以上の値を入力してください1243 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キーが見つかりませんでした

4

2 に答える 2

4
Node head = null;

nullオブジェクトでメソッドを呼び出すたびに、nullPointerExceptionが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 つのノードしか得られません)。

于 2012-11-08T05:03:19.387 に答える
0

Node head = null;

上記の行は、 lの型であるheadを作成します。このオブジェクト参照変数で任意のメソッドを呼び出すと、 .object reference variableNodenulNullPointerException

Node head = new Node();

これによりNullPointerException.

于 2012-11-08T05:16:57.920 に答える