I am trying to import a list from a txt file, but the reader skips the first line and also adds the last "null" line that the file writer adds.

I fixed the null part but I can't seem to be able to add the first line in the list.

BufferedReader bf = new BufferedReader( new FileReader(importpath+"\\additional_info\\"+name+".txt"));
            //System.out.println(importpath+"\\"+name+".txt");
            String line = bf.readLine();
            listModel.removeAllElements();
            while (line != null) {
                line = bf.readLine();
                if(line == null){

                }else{
                    listModel.addElement(line);
                }
            }
            bf.close();
            System.out.println(listModel);
            clipsLineList.setModel(listModel);

Dani AI

Generated

Root cause: the first read is done before the loop but the loop immediately calls readLine() again, so the initial value is overwritten and never processed. already found that by moving the add step you get the first line added. pointed to the usual read-in-condition approach — both are valid; the important bit is to make sure each read result is actually processed exactly once.

A more robust, up-to-date pattern (handles trimming, empty lines, resource closing, and uses platform-independent paths) is shown below. It also keeps file I/O off the EDT and sets the Swing model on the EDT:

DefaultListModel<String> model = new DefaultListModel<>();
Path path = Paths.get(importPath, "additional_info", name + ".txt");

try (BufferedReader br = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
    br.lines()
      .map(String::trim)
      .filter(s -> !s.isEmpty())
      .forEach(model::addElement);
}

SwingUtilities.invokeLater(() -> clipsLineList.setModel(model));

Troubleshooting tips and cautions: avoid doing long reads on the Swing thread — use a SwingWorker or executor and update the model via invokeLater. If the file ended up containing the literal "null", check the writer code: writing an object that is null will produce that string. Prefer try-with-resources (shown above) to guarantee closure. If you want to keep the pre-read style, process the initial readLine() result first (add it to the model) and then loop reading the next line — that fixes the overwritten-first-line bug you originally had.

Recommended Answers

All 4 Replies

ok it was stupidly easy and I am still hitting my head on the wall, inside the while, the first line is line=bf.readLine() right? well with this it skips the first line so I simply needed to add this after the else and everything works

Just FYI the “standard” way to code this (saving duplicated reads and tests) is simply

while ((String line = br.readLine()) != null) {
   // process line
}

I know but for some reason it didn't seem to work as it should, I will try to refactor later, but first I want it to work.
Probably a netbeans freak out cause it sees it as a false code or something.

My mistake (typing while watching to), you need to declare String line before the while loop.

commented: Well either way I need to fix things up make it work as should and will refactor after that :P +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.