-1

I use childFragmentManager then

java.lang.Class java.lang.Object.getClass()

has this error

It's my first fragment code and I have four child Fragment.

public class Main2Fragment extends Fragment {

    Main2Season main2Season;
    Main2Festival main2Festival;
    Main2Local main2Local;
    Main2Parking main2Parking;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_main2, container, false);

        view.findViewById(R.id.season).setOnClickListener(mListener);
        view.findViewById(R.id.festival).setOnClickListener(mListener);
        view.findViewById(R.id.local).setOnClickListener(mListener);
        view.findViewById(R.id.parking).setOnClickListener(mListener);


        return  view;
    }


    private final View.OnClickListener mListener = new View.OnClickListener() {
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.season:
                    getChildFragmentManager().beginTransaction().replace(R.id.frag_container, main2Season).commit();
                    break;
                case R.id.festival:
                    getChildFragmentManager().beginTransaction().replace(R.id.frag_container, main2Festival).commit();
                    break;
                case R.id.local:
                    getChildFragmentManager().beginTransaction().replace(R.id.frag_container, main2Local).commit();
                    break;
                case R.id.parking:
                    getChildFragmentManager().beginTransaction().replace(R.id.frag_container, main2Parking).commit();
                    break;
            }
        }
    };

And error message is:

Process: com.example.bermuda, PID: 14194
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
halfer
  • 19,824
  • 17
  • 99
  • 186
  • Does this answer your question? [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) – plplmax Aug 16 '22 at 21:22

2 Answers2

1

The problem here is clear, first you need to know what's a NullPointerException.

Is normal, because you are declaring this :

Main2Season main2Season;
Main2Festival main2Festival;
Main2Local main2Local;
Main2Parking main2Parking;

And then without initialise them you use :

getChildFragmentManager().beginTransaction().replace(R.id.frag_container, main2Season).commit();

So doesn't matter which button you press, they'll fail.

So you need to initialise them before using them.

For instance you could do :

getChildFragmentManager().beginTransaction().replace(R.id.frag_container, Main2Season()).commit();
Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
  • Thank you very much sir.. !! i really happy :) i use your code but android studio fix like this getChildFragmentManager().beginTransaction().replace(R.id.frag_container, new Main2Season()).commit(); and successful :) thanks – Stupid_Student Aug 12 '22 at 08:52
  • i have one more question sir. I heard that I used it without initializing it, but is my method correct if I use it after initializing it? – Stupid_Student Aug 12 '22 at 08:52
1

The problem is, you didn't instantiate your child fragments. Change your code to this:

private final View.OnClickListener mListener = new View.OnClickListener() {
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.season:
                    getChildFragmentManager().beginTransaction().replace(R.id.frag_container, Main2Season()).commit();
                    break;
                case R.id.festival:
                    getChildFragmentManager().beginTransaction().replace(R.id.frag_container, Main2Festival()).commit();
                    break;
                case R.id.local:
                    getChildFragmentManager().beginTransaction().replace(R.id.frag_container, Main2Local()).commit();
                    break;
                case R.id.parking:
                    getChildFragmentManager().beginTransaction().replace(R.id.frag_container, Main2Parking()).commit();
                    break;
            }
        }
    };
halfer
  • 19,824
  • 17
  • 99
  • 186