2014-02-07 64 views
0

我的程序存在問題,它是進程完成的,但它有運行時異常java.lang.NullPointerException。任何人都可以幫助我?這是我的代碼到目前爲止。在java.lang.NullPointerExceptions中遇到錯誤

import javax.swing.*; 
import java.lang.Character; 
import java.io.*; 
public class CoproHW 
{ 
    public static String c; 
    public static int NOc; 
    public static String vince; 
    public static void main(String args [])throws IOException 
    { 

     String vince = JOptionPane.showInputDialog("Enter Your File path :"); 
     c = JOptionPane.showInputDialog("Enter a character"); 

     briefer(); 
    } 
    public static void briefer() 
    { 

     for(int v = 1; v<c.length(); v++) 
     { 
      char x = c.charAt(v); 
      if(Character.isSpaceChar(x)) 
      { 
       NOc++; 
      } 


      char z = c.charAt(v); 
      if(Character.isLetter(z)) 
      { 
       NOc++; 
      } 
     } 

     panty(); 
    } 
    public static void panty() 
    { 
     File file = new File(vince); 

     if(!file.exists()) 
     { 
      JOptionPane.showMessageDialog(null,"Wrong file path !"); 
     } 
     else 
     { 
      JOptionPane.showMessageDialog(null, "The Number of Characters in "+ c +" is "+ NOc); 

      try 
      { 
       RandomAccessFile gui = new RandomAccessFile(file," "); 

       gui.writeBytes("The number of Characters in "+ c + " is " +NOc); 
       gui.close(); 
      } 

      catch(IOException m) 
      { 
       System.out.print(m.getMessage()); 
       System.exit(0); 
      } 
     } 
    } 
} 
+0

NOc在這裏是int,所以不能是異常的來源。請發佈堆棧跟蹤。我認爲你的意圖是做 File file = new File(c); 而不是 File file = new File(vince); – Kishore

回答

2

因爲您沒有爲您的全球vince變量賦值。相反,您指定的預期值局部變量:

String vince = JOptionPane.showInputDialog("Enter Your File path :"); 

代碼修改到:

vince = JOptionPane.showInputDialog("Enter Your File path :"); 

,然後它會工作。

0

用主要方法刪除String

vince = JOptionPane.showInputDialog("Enter Your File path :");

這是你已經被宣佈爲全局變量。

+0

我只是好奇,這是否添加了任何不是已經提供的答案,並已upvotes? – csmckelvey

+0

雅,我發佈我的答案後,我意識到這件事。不知道還有另一個答案,因爲當我閱讀這篇文章時,沒有答案發布。 – irvana