所以我正在编写一个基于文本的冒险游戏程序,但是在尝试运行代码时我一直收到此错误。我的朋友有完全相同的代码,但当她在她的电脑上运行时,它工作正常。所以我不知道为什么它不适合我。
此处出现错误:
currentRoom.displaydesc();
以下是代码:
import java.io.*;
import java.util.*;
class Option {
public String description;
public String target;
public Option(){
description = "";
target = "";
}
}
class Room{
public String name;
//public String current;
public Option[] Options;
public String description;
public String tag;
public Room next;
//public String[] key = new String[12];
public Room(String line){
name = line;
Options = new Option[12];
}
public void displayLink(){
System.out.print(name + " : ");
for(int i=0; i<Options.length; i++){
if(Options[i] != null){
System.out.print(Options[i].target+" ");
}
}
System.out.println("");
}
public void displaydesc(){
//if(description != null){
System.out.println(description);
//}
}
public void displayopt(){
char[] key= {'a','b','c','d','e','f','g','h','i','j','k','l'};
for(int i=0; i<Options.length; i++){
if(Options[i] != null){
System.out.println(key[i]+" - "+Options[i].description);
}
}
}
public void displayInfo(){
System.out.println("Room: "+name);
System.out.println("Description: "+ description);
for(int i=0; i<Options.length; i++){
System.out.println("Option: "+ Options[i].description);
System.out.println("tags: "+ Options[i].target);
}
}
}
class LinkedList{
public Room first;
public Room root;
public Room current;
public LinkedList(){
first = null;
current = null;
}
public boolean isEmpty(){
return first == null;
}
public Scanner insertRoom(Scanner scan){
int i =0;
//String tag;
String line;
while(scan.hasNextLine()){
line = scan.nextLine();
//String name;
if(line != null){
if(line.isEmpty()){
line = scan.nextLine();
}
char command = line.charAt(0);
line = line.substring(1,line.length()).trim();
switch(command){
case 'r':
Room newRoom = new Room(line);
newRoom.name = line;
if(isEmpty()){
first = newRoom;
root = first;
}else{
newRoom.next = first;
first = newRoom;
}
i=0;
//System.out.println("Adding a new node with name "+line);
break;
case'd':
if(first.description != ""){
first.description = first.description+ "\n" + line;
}else{
first.description = line;
}
//System.out.println("Adding a new description "+line);
break;
case'o':
Option o = new Option();
o.description = line;
first.Options[i] = o;
//i++;
//System.out.println("Adding a new option "+line);
break;
case't':
first.Options[i].target = line;
i++;
//System.out.println("Adding a new tag "+line);
break;
}
}
}
return scan;
}
public Room findRoom(String find){
Room current = first;
while(!current.name.equals(find)){
if(current.next != null){
current = current.next;
}else{
return null;
}
}
return current;
}
public void displayList(){
Room current = first;
while (current != null){
current.displayLink();
//current.displayInfo();
current = current.next;
}
System.out.println("");
}
}
class LinkedListApp{
public static void main(String[] args){
LinkedList theList = new LinkedList();
Scanner scan = new Scanner(System.in);
try{
scan = new Scanner (new File(args[0]));
}catch(Exception e){
}
String line;
while(scan.hasNextLine()){
scan = theList.insertRoom(scan);
}
//char[] key= {'a','b','c','d','e','f','g','h','i','j','k','l'};
scan = new Scanner(System.in);
Room currentRoom = theList.root;
//Option currentO = theList.
while(true){
currentRoom.displaydesc();
currentRoom.displayopt();
String input = scan.nextLine();
switch(input){
case "q":
return;
case "r":
currentRoom = theList.root;
break;
case "y":
System.out.println("[information]");
theList.displayList();
break;
case "z":
break;
case "a":
if(currentRoom.Options[0].target != ""){
System.out.println("["+currentRoom.Options[0].description+"]");
String room = currentRoom.Options[0].target;
currentRoom = theList.findRoom(room);
}else{
System.out.println("No option available.");
}
break;
case "b":
if(currentRoom.Options[1].target != ""){
System.out.println("["+currentRoom.Options[1].description+"]");
String room = currentRoom.Options[1].target;
currentRoom = theList.findRoom(room);
}else{
System.out.println("No option available.");
}
break;
case "c":
if(currentRoom.Options[2].target != ""){
System.out.println("["+currentRoom.Options[2].description+"]");
String room = currentRoom.Options[2].target;
currentRoom = theList.findRoom(room);
}else{
System.out.println("No option available.");
}
break;
case "d":
if(currentRoom.Options[3].target != ""){
System.out.println("["+currentRoom.Options[3].description+"]");
String room = currentRoom.Options[3].target;
currentRoom = theList.findRoom(room);
}else{
System.out.println("No option available.");
}
break;
case "e":
if(currentRoom.Options[4].target != ""){
System.out.println("["+currentRoom.Options[4].description+"]");
String room = currentRoom.Options[4].target;
currentRoom = theList.findRoom(room);
}else{
System.out.println("No option available.");
}
break;
case "f":
if(currentRoom.Options[5].target != ""){
System.out.println("["+currentRoom.Options[5].description+"]");
String room = currentRoom.Options[5].target;
currentRoom = theList.findRoom(room);
}else{
System.out.println("No option available.");
}
break;
case "g":
if(currentRoom.Options[6].target != ""){
System.out.println("["+currentRoom.Options[6].description+"]");
String room = currentRoom.Options[6].target;
currentRoom = theList.findRoom(room);
}else{
System.out.println("No option available.");
}
break;
case "h":
if(currentRoom.Options[7].target != ""){
System.out.println("["+currentRoom.Options[7].description+"]");
String room = currentRoom.Options[7].target;
currentRoom = theList.findRoom(room);
}else{
System.out.println("No option available.");
}
break;
case "i":
if(currentRoom.Options[8].target != ""){
System.out.println("["+currentRoom.Options[8].description+"]");
String room = currentRoom.Options[8].target;
currentRoom = theList.findRoom(room);
}else{
System.out.println("No option available.");
}
break;
case "j":
if(currentRoom.Options[9].target != ""){
System.out.println("["+currentRoom.Options[9].description+"]");
String room = currentRoom.Options[9].target;
currentRoom = theList.findRoom(room);
}else{
System.out.println("No option available.");
}
break;
case "k":
if(currentRoom.Options[10].target != ""){
System.out.println("["+currentRoom.Options[10].description+"]");
String room = currentRoom.Options[10].target;
currentRoom = theList.findRoom(room);
}else{
System.out.println("No option available.");
}
break;
case "l":
if(currentRoom.Options[11].target != ""){
System.out.println("["+currentRoom.Options[11].description+"]");
String room = currentRoom.Options[11].target;
currentRoom = theList.findRoom(room);
}else{
System.out.println("No option available.");
}
break;
}
//theList.displayList();
}
}
}
答案 0 :(得分:1)
在大多数case
块中,您设置currentRoom = findRoom()
,然后循环回currentRoom.displaydesc()
,但findRoom()
有return null
,所以你得到NullPointerException
。