尝试在空对象引用上调用虚拟方法'java.lang.String Model.User.getPhone()'

时间:2019-09-01 11:45:08

标签: java

问题是,当我单击购物车对象做出命令时,应用崩溃了,并抛出了NullPointerExcetion。

我的Cart.java代码是

public class Cart extends AppCompatActivity {

    RecyclerView recyclerView;
    RecyclerView.LayoutManager layoutManager;

    FirebaseDatabase database;
    DatabaseReference requests;

    TextView txtTotalPrice;
    FButton btnPlace;

    List<Order> cart = new ArrayList<>();
    CartAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cart);

        //Firebase
        database = FirebaseDatabase.getInstance();
        requests = database.getReference("Requests");

            public void onClick(DialogInterface dialogInterface, int i) {
                        Request request = new Request(
                        Common.currentUser.getPhone(),
                        Common.currentUser.getName(),
                        edtAddress.getText().toString(),
                        txtTotalPrice.getText().toString(),
                        cart
                );

                // send it to firebase
                requests.child(String.valueOf(System.currentTimeMillis()))
                        .setValue(request);
                //Delete cart
                new Database(getBaseContext()).cleanCart();
                Toast.makeText(Cart.this, "Thank you, Order is placed!", Toast.LENGTH_SHORT).show();
                finish();
            }
        });
    private void loadListFood() {
        cart = new Database(this).getCarts();
        adapter = new CartAdapter(cart, this);
        recyclerView.setAdapter(adapter);
    }
}

1 个答案:

答案 0 :(得分:0)

这表明您在使用Common.currentUser时尝试访问空对象。由于我们看不到如何定义此对象变量,因此很难确定其为null的确切原因。