0

I'm trying to make a mini mobile wallet which stores users' details on firebase. I'm getting the error

Attempt to invoke virtual method 'void android.widget.TextView.setText(int)' on a null object reference

while using the setText() method to show user's balance. I've referred to many other similar questions on stackoverflow, but nothing helped me out.

Here are code files I think I have problem with.

The below code if for a fragment (wallet_info.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:tools="http://schemas.android.com/tools"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         tools:context=".PassBook">

<!-- TODO: Update blank fragment layout -->

<TextView
    android:layout_marginTop="250dp"
    android:layout_marginLeft="30dp"
    android:layout_marginRight="30dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textSize="25dp"
    android:id="@+id/walletText"
    android:text="Wallet Balance"/>

<TextView
    android:layout_margin="30dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textSize="40dp"
    android:layout_below="@id/walletText"
    android:id="@+id/balanceText"/>

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/balanceText"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="32dp"
    android:text="Request Balance"
    android:onClick="onRequest"/>

 </RelativeLayout>

The android:onClick="onRequest" shows a red underline asking me to create a public method even though I've created the method in the class on which the fragment is attached.

The below code is for home.java (a class on which wallet_info.xml fragment is attached.) I'm pasting the part where I'm getting the error.

public void onRequest(View view){
    tv = (TextView)findViewById(R.id.balanceText);
    wallet.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            wallet_info u = dataSnapshot.child(m).getValue(wallet_info.class);
            tv.setText(u.getBalance());
            //tv.setText(u.getBalance());
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

The line tv.setText(u.getBalance()); throws the mentioned error.

Thank you in advance.

  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Henry Apr 27 '18 at 03:35

2 Answers2

0

This is because you can't use the following attribute:

android:onClick="onRequest"

inside a layout for a Fragment. It's only work for an Activity. Hence, you need to bind the balanceText view and create the View.onClickListener for button2 button.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
0

Though questions like this on stackoverflow didn't have the answer I needed, I came up with the solution. I don't know why it shows a NULL POINTER EXCEPTION even when I created an object of TextView. The error got solved by changing the data type of parameter in setText() to string.

The problem got resolved after changing the code as follows.

wallet_info u = dataSnapshot.child(m).getValue(wallet_info.class);
tv.setText(u.getBalance());

The above lines of code were changed to -

wallet_info u = dataSnapshot.child(m).getValue(wallet_info.class);
g = String.valueOf(u.getBalance());
tv.setText(g);

This solved my problem.

  • Yeah, the `TextView#setText(int)` method expects that the int passed is a string resource id like R.string.string_name. That's a pretty wild error message if that was the only problem, though. – drdaanger Apr 30 '18 at 19:51