0

I am trying to populate a jTable from a .dat file in Java. However, it is not going well. I have tried every solution I have found on the internet and I am still getting a NullPointerException. I need to code to iterate through the file, convert each line to an array (or some other type of collection), display the array in 1 row of the jTable and then move on to the next line in the file and repeat the steps. Here is the code I have so far:

private void displayEmployees()
{
    String line = null;
    File employeeFile = null;
    BufferedReader br = null;
    String[] record;

    try
    {
        employeeFile = new File("Employees.dat");
        br = new BufferedReader(new FileReader(employeeFile));
        record = new String[]{};
        while (!(line = br.readLine()).equals(""))
        {
            record = line.split(", ");
            employeeTable.addRow(record);
        }
    }
    catch (Exception ex)
    {
        JOptionPane.showMessageDialog(null, "Unable to display employee details " 
                + ex.toString(), "Information Retrieval Failed", JOptionPane.ERROR_MESSAGE);
    }
    finally
    {
        try
        {
            br.close();
        }
        catch (Exception ex)
        {
            JOptionPane.showMessageDialog(null, "Unable to close the connection to"
                    + " the file", "Operation Failed", JOptionPane.ERROR_MESSAGE);
        }
    }
}

Although I know what a NullPointerException is, I can't figure out what is causing it within my specific code. After looking through all the related questions I could find on stackoverflow and elsewhere on the internet, I still can't find a solution that works. Can anyone help me with this please?

Any help would be greatly appreciated.

  • 1
    There's not enough information available to determine why you're getting a NPE. Since this is going to happen to you an a frequent bases, you should spend the time to learn some of the tools to solve it – MadProgrammer Jun 19 '18 at 00:28
  • Probably better to split on comma only – Scary Wombat Jun 19 '18 at 00:31
  • `br.readLine()` returns `null` for end of stream, and you call `equals` on it. You need to properly handle the return value of `readLine()`. https://docs.oracle.com/javase/9/docs/api/java/io/BufferedReader.html#readLine-- – Radiodef Jun 19 '18 at 02:00

0 Answers0