我的代码遇到问题。这是我第一次使用Map,由于某种原因,在第二个try块中添加的Gate为null。我无法发现错误。错误首先显示在最后一个try块中,当我尝试设置inputgates时,我得到nullpointer。
给予函数的文件有几行,每行看起来像这样:从门的名称开始,然后是门的类型,然后是一些输入门。 (Gates previouse创建)。有一种称为信号门的门只能作为启动门。没有输入,只有输出。我们正在谈论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;
}
答案 0 :(得分:0)
<强> TL; DR:强>
冒险你的问题出现在第90,92和94行:你没有就这些新创建的异常调用throw
。
这些类可能永远不会因为某些类问题而被实例化(修复上面的内容会显示),所以这是 catch 但是如上所述不会中断执行或日志(可能) ,但实际上不会添加到地图。当然,它可能是别的......
除此之外,还有一些代码质量要点:
Capital(String s)
方法通常应该有小'c'(或考虑有用的WordUtils.capitalizeFully)null
的回复值。