コードに問題があります。マップを使用するのはこれが初めてで、何らかの理由で 2 番目の try ブロックに追加されたゲートが null です。エラーを見つけることができません。エラーは、入力ゲートを設定しようとするとnullpointerを取得する最後のtryブロックに最初に表示されます。
関数に指定されたファイルには、各行が次のような行がいくつかあります。ゲートの名前、ゲートのタイプ、入力ゲートの数で始まります。(ゲートは以前に作成されました)。スタートゲートとしてのみ機能するシグナルゲートと呼ばれるゲートのタイプがあります。入力はなく、出力のみです。NAND、AND、ORゲートなどについて話しています。
前もって感謝します!
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;
}