繁体   English   中英

in onDataChange java.lang.String java.lang.Object.toString()' on a null object reference

[英]in onDataChange java.lang.String java.lang.Object.toString()' on a null object reference

我的代码应该检查 fire base 数据库中的订单 state 并向用户显示一条消息。

在这一行:

String shippingstate = snapshot.child("state").getValue().toString(); 

我遇到了 null object 参考致命错误和应用程序强制关闭。 我验证了数据库中的所有子值及其匹配,但仍然出现错误。

private void checkorderstate()
{
    DatabaseReference orderref = FirebaseDatabase.getInstance().getReference().child("orders");
    orderref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {

            if (snapshot.exists())
            {

                **String shippingstate = snapshot.child("state").getValue().toString();**
                String username = snapshot.child("name").getValue().toString();


                if (shippingstate.equals("shipped"))
                {
                    txttotalamount.setText("Dear "+ username+"\n order is shipped successfully");
                    recyclerView.setVisibility(View.GONE);
                    txtmsg1.setVisibility(View.VISIBLE);
                    txtmsg1.setText("Dear "+username+"\n Your order is completed and shipped successfully");
                    nextproccessbtn.setVisibility(View.GONE);
                    Toast.makeText(CartActivity.this, "You can put new order after your current order get shipped", Toast.LENGTH_LONG).show();
                }
                else if (shippingstate.equals("not shipped"))
                {
                    txttotalamount.setText("Dear "+ username+"Order State: not shipped");
                    recyclerView.setVisibility(View.GONE);
                    txtmsg1.setText("Dear "+username+"\n your order under proccess, please wait the order shipped massage. thank you");
                    nextproccessbtn.setVisibility(View.GONE);
                    Toast.makeText(CartActivity.this, "You can put new order after your current order get shipped", Toast.LENGTH_LONG).show();

                }
            }
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM