-1

I want to gather what's in one listview. Write that information to a file. Read the file and display it's contents back into another listview. Currently I am struggling with accessing a listview in another class to be able to iterate through it and write to a file. Listview "temp" is returning null.

public void writeToFile(String file, Controller controller) throws IOException {
        FileWriter fw = new FileWriter(file, true);
        BufferedWriter bw = new BufferedWriter(fw);
        ListView<Items> temp = controller.getListtaking();
        System.out.print(temp);
        for (int i = 0; i < temp.getItems().size(); i++) {
            bw.write(temp.getItems().get(i) + ",\r");
        }
        bw.write(totalorder+ "\r");
        bw.write(";\r");
        bw.close();
    } 
       // This is one class

Controller controller;
    public ListView<Items> getListtaking() {
       return listtaking;
    }
    public void OrderIn(ActionEvent actionEvent) throws IOException {
        Orders orderin = new Orders(total);
        totalorder = total;
        System.out.println(orderin.totalcost);
        System.out.println(orderin);
        System.out.println(listtaking.getItems());
        listcurrent.getItems().add(orderin);
        listorder = listtaking;
        String FileName = String.valueOf(Orders.ordernumber);
        new File(FileName + ".txt");
        ObservableList<Items> myList = listtaking.getItems();
        for (Items i : myList) {
            i.writeToFile(FileName,controller);
        }
        listtaking.getItems().clear(); // This is another class

`

Cristian
  • 9
  • 1
  • I'm not very familiar with JavaFX, but what I do in this cases os to pass the content of the listview to the other controller by creating a "setup" (or whatever you want to call it) method. So just before I load the scene, I get its controller and call the setup method and pass whatever I will need there (in your case, the content of the listview). Hope it helps! – Calfa Feb 17 '23 at 09:11
  • 1
    looks like plain java, nothing special to fx - for communication between classes, one of them needs a reference to the other. Better than communication via the view (that is access the listView directly) is to organize the interaction via a model (both/all view classes have a common data class they talk to). See f.i. https://stackoverflow.com/questions/32342864/applying-mvc-with-javafx to learn how to do it. Unrelated: stick to java naming conventions please when showing java code publicly. – kleopatra Feb 17 '23 at 10:50

1 Answers1

-2

I don't see anywhere where you are declaring listtaking:

listtaking = new ListView<Items>();

Maybe that's an issue.

Riho
  • 4,523
  • 3
  • 33
  • 48
  • 2
    well, it's a mere snippet, not an [mcve] as it should be - so we can't know whether or not the listView ever is instantiated :) Plus in the context of fx - seeing one class described as "Controller" - the view probably is injected via fxml in which case it _must not_ be instantiated. Without more details in the question, one guessing game is as good or bad as the other, though. – kleopatra Feb 17 '23 at 11:37