我的程序有问题,它是流程完成但是运行时异常java.lang.NullPointerException。有人可以帮帮我吗?到目前为止,这是我的代码。
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 :(得分:2)
因为您没有为全局vince
变量指定值。而是将期望值分配给局部变量:
String vince = JOptionPane.showInputDialog("Enter Your File path :");
将代码修改为:
vince = JOptionPane.showInputDialog("Enter Your File path :");
然后它会起作用。
答案 1 :(得分:0)
删除主要方法的String
。
vince = JOptionPane.showInputDialog("Enter Your File path :");
这是您已声明的全局变量。