Java 再次出现空指针错误

Java 再次出现空指针错误,java,argumentnullexception,Java,Argumentnullexception,所以我有一个编译器类,它编译了一些.mjava文件,但其他一些文件失败了,我想知道是否有人能帮我找出原因。我有两种方法,分别用于两个不同的文件。我尝试编译的第一个consts.mjava文件是: // demo of true local and global variables int glob0; int glob1; final int two = 2; final int three = 3; main() { int loc1; int loc2; int lo

所以我有一个编译器类,它编译了一些.mjava文件,但其他一些文件失败了,我想知道是否有人能帮我找出原因。我有两种方法,分别用于两个不同的文件。我尝试编译的第一个consts.mjava文件是:

// demo of true local and global variables
int glob0;
int glob1;
final int two = 2;
final int three = 3;
main() {
    int loc1;
    int loc2;
    int loc3;
    final int four = 4;
    glob0 = three;
    //print("glob0=", glob0, "\n");
    loc1 = glob0*two+1;
    glob1 = glob0*loc1;
    loc2  = glob1+1;
    loc3 = glob1*loc2/four;
    print("glob0=", glob0, " (should be 3)\n");
    print("glob1=", glob1, " (should be 21)\n");
    print("loc1=",  loc1,  " (should be 7)\n");
    print("loc2=",  loc2,  " (should be 22)\n");
    print("loc3=",  loc3,  " (should be 115)\n");
}
当我试图用我的编译器类编译它时,它在这里中断:

private void compileFactor() {

    if (isIdent(theToken)) {
        String ident = theToken;
        theToken = t.token();            
        IdentInfo theInfo = symTable.lookup(ident);


        boolean its_a_variable = theInfo.isVar();  ***//Breaks Here for consts.mjava Null Exception***
        int theAddr; 
        boolean isGlobal = theInfo.getIsGlobal();
        int constValue;
        int theNumber = 0;

        if (its_a_variable) {   // pld12: CHANGE THIS!!
                 theAddr = theInfo.getAddr(); 
                 isGlobal = theInfo.getIsGlobal();
                 if (theAddr == -1) t.error("undeclared identifier used in expr: "+ident);
                 if (isGlobal) cs.emit(Machine.LOAD, theAddr);
                 else cs.emit(Machine.LOADF, theAddr);
        } else {
                 constValue = theInfo.getValue();
                 if (constValue == 0) 
                        t.error("undeclared identifier used in expr: "+ident);
                 else {
                          cs.emitLOADINT(theNumber);
                 }
                }
            } else if (isNumber(theToken)) {
                int theNumber = new Integer(theToken).intValue();
                cs.emitLOADINT(theNumber);
                theToken = t.token();
            } else if (equals(theToken, "(")) {  
                accept("(");
                compileExpr();
                accept(")");
            }
        }
我尝试在此方法上运行的下一个locs.mjava文件:

private void compileIdentStmt() {
        String ident = theToken;
        boolean isGlobal = true;
        int location = 0;
        int entryPoint = 0;
        IdentInfo varInfo = null;
        if (!isIdent(ident)) t.error("expected identifier, got " + theToken);
        theToken = t.token();
        if (equals(theToken, "=")) {    
            accept("=");

            varInfo = symTable.lookup(ident);
            if (varInfo.isVar() == true) {      ***//Breaks Here on locs.mjava: Null Exception***
                location = varInfo.getAddr();        
                isGlobal = varInfo.getIsGlobal();    
            }

            /*
            if (varInfo==null) {
                location = GHack(ident);
                isGlobal = true;
            }
            if (location == -1) {
                location = LHack(ident);
                isGlobal = false;
            }
            /* */
            compileExpr();
            if (isGlobal) cs.emit(Machine.STOR, location);
            else cs.emit(Machine.STORF, location);      
            accept(";");
        } else if (equals(theToken, "(")) {     
                         varInfo = symTable.lookup(ident);

             if (varInfo.isProc() == true) {  
                entryPoint = varInfo.getEntryPoint();
                dprint("call to function " + ident + "; generating JSR to location " + entryPoint);
                accept("(");
            }
            /* 
            if (!equals(theToken, ")")) {   
                compileExpr();
                while (equals(theToken, ",")) {
                    accept(",");
                    compileExpr();
                }
            }
            /* */
            accept(")");
            accept(";");
            cs.emit(Machine.JSR, entryPoint);
        } else t.error("expected \"=\" or \"(\", got " + theToken);
    }
我甚至会提供我的symTable()中的查找方法来帮助:


如果您得到的是NullPointerException,那是因为
theInfo
varInfo
在您的示例中为空。

之后

IdentInfo theInfo = symTable.lookup(ident);

在尝试使用该信息之前,您应该检查该信息是否为null,因为您的查找方法明确指出它可以返回null。

它被称为NullPointerException。了解错误和异常之间的区别mjava文件有什么特殊之处?
IdentInfo theInfo = symTable.lookup(ident);