2013-12-21 14 views
0

我遇到了我的代碼問題。這是我第一次使用Map,由於某些原因,在第二個try塊中添加的Gate是空的。我無法發現錯誤。該錯誤首先顯示在最後的try塊中,當我嘗試設置inputgates時,我得到了空指針。JAVA:將我的班級「Gate」添加到地圖的示例爲空

提供給函數的文件有幾行,其中每行看起來像這樣:以門的名稱開頭,然後是門的類型,然後是一些輸入門。 (蓋茨previouse創建)。有一種稱爲信號門的門,它只是作爲啓動門。沒有輸入,只有輸出。我們在談論NAND,AND,或者門等。

在此先感謝!

public static String Capital(String s){ 
    String string = s.substring(0,1).toUpperCase() + s.substring(1).toLowerCase(); 
    string = string + "Gate"; 
    return string; 
} 

public static Map<String, Gate> createGates(File file){ 
    //variabler. 
    BufferedReader bufferedReader = null; 
    String[] lines = null; 
    String line; 
    Map<String, Gate> map = new LinkedHashMap<String, Gate>(); 
    int index = 0; 
    Gate g; 

    try{ 
     //tests reading the file 
     bufferedReader = new BufferedReader(new FileReader(file)); 
     //As long as there still are lines left 
     while((line = bufferedReader.readLine()) != null){ 
      index++; 
      //as long as the line's not a comment. 
      if(line.trim().charAt(0) == '*' || line.trim().charAt(0) == '/'){ 
       continue; 
      }else{ 
       lines = line.split("\\s+"); 
      } 
      //Gör namnet till att börja med stor bokstav och resten blir små bokstäver. 
      lines[1] = Capital(lines[1]); 
      try{ 
       System.out.println("hej"); 
       //Skapar en instans av en gate med class.metoden 
       g = (Gate) Class.forName(lines[1]).newInstance(); 
       //sätter namnet. Detta använder vi senare 
       g.setName(lines[0]); 
       //För in dom i mapen. 
       map.put(g.getName(), g); 

      }catch (InstantiationException e) { 
       new GateException("Something went wrong instantiating the gate " + lines[0] + " on line " + index + " in the code"); 
      } catch (IllegalAccessException e) { 
       new GateException("Something went wrong in the CreateGates-function for gate " + lines[0] + " on line " + index + " in the code"); 
      } catch (ClassNotFoundException e) { 
       new GateException("The gate " + lines[0] + " written in the file does not exist. This error occured on line " + index); 
      } 
     } 
    }catch (FileNotFoundException e){ 
     new GateException("Could not load the given file."); 
    } catch (IOException e) { 
     new GateException("Something went wrong while trying to read the file. (NOT filenotfoundexception)"); 
    } 


    //läser filen på nytt som det står i labbbeskrivningen 
    try{ 
     //samma som ovan. Reader, string och array med strings. 
     BufferedReader br = new BufferedReader(new FileReader(file)); 
     String l = null; 
     String[] li = null; 

     while((l = br.readLine()) != null){ 

      //as long as the line's not a comment. 
      if(l.trim().charAt(0) == '*' || l.trim().charAt(0) == '/'){ 
       //Omd et är en kommentar, hoppa och kör loopen nästa gång direkt. 
       continue; 
      }else{ 
       //annars splitta den 
       li = l.split("\\s+"); 
      } 

      //om det är en deklarering av en signalgate så¨ska den forstätta, annars ska den sätta inputs osv som den ska. 
      if(li.length == 2){ 
       continue; 
      }else{ 
       Gate gate = map.get(li[0]); 
       for(int i = 2; i < li.length; i++){ 
        System.out.println(map.get("b")); 
        gate.setInputGate(map.get(li[i])); 
       } 
      } 

     } 
    }catch (FileNotFoundException e){ 
     throw new GateException("Error loading the file"); 
    }catch (IOException e){ 
     throw new GateException("Error reading the file"); 
    } 

    return map; 
} 
+0

請發佈您看到的錯誤的完整堆棧跟蹤。 – jgm

+0

我在想我們需要'Gate.java'。而不是從一個文件讀取,你可以在文件中添加三行或四行來創建'Gate's? – Teepeemm

+0

http://lpaste.net/97334 有errormessage,進一步下來是文件的外觀。以*或/開頭的行是註釋。 gateprog.java中沒有錯誤。這是給我們的文件。我們不會改變它。 – DakkVader

回答

0

TL; DR: 我斗膽你的問題是上線90,92和94:你沒有調用這些新創建的異常throw

這些類可能永遠不會得到實例化,由於一些類的問題或其他(固定上面會告訴你),所以這是但如前所述不會中斷執行或日誌(可能),但將實際上並沒有添加到地圖。這可能,當然,是別的東西......

順便說一句,一些代碼質量分:

  • 再利用代碼明智:大多數文件讀取塊的可提取的方法這將使調試(或閱讀)這樣的事情變得更容易。
  • Capital(String s)方法通常應該具有小的「C」(或考慮有用WordUtils.capitalizeFully
  • 始終檢查值從map.get(foo)回來null
+0

我增加了投票,現在我得到94行的錯誤,門不存在。很明顯,因爲創建的門是空的。有沒有人有一個想法,爲什麼它不正確地堵塞門? – DakkVader

+0

做一些關於classnotfound的研究。將回來與我想出 – DakkVader

+0

我真的不明白爲什麼它給了我這個例外:/我有一個名爲SignalGate.java的類在與Gate相同的文件夾中,其他類可以正確繼承等。 – DakkVader