空指针异常:空错误

发布于 2024-08-13 18:39:22 字数 1795 浏览 3 评论 0原文

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

I have this Hash Set code and when I try to run my compile method on it I get the Null Pointer Exception: null error on it. Here is the code:

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;
        }
    }

The other lines that goes along with this error is:

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);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暖阳 2024-08-20 18:39:22

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

Are you sure you're running initKeywords() before isIdent()?

稚气少女 2024-08-20 18:39:22

keywordst 为空。使用调试器或打印语句应该很容易确定。如果 keywords 为 null,我会假设 initKeywords() 尚未被调用。

Either keywords or t is null. Using either a debugger or print statements it should be pretty simple to determine. If keywords is null, I'd assume that initKeywords() has not been called yet.

往日 2024-08-20 18:39:22

您可能想从该对象的构造函数中调用 initKeywords

You probably want to call initKeywords from the constructor of this object.

GRAY°灰色天空 2024-08-20 18:39:22

我个人尝试远离 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");
}

I personally try to stay away from init methods. As previously mentioned, a constructor serves as an initializer, and so does the static block:

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");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文