-1

I have two buttons and an EditText in a fragment. When I click the addMusicButton, I want it to disappear and make newMusic(my EditText) and confirmAddButton appear. If I only change the visibility of the addMusicButton in the onClick method, it works, but if I try to change the visibility of newMusic or confirmAddButton in the onClick method, it gives me this error:

    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setVisibility(int)' on a null object reference

and my app crashes. I don't understand why it's saying my button is null because I initialized it right before I changed the visibility in the onClick method(the same thing happens if I try to change the visibility of the EditText). I am able to change the visibility of both my buttons and my EditText in onCreate, just not in onClick. I thought it might be because newMusic and confirmAddButton were in a different linear layout from addMusicButton, so I tried moving them all to one layout and it still gave me the same error.

This is my XML file:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <Button
            android:id="@+id/addMusicButton"
            android:layout_width="match_parent"
            android:layout_height="109dp"
            android:layout_weight="1"
            android:text="@string/add_music" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="109dp"
            android:layout_weight="1"
            android:orientation="horizontal">

            <EditText
                android:id="@+id/newMusic"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:width="330dp"
                android:autofillHints=""
                android:ems="10"
                android:gravity="left|center_vertical"
                android:inputType="textAutoComplete" />

            <Button
                android:id="@+id/confirmAddButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center|center_vertical"
                android:text="@string/add" />
        </LinearLayout>

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="585dp"
            android:layout_weight="1">

            <LinearLayout
                android:id="@+id/musicLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" />
        </ScrollView>
    </LinearLayout>

And my fragment code

public class MusicFragment extends Fragment implements View.OnClickListener {

    private MusicViewModel musicViewModel;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        musicViewModel = ViewModelProviders.of(this).get(MusicViewModel.class);
        View root = inflater.inflate(R.layout.fragment_music, container, false);
        Button addMusicButton = root.findViewById(R.id.addMusicButton);
        EditText newMusic = root.findViewById(R.id.newMusic);
        Button confirmAddButton = root.findViewById(R.id.confirmAddButton);

        newMusic.setVisibility(View.GONE);
        confirmAddButton.setVisibility(View.GONE);

        addMusicButton.setOnClickListener(this);
        confirmAddButton.setOnClickListener(this);

        return root;
    }


    @Override
    public void onClick(View view) {
        Button addMusicButton = view.findViewById(R.id.addMusicButton);
        Button confirmAddButton = view.findViewById(R.id.confirmAddButton);
        EditText newMusic = view.findViewById(R.id.newMusic);

        switch (view.getId()) {
            case R.id.addMusicButton:
                confirmAddButton.setVisibility(View.VISIBLE);
                newMusic.setVisibility(View.VISIBLE);
                addMusicButton.setVisibility(View.GONE);
                break;
            case R.id.confirmAddButton:
                (...)
                break;
        }
    }
}
Annie Li
  • 1
  • 1
  • Declaring a variable doesn't mean it won't be null. Try testing if it is null or not before you call one of its methods. You may find the error is somewhere else, like a bad reference to the view. – Michael McKay Jan 13 '20 at 03:50

3 Answers3

0

Instead of using separate initialization, you have to use only one initialization which is at onCreateView method only. And declare Button and EditText outside the method. Try this code:

Button addMusicButton, confirmAddButton ;
EditText newMusic;

In OnCreateView method :

addMusicButton = root.findViewById(R.id.addMusicButton);
newMusic = root.findViewById(R.id.newMusic);
confirmAddButton = root.findViewById(R.id.confirmAddButton);


addMusicButton.setOnClickListener(this);
confirmAddButton.setOnClickListener(this);
 @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.addMusicButton:
                confirmAddButton.setVisibility(View.VISIBLE);
                newMusic.setVisibility(View.VISIBLE);
                addMusicButton.setVisibility(View.GONE);
                break;
            case R.id.confirmAddButton:
                (...)
                break;
        }
    }
0

Why your again initialize Button addMusicButton , Button confirmAddButton ,EditText newMusic that is why your getting null pointer exception so please remove onclick initialize Button , EditText

ThavaSelvan
  • 123
  • 6
0

Declare the view(Button, EditText, TextView...) outside onCreateView method if you want to perform some operations. So, try the below code.

public class MusicFragment extends Fragment implements View.OnClickListener {

    private MusicViewModel musicViewModel;
    private Button addMusicButton, confirmAddButton;
    private EditText newMusic;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        musicViewModel = ViewModelProviders.of(this).get(MusicViewModel.class);
        View root = inflater.inflate(R.layout.fragment_music, container, false);
        addMusicButton = root.findViewById(R.id.addMusicButton);
        newMusic = root.findViewById(R.id.newMusic);
        confirmAddButton = root.findViewById(R.id.confirmAddButton);

        newMusic.setVisibility(View.GONE);
        confirmAddButton.setVisibility(View.GONE);

        addMusicButton.setOnClickListener(this);
        confirmAddButton.setOnClickListener(this);

        return root;
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.addMusicButton:
                confirmAddButton.setVisibility(View.VISIBLE);
                newMusic.setVisibility(View.VISIBLE);
                addMusicButton.setVisibility(View.GONE);
                break;
            case R.id.confirmAddButton:
                (...)
                break;
        }
    }
}
Vijay
  • 575
  • 4
  • 14