我正在完成这项工作,但发生了一些回滚,所以当我尝试使用 try-catch 写入文本文件时,它给了我这样的错误:
Exception in thread "main" java.lang.NullPointerException
它指向这条线:
output.format("%-10s%-20s%-20s%-20s%-20s%-20s%-20s%-20s%n", ....
我读到这可能会发生,因为引用的对象是null
什么。但是在输出行之前,我尝试用 输出所有值system.out.println()
,这没有问题。
有什么解决办法吗?
try {
record.setIncome(input.nextDouble());
record.setAtv(atv_sum);
record.setSumAtv(atv_sum+45.00);
DecimalFormat df = new DecimalFormat("#.###");
double soc_apd=(((record.getIncome()-record.getSumAtv())/simt)*soc_proc);
double iedz=((((record.getIncome()-record.getSumAtv()-soc_apd))/simt)*iedz_proc);
df.format(iedz);
double netto =record.getIncome()-(iedz+soc_apd);
if((record.getIncome()>=min_alga)){
output.format("%-10s%-20s%-20s%-20s%-20s%-20s%-20s%-20s%n",
year_n,
months[month_n],
df.format(record.getIncome()),
df.format(record.getAtv()),
df.format(record.getSumAtv()),
df.format(iedz),
df.format(soc_apd),
df.format(netto));
}else{
String error_1 = "Kļūda ! Alga ievadÄ«ta zem 0 vai minimÄ?lÄ?s algas "+min_alga+" LS";
System.err.println(error_1);
input.nextLine();
}
}
catch ( FormatterClosedException formatterClosedException ){
System.err.println(error_2);
return;
}
catch (NoSuchElementException elementException){
System.err.println(error_3);
input.nextLine();
}
我找到了导致问题的记录。输出是这样的:
所以导致 df.format(record.getIncome())
了问题
2013
45.0
Exception in thread "main" java.lang.NullPointerException
at course.CreateTextFile.addRecords(CreateTextFile.java:163)
at course.calc.CreateTextFileTest(calc.java:16)
at course.calc.main(calc.java:33)
321.468
165.55
1062.982
1550.0