0

Text file:

32|Chan|2019-11-29|14:12:01.346

Code:

DefaultListModel<String> listModel;
public LogActivity() {

    initComponents();

    LoginInfo login = new LoginInfo();
    try {
        FileReader fr = new FileReader(login.getLogoutRecord());
        BufferedReader br = new BufferedReader(fr);
        String read = br.readLine();
        
        while(read != null)
        {
            listModel.addElement(read);//add data to list model object
        }
        logoutList.setModel(listModel);//store  listmodel to logoutlist object
    } catch (FileNotFoundException ex) {
        Logger.getLogger(LogActivity.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(LogActivity.class.getName()).log(Level.SEVERE, null, ex);
    }
}

I'm trying to display my text file into a Jlist, but I keep on getting this exception.

Error: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Angel Chan
  • 25
  • 3
  • the link you sent is not about jlist. – Angel Chan Nov 30 '19 at 09:20
  • you are not initializing your list model, it is null, so you get NullPointerException when trying to `addElement` to it. try `DefaultListModel listModel = new DefaultListModel<>();` – guleryuz Nov 30 '19 at 09:25
  • 1
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Curiosa Globunznik Nov 30 '19 at 09:27
  • yes, thank you. But now the GUI doesn't show up at all. – Angel Chan Nov 30 '19 at 09:35
  • Note that in the code you provided the file is not advanced. You don't read each line. You just read a single line and check in the while loop if it is null. If it is not, you enter the while loop and the whole process repeats, never leaving the loop. – gthanop Dec 22 '19 at 22:04
  • *"But now.."* .. you should ask a new question. Add a [mre]. – Andrew Thompson Jun 28 '21 at 12:50

0 Answers0