-1

Hi im trying to display customer orders that are added to a 'table' class from another class

public class PaymentScreenController {


    public ArrayList<Customer> allcustomers; // Defining allcustomers Arraylist




    public Table tbl; // Creating instance of Table class


    @FXML
    public void initialize() {

        allcustomers = tbl.getCustomers(); // Error seems to come from here when i assign allcustomers to the getcustomers method of table
        System.out.println(tbl.getCustomers());

        System.out.println("test");
        for (Customer customer : allcustomers) {

            System.out.println(customer.getCustomerorder());
        }


    }
}

any help would be appreciated thanks

** More code*

public void submitorder(ActionEvent actionEvent) throws IOException {
        //create a new UNIQUE customer to store that customers menu items
        Customer cust = new Customer();
        //add items to customer
        cust.getCustomerorder().addAll(order);
        // Adds the customer order to the table class
        tbl.getCustomers().add(cust);
        System.out.println(tbl.getCustomers());
        //clear menu choices screen
        lvOrder.getItems().clear();

        // Once added the desired number of customers move to next screen
        //extra customer was being added as a customer was put into the ArrayLust after the check happened.
        if (tbl.getCustomers().size() == noOfDiners) {
            Window mainWindow = btnSubmitOrder.getScene().getWindow();
            Parent newRoot = FXMLLoader.load(getClass().getResource("payment_screen.fxml"));
            mainWindow.getScene().setRoot(newRoot);
        }

    }

So the customer adds stuff to their order, and then the customer is added to the table class

jack1856
  • 1
  • 1
  • 4
    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) – Savior Apr 09 '20 at 14:45
  • Isn't it the same question as https://stackoverflow.com/questions/61105027/getting-specific-elements-from-arraylist/ – Arvind Kumar Avinash Apr 09 '20 at 15:09

2 Answers2

1

If an NPE raise at the line that you have mentioned in your comment, that means, when the method initialize() is called, variable tbl is not instantiated and hold null value.

And when you call tbl.getCustomers() it causes NullPointerException, since tbl is null here.

I don't see any code, that creates the object of Table and assigns it to the variable. Maybe it happens somewhere outside this class since your variable has public modifier, then please, make sure that this code called earlier than the initialize method.

You need to create an object of Table, like:

public Table tbl = new Table(..);

For the more, specific answer, please, share your Stack Trace here.

ish
  • 161
  • 7
0
public Table tbl; // Creating instance of Table class

Here, you just declared your Table and not create an instance of it.

To do that you must do tbl = new Table(insert arguments here);

That's why you have a null pointer

Quentin
  • 486
  • 4
  • 20
  • Hi But i add to the table class in another class, ive tried creating a new table here but becuase of that it makes a new empty one – jack1856 Apr 09 '20 at 14:33
  • If you add to the table in another class, it's another instance of the class Table. You can consider that the table in the class A where you put elements in it is another table than the one present in PayementScreenController – Quentin Apr 09 '20 at 14:34
  • Ah ok what arguements would be the parameters there then? Ive tried public Table tbl = new Table(); but i just get an empty table returned? – jack1856 Apr 09 '20 at 14:36