What is a NullPointerException, and how do I fix it?
(12个答案)
3个月前关闭。
我有一堂课,上面有一个人的名字列表,并玩刺客游戏,它在字符串列表中跟踪谁跟踪谁,谁杀死谁。
例:五个人的名单
乔->萨利->吉姆->卡罗尔->克里斯
乔在跟踪Sally,Sally在跟踪Jim等等,一直到克里斯,克里斯一直在列表的开头跟踪乔。
如果Jim首先被Sally暗杀,则列表是“重新链接的”,其中Sally的新目标是Carol。
刺客主要的主要职业
import java.io.*;
import java.util.*;
public class AssassinMain {
public static void main(String[] args) throws FileNotFoundException {
// prompt for file name
Scanner console = new Scanner(System.in);
System.out.println("Welcome to the CSE143 Assassin Manager");
System.out.println();
System.out.print("What name file do you want to use this time? ");
String fileName = console.nextLine();
// read names into a list, using a Set to avoid duplicates
Scanner input = new Scanner(new File(fileName));
Set<String> names = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
List<String> names2 = new ArrayList<>();
while (input.hasNextLine()) {
String name = input.nextLine().trim();
if (name.length() > 0 && !names.contains(name)) {
names.add(name);
names2.add(name);
}
}
// shuffle if desired
if (yesTo(console, "Do you want the names shuffled?")) {
Collections.shuffle(names2);
}
// make an immutable version and use it to build an AssassinManager
List<String> names3 = Collections.unmodifiableList(names2);
AssassinManager manager = new AssassinManager(names3);
System.out.println();
// prompt the user for victims until the game is over
while (!manager.gameOver()) {
oneKill(console, manager);
}
// report who won
System.out.println("Game was won by " + manager.winner());
System.out.println("Final graveyard is as follows:");
manager.printGraveyard();
}
// Handles the details of recording one victim. Shows the current kill
// ring and graveyard to the user, prompts for a name and records the
// kill if the name is legal.
public static void oneKill(Scanner console, AssassinManager manager) {
System.out.println("Current kill ring:");
manager.printKillRing();
System.out.println("Current graveyard:");
manager.printGraveyard();
System.out.println();
System.out.print("next victim? ");
String name = console.nextLine().trim();
if (manager.graveyardContains(name)) {
System.out.println(name + " is already dead.");
} else if (!manager.killRingContains(name)) {
System.out.println("Unknown person.");
} else {
manager.kill(name);
}
System.out.println();
}
// post: asks the user a question, forcing an answer of "y" or "n";
// returns true if the answer was yes, returns false otherwise
public static boolean yesTo(Scanner console, String prompt) {
System.out.print(prompt + " (y/n)? ");
String response = console.nextLine().trim().toLowerCase();
while (!response.equals("y") && !response.equals("n")) {
System.out.println("Please answer y or n.");
System.out.print(prompt + " (y/n)? ");
response = console.nextLine().trim().toLowerCase();
}
return response.equals("y");
}
}
AssassinNode类
// The AssassinNode class is used to store the information for one
// player in the game of assassin. Initially the "killer" field
// is set to null, but when the person is killed, this should be
// set to the name of the killer.
public class AssassinNode {
public String name; // this person's name
public String killer; // name of who killed this person
public AssassinNode next; // next node in the list
// constructs a node with the given name and a null link
public AssassinNode(String name) {
this(name, null);
}
// constructs a node with the given name and link
public AssassinNode(String name, AssassinNode next) {
this.name = name;
this.killer = null;
this.next = next;
}
}
AssassinManager类
import java.util.*;
public class AssassinManager {
private AssassinNode killRingFront;
private AssassinNode graveyardFront;
public AssassinManager(List<String> names) {
if (names.isEmpty() || names == null) {
throw new IllegalArgumentException("Empty List");
}
AssassinNode current = killRingFront;
for (int i = 0; i < names.size(); i++) {
AssassinNode temp = new AssassinNode(names.get(i));
current.next = temp;
current = temp;
}
}
public void printKillRing() {
AssassinNode current = killRingFront;
while(current != null) {
if (current.next != null) {
System.out.println(" " + current.name + " is stalking "
+ killRingFront.name);
} else {
System.out.println(" " + current.name + " is stalking "
+ current.next.name);
}
current = current.next;
}
}
public void printGraveyard() {
AssassinNode current = graveyardFront;
while (current != null) {
System.out.println(" " + current.name + " was killed by "
+ current.killer);
current = current.next;
}
}
public boolean killRingContains(String name) {
return listContains(killRingFront, name);
}
public boolean graveyardContains(String name) {
return listContains(graveyardFront, name);
}
public boolean gameOver() {
return killRingFront.next == null;
}
public String winner() {
if(gameOver()) {
return killRingFront.name;
} else {
return null;
}
}
public void kill(String name) {
if(!killRingContains(name)) {
throw new IllegalArgumentException();
}
if(gameOver()) {
throw new IllegalStateException();
}
AssassinNode current = killRingFront;
if (current.name.equalsIgnoreCase(name)) {
while(current.next != null) {
current = current.next;
}
moveToGraveyard(killRingFront.name, current.name);
killRingFront = killRingFront.next;
} else {
while (current.next != null) {
if (current.next.name.equalsIgnoreCase(name)) {
moveToGraveyard(current.next.name, current.name);
current.next = current.next.next;
}
current = current.next;
}
}
}
private boolean listContains(AssassinNode frontNode, String name) {
AssassinNode current = frontNode;
while (current != null) {
if (current.name.equalsIgnoreCase(name)) {
return true;
}
current = current.next;
}
return false;
}
private void moveToGraveyard(String killed, String killerName) {
if (graveyardFront == null) {
graveyardFront = new AssassinNode(killed);
graveyardFront.killer = killerName;
} else {
graveyardFront = new AssassinNode(killed, graveyardFront);
graveyardFront.killer = killerName;
}
}
}
NullPointerException错误发生在我的
AssassinManager
类中的该行
current.next = temp;
在
public AssassinManager(List<String> names)
构造函数中
我是一名优秀的程序员,十分优秀!