我遇到了我的代碼問題。這是我第一次使用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;
}
請發佈您看到的錯誤的完整堆棧跟蹤。 – jgm
我在想我們需要'Gate.java'。而不是從一個文件讀取,你可以在文件中添加三行或四行來創建'Gate's? – Teepeemm
http://lpaste.net/97334 有errormessage,進一步下來是文件的外觀。以*或/開頭的行是註釋。 gateprog.java中沒有錯誤。這是給我們的文件。我們不會改變它。 – DakkVader