L
L
Lord Drous2017-02-01 20:42:29
Java
Lord Drous, 2017-02-01 20:42:29

The program throws nullPointerException, what should I do?

Hello, working with excel, the task is as follows: you need to find those who have more than n experience. the program is ready, in fact everything should work, but it swears at 2 lines of
Main.java:

public class Main {
    HSSFWorkbook workbook = null;
    List<Employee> list = new ArrayList<>();


    public static void main(String[] args) {
        Main m = new Main();
        m.getData();
        m.showWithSeniorityNotLessThan(3);
    }

    // получаем дату, записываем ее в list в виде класса Employee
    public void getData(){
        try {
            workbook = new HSSFWorkbook(new FileInputStream("E:\\Book.xls"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Sheet sheet = workbook.getSheetAt(0);
        int limit = sheet.getLastRowNum();
        for (int i = 1; i <= limit; i++) {
            Row row = sheet.getRow(i);
            String name = row.getCell(0).getStringCellValue();
            int seniority = (int) row.getCell(1).getNumericCellValue();
            int salary = (int) row.getCell(2).getNumericCellValue();
            String appointment = row.getCell(3).getStringCellValue();
            list.add(new Employee(name, seniority, salary, appointment));
        }
    }

    // пробегаем по списку, выводим если подходит
    public void showWithSeniorityNotLessThan(int limit){
        for (Employee e : list){
            if (e.getSeniority() >= limit) System.out.println(e);
        }
    }

}

Employee.java
class Employee {
    private String name;
    private int seniority;
    private int salary;
    private String appointment;

    public Employee(String name, int seniority, int salary, String appointment) {
        this.name = name;
        this.seniority = seniority;
        this.salary = salary;
        this.appointment = appointment;
    }

    @Override
    public String toString() {
        return name + " | "  +
                seniority + " | "
                + salary + " | "
                + appointment;
    }

    public int getSeniority() {
        return seniority;
    }
}

mistake:
Exception in thread "main" java.lang.NullPointerException
  at Main.getData(Main.java:38)
  at Main.main(Main.java:23)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

Process finished with exit code 1

Answer the question

In order to leave comments, you need to log in

3 answer(s)
B
breakoffbrain, 2017-02-02
@breakoffbrain

it would be nice to see a listing with line numbers)

D
Dmitry, 2017-02-04
@dsv

apparently this is a string String name = row.getCell(0).getStringCellValue();
row or row.getCell(0) null?

R
Roman, 2017-02-17
@Terran37

Note that the line numbers are given in the error.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question