Note that there are some explanatory texts on larger screens.

plurals
  1. POtry-catch exception handling java
    primarykey
    data
    text
    <p>I was on the way to finish this but some rollback happaned, so when I am trying to write in text file using try-catch it gives me such error:</p> <pre><code>Exception in thread "main" java.lang.NullPointerException </code></pre> <p>It is pointing to this line :</p> <pre><code>output.format("%-10s%-20s%-20s%-20s%-20s%-20s%-20s%-20s%n", .... </code></pre> <p>I read that can happen because the object referenced is <code>null</code> or something. But before the output line I tried to output all values with <code>system.out.println()</code> , and there was no problem with that.</p> <p>Is there some solution to that ?</p> <pre><code>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()&gt;=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(); } </code></pre> <p>I found which record couses the problem . Output is like this:</p> <p>so the <code>df.format(record.getIncome())</code> couses the problem</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload