pra*_*.ps 0 java nullpointerexception
这是用于对现有文件的内容进行排序的代码:
public void sortFile(String fileName)throws IOException
{
FileReader fin=new FileReader("C:\\File Handling\\"+fileName+".txt");
BufferedReader bin=new BufferedReader(fin);
String[] str=new String[100];
int i=0;
while((str[i]=bin.readLine())!=null)
i++;
Arrays.sort(str); //getting a NullPointerException here
FileWriter fout=new FileWriter("C:\\File Handling\\"+fileName+".txt");
BufferedWriter bout=new BufferedWriter(fout);
PrintWriter pout=new PrintWriter(bout);
for(i=0;i<str.length;i++)
pout.println(str[i]);
}
Run Code Online (Sandbox Code Playgroud)
但是,在IDE中执行代码时,我得到了一个 java.lang.NullPointerException
有人可以告诉我为什么会这样吗?我无法弄清楚.
你应该试试:
Arrays.sort(str, 0, i);
Run Code Online (Sandbox Code Playgroud)
该数组包含空字符串,这些导致NullPointerException.
| 归档时间: |
|
| 查看次数: |
86 次 |
| 最近记录: |