JAVA - 线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:0

时间:2015-01-18 13:11:21

标签: java exception analytics

package parserstruct;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

public class ParserStruct {

private BufferedInputStream input;


public ParserStruct(String vstup) {
    InputStream stream = new ByteArrayInputStream(vstup.getBytes(StandardCharsets.UTF_8));
    input = new BufferedInputStream(stream);
}

private int readChar() throws IOException {
    int c = input.read();
    return c;
}

private int peekChar() throws IOException {
    input.mark(1);
    int r = input.read();
    input.reset();
    return r;
}

private int readSymbol() throws IOException {
    int c = peekChar();
    while (Character.isSpaceChar(c)) {
        readChar();
        c = peekChar();
    }
    return readChar();
}

private int peekSymbol() throws IOException {
    int c = peekChar();
    while (Character.isSpaceChar(c)) {
        readChar();
        c = peekChar();
    }
    return c;
}


private String getString(int jdiDo) throws IOException, Exception {
    char[] p = new char[jdiDo];
    for (int i = 0; i < jdiDo; i++) {
        p[i] = (char) readSymbol();
    }
    char c = (char) peekChar();

    if (!Character.isSpaceChar(c)) {
        //throw new Exception("parsing error wrong syntax expect primitive type or String");
        throw new Exception("NO");
    }
    return new String(p);
}

public void parse() throws IOException, Exception {
    S();
    System.out.println("YES");
}

private void S() throws IOException, Exception {
    int c = peekSymbol();
    char[] p = new char[8];
    if (c == (int) 't') {
        for (int i = 0; i < 8; i++) {
            p[i] = (char) readChar();
        }
    } else {
        //throw new Exception("parsing error wrong syntax expect 't'");
        throw new Exception("NO");
    }
    String def = new String(p);
    if (def.equals("typedef ")) {
        T();
    } else {
        //throw new Exception("parsing error wrong syntax expect 'typedef' you wrote " + def);
        throw new Exception("NO");
    }
}


private void T() throws IOException, Exception {
    int c = peekSymbol();
    char[] p = new char[6];
    if (c == (int) 's') {
        for (int i = 0; i < 6; i++) {
            p[i] = (char) readChar();
        }
    } else {
        //throw new Exception("parsing error wrong syntax expect 's' after typedef");
        throw new Exception("NO");
    }
    String def = new String(p);
    if (!def.equals("struct")) {
        //throw new Exception("parsing error wrong syntax expect 'struct' you wrote " + def);
        throw new Exception("NO");
    }
    c = readSymbol();
    char d = (char) c;
    if (!(c == (int) '{')) {
        //throw new Exception("parsing error wrong syntax expect '{' after struct");
        throw new Exception("NO");
    }
    V();
    c = readSymbol();
    d = (char) c;
    if (!(c == (int) '}')) {
        //throw new Exception("parsing error wrong syntax expect '}'");
        throw new Exception("NO");
    }
    I();
    c = readSymbol();
    d = (char) c;
    if (!(c == (int) ';')) {
        //throw new Exception("parsing error wrong syntax expect ';'");
        throw new Exception("NO");
    }
}

private void V() throws IOException, Exception {
    int c = peekSymbol();
    char d = (char) c;
    if (c == (int) 's') {
        T();
    } else if (c == (int) 'i' || c == (int) 'd' || c == (int) 'S' || c == (int) 'c' || c == (int) 'l') {
        P();
    } else {

        throw new Exception("NO");
    }
    c = peekSymbol();
    d = (char) c;
    if (c != (int) '}') {
        V();
    }

}

private void P() throws IOException, Exception {
    X();
    I();
    int c = readSymbol();
    char d = (char) c;
    if (c != (int) ';') {

        throw new Exception("NO");
    }
}


private void X() throws IOException, Exception {
    int c = peekSymbol();
    char d = (char) c;
    if (c == (int) 'i') {
        String string = getString(3);
        if (!string.equals("int")) {
            //throw new Exception("parsing error wrong syntax expect int you wrote " + string);
            throw new Exception("NO");
        }
    } else if (c == (int) 'd') {
        String string = getString(5);
        if (!string.equals("double")) {
            //throw new Exception("parsing error wrong syntax expect int you wrote " + string);
            throw new Exception("NO");
        }
    } else if (c == (int) 'l') {
        String string = getString(4);
        if (!string.equals("long")) {
            //throw new Exception("parsing error wrong syntax expect int you wrote " + string);
            throw new Exception("NO");
        }
    } else if (c == (int) 'c') {
        String string = getString(4);
        if (!string.equals("char")) {
            //throw new Exception("parsing error wrong syntax expect int you wrote " + string);
            throw new Exception("NO");
        }
    } else if (c == (int) 'S') {
        String string = getString(6);
        if (!string.equals("String")) {
            //throw new Exception("parsing error wrong syntax expect int you wrote " + string);
            throw new Exception("NO");
        }
    } else {
        //throw new Exception("parsing error wrong syntax expect primitive type or String");
        throw new Exception("NO");
    }
}

private void I() throws IOException, Exception {
    char c = (char) readSymbol();
    char d = (char) c;
    if (!Character.isLetter(c)) {

        throw new Exception("NO");
    }
}

public static void main(String[] args) {
    ParserStruct parser = new ParserStruct(args[0]);
    try {
        parser.parse();
    } catch (IOException ex) {
        System.out.println(ex.getMessage());
    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }
}
}

有错误: 线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:0     在parserstruct.ParserStruct.main(ParserStruct.java:307) Java结果:1

307:ParserStruct解析器= new ParserStruct(args [0]);

2 个答案:

答案 0 :(得分:3)

运行程序时没有传递任何参数。在将args.length传递给args[0]之前,您可以查看ParserStruct

答案 1 :(得分:2)

如果在没有任何命令行参数的情况下运行程序,new ParserStruct(args[0])将抛出该异常,因为args数组将为空,因此0将是无效索引。