2013-09-06 3 views
-1

이 코드에서 널 포인터 예외가 있습니다. null 포인터 란 정확히 무엇입니까?널 포인터 예외

어떻게 해결할 수 있습니까? 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

라인 (126) 어느 하나를 읽을 수 없습니다? –

+6

불쾌감은 없지만 누군가가 자신의 Stack 구현을 작성하고, FileReader와 BufferedReader를 사용하여 NPE가 무엇인지 물어 보는 것이 좀 재미 있습니다. –

+1

나는 수업에서 나타나지 않고 친구에게서 베낀 후에 숙제를 짐작하고있다. –

답변

0

먼저 guido의 의견을 참조하십시오.

두 번째로 코드가 엉망입니다. 고쳐. 아마 당신은 (126)

복사 인 라인 지적하고 코드를 붙여해야 내가 선으로 이것을 가지고 126 :

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

입력이 null의 경우, 또는 아무것도를 가리키는 경우에, 당신은 호출 할 수 없습니다 그것을 대체하는 방법. 그것은 당신이 혼자 살 때 룸메이트가 피자 대가를 치를 것이라고 피자 배달원에게 말하는 것과 같습니다.

실제 줄 126은 복사하여 여기에 붙여 넣은 것과 다를 수 있습니다. 또한 오류 메시지에 대해 Google 검색을 시도 할 수도 있습니다.

0

버퍼링 된 판독기가 파일 끝에 null을 반환합니다.

따라서 126 행의 "input"String은 null입니다. while 루프에서와 같은 방식으로 readLine() 메서드 다음에 결과를 확인해야합니다. 다른 반응에서 설명 된 바와 같이

input = read.readLine(); 
0

이 유일한 이유는 경우 : documentation 기준

상기 BufferedReader.readLine()는 스트림의 끝에 도달 널 때만 반환한다. 즉, readLine()에 대한 첫 번째 호출이 null을 반환하면 입력 스트림에 아무것도 표시되지 않습니다.

그래서, 당신의 파일 위치하지 않았거나