java.lang.NullPointerException?在将get方法赋值给一个ArrayList时

huangapple go评论64阅读模式
英文:

java.lang.NullPointerException? When assigning get method to an arraylist

问题

你好,我正在尝试从另一个类向名为 'Table' 的类添加的顾客订单。

public class PaymentScreenController {

    public ArrayList<Customer> allcustomers; // 定义 allcustomers ArrayList

    public Table tbl; // 创建 Table 类的实例

    @FXML
    public void initialize() {

        allcustomers = tbl.getCustomers(); // 错误似乎来自这里,当我将 allcustomers 赋值给 table 的 getCustomers 方法时
        System.out.println(tbl.getCustomers());

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

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

    }
}

任何帮助都将不胜感激,谢谢。

更多代码

public void submitorder(ActionEvent actionEvent) throws IOException {
    // 创建一个新的唯一顾客以存储该顾客的菜单项目
    Customer cust = new Customer();
    // 向顾客添加项目
    cust.getCustomerorder().addAll(order);
    // 将顾客订单添加到 table 类
    tbl.getCustomers().add(cust);
    System.out.println(tbl.getCustomers());
    // 清除菜单选择屏幕
    lvOrder.getItems().clear();

    // 一旦添加了所需数量的顾客,就进入下一个屏幕
    // 由于在检查发生后将顾客放入 ArrayList 中,因此会添加额外的顾客。
    if (tbl.getCustomers().size() == noOfDiners) {
        Window mainWindow = btnSubmitOrder.getScene().getWindow();
        Parent newRoot = FXMLLoader.load(getClass().getResource("payment_screen.fxml"));
        mainWindow.getScene().setRoot(newRoot);
    }
}

所以顾客添加了订单内容,然后将顾客添加到 table 类中。

英文:

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

public class PaymentScreenController {


    public ArrayList&lt;Customer&gt; 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(&quot;test&quot;);
        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(&quot;payment_screen.fxml&quot;));
            mainWindow.getScene().setRoot(newRoot);
        }
        
    }

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

答案1

得分: 1

如果在您评论中提到的那一行出现了NPE(NullPointerException)错误,这意味着当调用方法initialize()时,变量tbl未被实例化并且保持了null值。

当您调用tbl.getCustomers()时会导致NullPointerException,因为此处tbl为空。

我没有看到任何代码创建了Table对象并将其赋值给变量。也许这在该类之外的某个地方发生,因为您的变量具有public修饰符,那么请确保此代码在initialize方法之前被调用。

您需要创建一个Table对象,例如:

public Table tbl = new Table(..);

为了得到更详细的答案,请在这里分享您的堆栈跟踪(Stack Trace)。

英文:

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.

答案2

得分: 0

public Table tbl; // 创建 Table 类的实例

在这里,你只是声明了一个 Table 变量,而没有创建它的实例。

要实现这一点,你需要执行 `tbl = new Table(在这里插入参数);`

这就是为什么你会得到空指针错误。
英文:
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

huangapple
  • 本文由 发表于 2020年4月9日 22:28:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/61123496.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定