2014-10-19 6 views
-2

이 예외의 원인은 해결할 수 없습니다. reRead() 메서드에서 발생합니다.java.lang.NullPointerException

reRead() 메서드를 사용하지 않고 테스트 한 결과 모든 것이 정상적으로 작동합니다.

public class SerFiles { 

    private ObjectInputStream in; 
    private ObjectOutputStream out; 
    private FileReader fr; 
    private FileWriter fw; 
    private BufferedReader br; 
    private BufferedWriter bw; 
    private StringTokenizer token; 
    private ArrayList<Product> prod = new ArrayList<Product>(); 
    private String line = "c"; 
    private Product proc; 

    private int a,b,d,e,f; 
    private String c; 

    public SerFiles(){ } 

    public void openFiles() 
    { 
     try 
     { 
      out = new ObjectOutputStream(new FileOutputStream("prod.ser")); 
      fr = new FileReader("SalesDelim.txt"); 
      br = new BufferedReader(fr); 

      System.out.println("OPEN SUCCESS"); 
     } 
     catch(Exception ex) 
     { 
      System.out.println(ex.toString()); 
     } 
    } 

    public void readAndWrite() 
    { 
     try 
     { 
      line = br.readLine(); 

      while(line != null) 
      { 
       if(line != null) 
       { 
        token = new StringTokenizer(line, "**"); 

        a = Integer.parseInt(token.nextToken()); 
        b = Integer.parseInt(token.nextToken()); 
        c = token.nextToken(); 
        d = Integer.parseInt(token.nextToken()); 
        e = Integer.parseInt(token.nextToken()); 
        f = Integer.parseInt(token.nextToken()); 

        prod.add(new Product(a,c,e,f)); 
        line = br.readLine(); 
       } 
      } 

      for(int i = 0; i<prod.size(); i++) 
      { 
       out.writeObject(prod.get(i)); 
      } 

      System.out.println("WRITE SUCCESS"); 
     } 
     catch(Exception ex) 
     { 
      System.out.println(ex.toString()); 
     } 
    } 

    public void reRead() 
    { 
     try 
     { 
      in = new ObjectInputStream(new FileInputStream("prod.ser")); //////ERROR HAPPENS HERE 

      while(true) 
      { 
       proc = (Product)in.readObject(); 
       System.out.println(proc.toString()); 
      } 
     } 
     catch(EOFException ioe){ 
      return; 
     } 
     catch(Exception ex) 
     { 
      System.out.println(ex.toString()); 
     } 
    } 

    public void closeFiles() 
    { 

     try 
     { 
      fr.close(); 
      br.close(); 
      out.close(); 
      in.close(); 

      System.out.println("CLOSE SUCCESS"); 
     } 
     catch(Exception ex) 
     { 
      System.out.println(ex.toString()); 
     } 
    } 
} 

reRead() 메소드없이 테스트 한 결과 정상적으로 작동했습니다.

당신에게

+0

NullPointerException이 발생하지 않는다고 생각합니다. stacktrace를 게시하십시오. 코드를 형식화하십시오. 관련 부품 만 게시하십시오. –

+1

'System.out.println (ex.toString());'하지 마라. 'ex.printStackTrace()'를 수행하십시오; 더 유용한 정보를 제공합니다. – Pokechu22

답변

0

시도 감사 :

in = new ObjectInputStream(new FileInputStream("prod.ser")); 

while (true) 
{ 
    proc = (Product)in.readObject(); 
    System.out.println(proc); // change!! 

    if (proc == null) break; // quit the loop 
} 

그래서, 문제는 당신이 null -pointer에 toString()를 호출 한 것이 었습니다.

관련 문제