2015-09-07 72 views
0

因此,我正在編寫一個基於文本的冒險遊戲程序,但是當我試圖運行代碼時,我總是收到這個錯誤。我的朋友有完全相同的代碼,但是當她在計算機上運行它時,它工作正常。所以我不知道爲什麼它不適合我。線程「main」中的異常java.lang.NullPointerException用於基於文本的冒險遊戲

有錯誤發生在這裏:

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(); 
     } 
    } 
} 
+1

顯示堆棧跟蹤。 – Andreas

+0

如果你對錯誤的代碼行是正確的,那麼'currentRoom'是'null'。 –

回答

1

在大部分的case塊,設置currentRoom = findRoom(),然後循環回currentRoom.displaydesc(),但findRoom()return null ,所以你得到NullPointerException

+0

那麼我該如何解決這個問題? – ariel

+0

這是給你決定的。 findRoom()應該返回null嗎?如果是,那是什麼意思?應該發生什麼? – Andreas

相關問題