无法从Firebase和NullPointerException检索数据

时间:2019-08-02 17:53:59

标签: android firebase nullpointerexception

我正在尝试从firebase中获取数据,但是当我检查它时,每次都会获得null值,并且在下面提到的行上显示NullpointerException。我已经在许多应用程序中尝试过相同的方法,但在它们中效果很好。我不明白真正的问题在哪里。

代码如下:


        final DatabaseReference ref = FirebaseDatabase.getInstance ( ).getReference ( );


        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource (this, R.array.year, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource (android.R.layout.simple_spinner_dropdown_item);
        sp1.setAdapter (adapter);
        sp1.setOnItemSelectedListener (this);

        ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource (this, R.array.month, android.R.layout.simple_spinner_item);
        adapter1.setDropDownViewResource (android.R.layout.simple_spinner_dropdown_item);
        sp2.setAdapter (adapter1);
        sp2.setOnItemSelectedListener (this);

        runTask ();

        btn1.setOnClickListener (new View.OnClickListener ( ) {
            @Override
            public void onClick(View v) {
                rno = ed1.getText ( ).toString ( );
                if(net==1) {
                    DatabaseReference onlineattmgr = ref.child (year).child (month).child (rno).child ("TL");

                    onlineattmgr.addListenerForSingleValueEvent (new ValueEventListener ( ) {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                            tl = dataSnapshot.getValue (String.class);
                        }


                        @Override
                        public void onCancelled(@NonNull DatabaseError databaseError) {
                            Toast.makeText (MainActivity.this, "Unable to fetch data from database. Please try Again!", Toast.LENGTH_LONG).show ( );
                        }
                    });

                    DatabaseReference onlineattmgr1 = ref.child (year).child (month).child (rno).child ("LA");

                    onlineattmgr1.addListenerForSingleValueEvent (new ValueEventListener ( ) {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                            la = dataSnapshot.getValue (String.class);

                        }

                        @Override
                        public void onCancelled(@NonNull DatabaseError databaseError) {
                            Toast.makeText (MainActivity.this, "Unable to fetch data from database. Please try Again!", Toast.LENGTH_LONG).show ( );
                        }
                    });

//On below line it shows NullPointerException
                    if(tl.length ()!=0 && la.length ()!=0) {
                    tv1.setText (tl);
                    tv2.setText (la);
                    int x = Integer.parseInt (tl);
                    int y = Integer.parseInt (la);
        float z = (x / y) * 100;
        String zz = String.valueOf (z);
        tv3.setText (zz);
        }
                }else {
                    Toast.makeText(MainActivity.this, "Network Unavailable!", Toast.LENGTH_LONG).show();
                }


                }

        });

    }

1 个答案:

答案 0 :(得分:0)

清楚地表明,字符串ab和bc没有从上一个活动中获取任何值。请确保您使用的是正确的键名,然后重试。如果可能,请发送您要发送到的活动的代码因此,问题不在于与Firebase或数据库相关的代码。