for(File d : documents)
{
if(d.isFile())
count++;
{
BufferedReader inputStream = null;
try
{
inputStream = new BufferedReader(new FileReader(d));
String line;
while ((line = inputStream.readLine()) !=null)
{
//condition to check the hyphen at end of line
if(line.charAt(line.length() -1) == 45)
{
line = line.replace(line.charAt(line.length() -1),' ');
String line2 = inputStream.readLine();
line = line.trim()+line2;
}
System.out.println(line);
}
finally
{
try
{
if(inputStream != null)
inputStream.close();
}
catch(IOException e)
{
}
}
}
// System.out.println("\n" + tokens1);
//System.out.println("\n" + count);
}
}
catch(Exception e)
{
System.out.println("Null point exception");
}
当我删除条件以检查连字符时,它会读取文件中的所有行并在结尾处显示空指针异常。当我包含这个条件时,它会读取文件,但每当它找到第一个空行时,它就会停止并抛出空指针异常。
答案 0 :(得分:0)
当你尝试readLine inside if时,你没有进行任何空检查,这就是为什么它会导致NPE
while ((line = inputStream.readLine()) !=null)
{
//condition to check the hyphen at end of line
if(line.charAt(line.length() -1) == 45)
{
line = line.replace(line.charAt(line.length() -1),' ');
String line2 = inputStream.readLine(); // No null checking is being made here
line = line.trim()+line2;
}
System.out.println(line);
}