空指针异常

时间:2013-09-06 15:25:34

标签: java nullpointerexception

我的代码中有一个空指针异常。究竟什么是空指针?

我该如何解决这个问题?当我删除if条款时,NPE将会消失。那是为什么?

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class InToPost {
   private Stack theStack;
   private String input;
   private String output = "";
   public InToPost(String in) {
      input = in;
      int stackSize = input.length();
      theStack = new Stack(stackSize);
   }
   public String doTrans() {
      for (int j = 0; j < input.length(); j++) {
         char ch = input.charAt(j);
         switch (ch) {
            case '+': 
            case '-':
            gotOper(ch, 1); 
            break; 
            case '*': 
            case '/':
            gotOper(ch, 2); 
            break; 
            case '(': 
            theStack.push(ch);
            break;
            case ')': 
            gotParen(ch); 
            break;
            default: 
            output = output + ch; 
            break;
         }
      }
      while (!theStack.isEmpty()) {
         output = output + theStack.pop();
      }
      System.out.println(output);
      return output; 
   }
   public void gotOper(char opThis, int prec1) {
      while (!theStack.isEmpty()) {
         char opTop = theStack.pop();
         if (opTop == '(') {
            theStack.push(opTop);
            break;
         }
         else {
            int prec2;
            if (opTop == '+' || opTop == '-')
            prec2 = 1;
            else
            prec2 = 2;
            if (prec2 < prec1) { 
               theStack.push(opTop);
               break;
            }
            else
            output = output + opTop;
         }
      }
      theStack.push(opThis);
   }
   public void gotParen(char ch){ 
      while (!theStack.isEmpty()) {
         char chx = theStack.pop();
         if (chx == '(') 
         break; 
         else
         output = output + chx; 
      }
   }

   class Stack {
      private int maxSize;
      private char[] stackArray;
      private int top;
      public Stack(int max) {
         maxSize = max;
         stackArray = new char[maxSize];
         top = -1;
      }
      public void push(char j) {
         stackArray[++top] = j;
      }
      public char pop() {
         return stackArray[top--];
      }
      public char peek() {
         return stackArray[top];
      }
      public boolean isEmpty() {
         return (top == -1);
     }
   }
      public static void main(String[] args) 
   throws IOException {
       BufferedReader read = new BufferedReader (new FileReader("C:\\Users\\Josh\\Desktop\\test.txt"));
       String str = "";
        String s;
        char  in;
        int x=0;
         stack b=new stack();
        char[] store=new char[10];
         Scanner insert=new Scanner(System.in);
        while ((str=read.readLine())!= null)
        {
       if (str.contains("print"))
       {
           System.out.println(str);
       }
       else if (str.contains("read"))
       {
            in=insert.next().charAt(0);
                store[x]=in;
                 x++;
       }

       else if (str.contains("="))
       {String input = "";
      String output;
      input = read.readLine();
      input = input.replace(";", "");
      InToPost theTrans = new InToPost(input);
      output = theTrans.doTrans(); 
      System.out.println("Postfix is " + output + '\n');
       }
   }
}}

它说nullpointerexeption在第126行。 我该怎么做才能解决这个问题?

3 个答案:

答案 0 :(得分:0)

首先,请参阅guido的评论。

其次你的代码很乱。修理它。也许你应该指出哪一行是126。

复制并粘贴代码我将其作为第126行:

input = input.replace(";", "");

如果输入为空,或者它什么都没有,那么就不能调用方法替换它。就像告诉比萨饼送货员一样,当你独自生活时,你的室友会支付比萨饼。

请注意,您的实际行126可能与您在此处复制和粘贴的内容不同。您也可以尝试使用谷歌搜索错误消息。

答案 1 :(得分:0)

缓冲的阅读器在文件末尾返回null。

因此,第126行的“input”字符串为null。您应该在readLine()方法之后检查结果,就像在while循环中一样。

input = read.readLine();

答案 2 :(得分:0)

正如其他回应中所解释的那样,唯一的原因是:

  

基于documentation,BufferedReader.readLine()仅在到达流的末尾时才返回null。这意味着如果第一次调用readLine()返回null,则输入流中没有任何内容可以开始。

因此,您的文件未找到或无法读取

相关问题