質問
この質問に答えはこちら
私はこのハッシュセットコードは、私が試してみてください私のコンパイル方法で取得します場合は、Nullポインタが例外:nullの場合エラーです。こちらのコード:
private void initKeywords() {
keywords = new HashSet<String>();
keywords.add("final");
keywords.add("int");
keywords.add("while");
keywords.add("if");
keywords.add("else");
keywords.add("print");
}
private boolean isIdent(String t) {
if (keywords.contains(t)) { ***//This is the line I get the Error***
return false;
}
else if (t != null && t.length() > 0 && Character.isLetter(t.charAt(0))) {
return true;
}
else {
return false;
}
}
その他続けられるようにするために、このエラー:
public void compileProgram() {
System.out.println("compiling " + filename);
while (theToken != null) {
if (equals(theToken, "int") || equals(theToken, "final")) {
compileDeclaration(true);
} else {
compileFunction(); //This line is giving an error with the above error
}
}
cs.emit(Machine.HALT);
isCompiled = true;
}
private void compileFunction() {
String fname = theToken;
int entryPoint = cs.getPos();
if (equals(fname, "main")) {
cs.setEntry(entryPoint);
}
if (isIdent(theToken)) theToken = t.token(); ***//This line is giving an error***
else t.error("expecting identifier, got " + theToken);
symTable.allocProc(fname,entryPoint);
accept("(");
compileParamList();
accept(")");
compileCompound(true);
if (equals(fname, "main")) cs.emit(Machine.HALT);
else cs.emit(Machine.RET);
}
解決
keywords
またはt
のどちらかがnullです。デバッガやprint文のいずれかを使用すると、決定する非常に単純でなければなりません。 keywords
がnullの場合、私はinitKeywords()
がまだ呼び出されていないことを前提と思います。
他のヒント
あなたがinitKeywords()
前isIdent()
を実行しているか?
あなたはおそらく、このオブジェクトのコンストラクタからinitKeywords
を呼び出したい。
私は個人的には離れのinitメソッドから滞在してみてください。先に述べたように、コンストラクタは初期化として機能し、従って静的ブロックが行う
private final static Set<String> KEYWORDS = new HashSet<String>();
static {
keywords.add("final");
keywords.add("int");
keywords.add("while");
keywords.add("if");
keywords.add("else");
keywords.add("print");
}
所属していません StackOverflow