gpt4 book ai didi

java - 空指针异常 : null error

转载 作者:行者123 更新时间:2023-12-02 08:35:45 25 4
gpt4 key购买 nike

我有这个哈希集代码,当我尝试在其上运行我的编译方法时,我收到 Null Pointer Exception: 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);
}

最佳答案

您确定在 isIdent() 之前运行 initKeywords() 吗?

关于java - 空指针异常 : null error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1864028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com