2013-12-21 18 views
-5

哪里是我的错误在这个项目? here is my source code编译程序..我有它的一些错误

编译程序

  • 后,我运行此代码,我创建它..它给了我这些错误:螺纹
  • 异常 “主” 显示java.lang.NullPointerException
  • 在First_Project.First_Project.LetsDoThis(First_Project.java:78)
  • 在First_Project.Firs t_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(); 
    } 
} } 
  • 谢谢你对我的帮助^ _^
+3

请注明线扔你的NPE(空指针异常)。该错误消息告诉你的行号,第27和78,和你要说明这些线给我们一些明显的意见,即'// ***** NPE这里*****' –

+0

这问题不会增加社区的价值。尝试学习调试。 –

回答

2

你的那一行,78:

while (input.hasNext()) { 

这意味着你的输入变量为空。解决这个问题 - 在使用之前将其初始化为有效的扫描对象,可能使用你的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) 

这又应该密钥在线路78和线路的First_Project.java文件27。