0

I've been trying to import a csv file (containing empty fields) by selecting the file with a filechooser into my database. Using the filechooser is important as it is a program to be used by a school and they want to be able to import their new student records every year by importing the excel/csv file they have. Whenever I run the code given below I get the following error:

SEVERE: null
java.lang.NullPointerException
at gui.FXMLStudentController$1.run(FXMLStudentController.java:86)
at java.lang.Thread.run(Thread.java:745)

Question is obvious I think. How do I make it work without the error?

Importer class:

public class ImportStudents
{
private File file;
private List<Student> students = new ArrayList<>();

public ImportStudents(File file) throws IOException 
{
    this.file = file;
}

public List importStudents() throws FileNotFoundException, IOException
{
   try(CSVReader reader = new CSVReader(new FileReader(file), ';'))
   {
   String[] nextLine;
   boolean notFirst = false;
   while ((nextLine = reader.readNext()) != null) {
        if (notFirst) {

             students.add(new Student(nextLine[3], nextLine[1], nextLine[0],nextLine[2]));
        }
        notFirst = true;
    }

   }catch(Exception e)
           {
               e.printStackTrace();
           }
    return students;
}
}

Code in GUI for when import button is pressed:

   @FXML
private void importeer(ActionEvent event)
{

    Stage stage = new Stage();
    ImportStudents = importStudents; //importStudents created earlier in the class
    FileChooser fileChooser = new FileChooser();
    fileChooser.setTitle("Open File");
    try
    {
        importStudents = new ImportStudents(fileChooser.showOpenDialog(stage));
        new Thread(new Runnable() {

            @Override
            public void run() 
            {
                try
                {
                    repository.importStudents(importStudents.importeerLeerlingen());
                }
                catch(Exception e)
                {
                    Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, e);
                }
            }
        }).start();

    }
    catch(Exception e)
    {

    }
}

Code in the repository:

  public void importStudents(List<Student> students)
{
    try{
        em.getTransaction().begin();
        for (Student : students) 
        {
           em.persist(student);
        }
        em.getTransaction().commit();
    }
    finally
    {
        em.close();
    }
}

Example from csv file I try to import this way: As you can see the email is empty most of times (it's for a kindergarten school) but for some it is given.

 SurName;Name;E-mail;Class
 Agacseven;Tuana;;3KA
 Ahmedov;Arman;;2KC
 Akcan;Efe;;3KA
 Akcan;Hanzade;;2KC
 Akhtar;Hussain;;1KA

Student constructor looks like this

public Student(String class, String name, String surNaam, String email) 
{

    this.class = class;
    this.name = name;
    this.surNaam = surNaam;
    this.email = email;
}
Chris Martin
  • 30,334
  • 10
  • 78
  • 137
user2270715
  • 23
  • 1
  • 1
  • 4

1 Answers1

0

How does the Student constructor look? I have hard time finding in the javadocs what valued readNext() puts in the string reference if there is nothing between delimiters. It could be either an empty string or a null (which it seems to be). In that case it might be that what you do in the Student constructor with that value is not legal with null value.

EDIT

if that is the case you could either handle the passing of a null value within the constructor or maybe write a static method that would be something like:

public static String Convert(String str) {
    return str == null ? "" : str;
}

and when instantiating a student do:

new Student(Convert(nextLine[3]), ... );
CrookedBadge
  • 130
  • 9