这个项目的错误在哪里? here is my source code
编译程序
在First_Project.First_Project.main(First_Project.java:27)
那么,我的错误是什么?在哪里?
包First_Project;
import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; public class First_Project { private static PrintWriter output; private static Scanner input; static char LF; public static void main(String args[]) throws FileNotFoundException, NullPointerException { File inputFile = new File("D:\\input.txt"); if (!inputFile.exists()) { System.out.println("Input file, " + inputFile + ", does not exist."); System.exit(0); } // Output File: File outputFile = new File("D:\\outputbaraa.txt"); output = new PrintWriter(outputFile); // Make Scanner for input and Printwriter for output LetsDoThis(); } static String Text = ""; // string to store lexemes static String WORD[] = {"abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "enum", "extends", "for", "final", "finally", "float", "goto", "if", "implements", "import", "instanceof", "int", "interface", "long", "native", "new", "package", "private", "protected", "public", "return", "short", "static", "strictfp*", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "try", "void", "volatile", "while"}; public static void error() { output.println(Text + "\t\tUnknown Character\n"); Text = ""; output.flush(); } private static boolean isLetter(char c) { return (((int) c > 96 && (int) c < 123) || ((int) c > 64 && (int) c < 91)); } private static boolean isDigit(char c) { return ((int) c > 47 && (int) c < 58); } private static boolean isReservedWord(String word) { for (int i = 0; i < WORD.length; i++) { if (word.equals(WORD[i])) { return true; } } return false; } private static boolean isWhiteSpace(char lookahead) { return (lookahead == '\r' || lookahead == '\t' || lookahead == ' ' || lookahead == '\n' || lookahead == '\r'); } public static void LetsDoThis() { output.println("Lexemes\t\tTokens\n"); output.flush(); String line; String text = ""; while (input.hasNext()) { line = input.nextLine(); text += line; } // <, >, <=, >=, == // <,<=, > int state = 0; int position = 0; // index for text int flag = 0; LF = text.charAt(position++); // while not end of inputfile //<editor-fold defaultstate="collapsed" desc="While code ^_^"> while (position < text.length() - 3 && !"\000".equals(text.substring(text.length() - 3, text.length()))) { switch (state) { case 0: if (isWhiteSpace(LF)) { state = 0; LF = text.charAt(position++); } else { // append character to Text variable Text += LF; if (LF == '_' || isLetter(LF)) { state = 1; } else if (isDigit(LF)) { state = 3; } else if (LF == ',') { state = 5; } else if (LF == ';') { state = 6; } else if (LF == ':') { state = 7; } else if (LF == '?') { state = 8; } else if (LF == '{') { state = 9; } else if (LF == '}') { state = 10; } else if (LF == '(') { state = 11; } else if (LF == ')') { state = 12; } else if (LF == '=') { state = 13; LF = text.charAt(position++); if (LF == '=') { flag = 1; } } else if (LF == '+') { LF = text.charAt(position); // if a single '+' operator state = 14; // if it is a double '+' operator if (LF == '+') { state = 19; // if '=' operator } else if (LF == '=') { flag = 1; } } else if (LF == '-') { state = 15; LF = text.charAt(position); if (LF == '-') { state = 20; } else if (LF == '=') { flag = 1; } } else if (LF == '*') { state = 16; if (text.charAt(position) == '=') { flag = 1; } } else if (LF == '/') { state = 17; if (text.charAt(position) == '=') { flag = 1; } } else if (LF == '%') { state = 18; if (text.charAt(position) == '=') { flag = 1; } } else if (LF == '<') { state = 21; if (text.charAt(position) == '=') { flag = 1; Text += text.charAt(position++); } } else if (LF == '>') { state = 22; if (text.charAt(position) == '=') { flag = 1; } } else { error(); LF = text.charAt(position++); state = 0; } } //</editor-fold> break; case 1: //Read the next character from the input inputfile LF = text.charAt(position++); // if (_, Letter, Digit) remain at the same state if (LF == '_' || isLetter(LF) || isDigit(LF)) { Text += String.valueOf(LF); } else { state = 2; } break; case 2: state = 0; if (isReservedWord(Text)) { output.println(Text + "\t\t" + Text); } else { output.println(Text + "\t\tidentifier\n"); } output.flush(); // clear Text variable Text = ""; break; case 3: LF = text.charAt(position++); if (isDigit(LF)) { Text += LF; LF = text.charAt(position++); } else { state = 4; } break; case 4: state = 0; output.println(Text + "\t\tinteger"); Text = ""; break; case 5: state = 0; output.println(Text + "\t\tcomma\n"); Text = ""; LF = text.charAt(position++); break; case 6: state = 0; output.println(Text + "\t\tsemi-colon"); Text = ""; LF = text.charAt(position++); break; case 7: state = 0; output.println(Text + "\t\tcolon"); Text = ""; LF = text.charAt(position++); break; case 8: state = 0; output.println(Text + "\t\tquestion_mark"); Text = ""; LF = text.charAt(position++); break; case 9: state = 0; output.println(Text + "\t\tleft_curly"); Text = ""; LF = text.charAt(position++); break; case 10: state = 0; System.out.println(Text + "\t\tright_curly"); Text = ""; LF = text.charAt(position++); break; case 11: state = 0; System.out.println(Text + "\t\tleft_parenth"); Text = ""; LF = text.charAt(position++); break; case 12: state = 0; System.out.println(Text + "\t\tright_parenth"); Text = ""; LF = text.charAt(position++); break; case 13: // =, == state = 0; System.out.println(flag == 0 ? (Text + "\t\tassign_op") : Text + '=' + "\t\tequality_op"); Text = ""; LF = text.charAt(position++); break; case 14: // +, +=, ++ state = 0; System.out.println(flag == 0 ? (Text + "\t\tarith_plus") : Text + '=' + "\t\tarith_assig_plus"); Text = ""; flag = 0; LF = text.charAt(position++); if (LF == '+') { state = 19; } break; case 15: state = 0; System.out.println(flag == 0 ? (Text + "\t\tarith_minus") : Text + '=' + "\t\tarith_assig_minus"); flag = 0; Text = ""; LF = text.charAt(position++); break; case 16: state = 0; System.out.println(flag == 0 ? (Text + "\t\tarith_mult") : Text + '=' + "\t\tarith_assig_mult"); flag = 0; Text = ""; LF = text.charAt(position++); break; // / and =/ case 17: state = 0; System.out.println(flag == 0 ? (Text + "\t\tarith_div") : Text + '=' + "\t\tarith_assig_div"); flag = 0; Text = ""; LF = text.charAt(position++); break; // % and %= case 18: state = 0; System.out.println(flag == 0 ? (Text + "\t\tarith_modulus") : Text + '=' + "\t\tarith_assig_modulus"); flag = 0; Text = ""; LF = text.charAt(position++); break; // ++ operator case 19: state = 0; System.out.println(Text + Text + "\t\tincrement_op"); Text = ""; LF = text.charAt(position++); break; // -- operator case 20: state = 0; System.out.println(Text + Text + "\t\tdecrement_op"); Text = ""; LF = text.charAt(position++); break; // < and <= case 21: state = 0; System.out.println(flag == 0 ? (Text + "\t\tless_than_op") : Text + "\t\tless_or_equal_op"); Text = ""; flag = 0; LF = text.charAt(position++); break; // > and >= case 22: state = 0; System.out.println(flag == 0 ? (Text + "\t\tgreater_than_op") : Text + '=' + "\t\tgreater_or_equal_op"); Text = ""; flag = 0; LF = text.charAt(position++); break; } System.out.flush(); } } }
答案 0 :(得分:2)
你的违规行,78:
while (input.hasNext()) {
这意味着您的输入变量为null。修复此问题 - 在使用之前将其初始化为有效的Scanner对象,可能使用inputFile:
File inputFile = new File("D:\\input.txt");
if (!inputFile.exists()) {
System.out.println("Input file, " + inputFile + ", does not exist.");
System.exit(0);
}
input = new Scanner(input); // ****** add this ******
这方面的关键教训是批判性地阅读您的异常消息,通常会告诉您问题的确切位置和位置,然后允许您修复它。例如,您提到的异常文本
Exception in thread "main" java.lang.NullPointerException
at First_Project.First_Project.LetsDoThis(First_Project.java:78)
at First_Project.First_Project.main(First_Project.java:27)
它应该再次键入你的First_Project.java文件的第78行和第27行。