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.