0

I am trying to get data from firebase but when I check it, I get null values everytime and it shows NullpointerException on the below mentioned line. I have already tried the same method in many apps and it worked fine in them. I cannot just understand where is the actual problem.

Here is the code:


        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();
                }


                }

        });

    }
Faisal Nazir
  • 51
  • 1
  • 8

1 Answers1

0

It is clearly showing that the string ab and bc are not getting any values from the previous activity.Make sure you are using exact key names and try again.If possible,post the code of the activity from which you are sending to this activity.So the problem is not with the firebase or database related code.

Thrishool MSM
  • 337
  • 3
  • 11