Q:

Bottom nav view fragment NullPointerException

In my one of my fragments I have a toggle button in the xml:
<FrameLayout 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="com.stefan.findage.piano">

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

    <ToggleButton
        android:id="@+id/metronome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ToggleButton" />

</FrameLayout>

And
toggleButton = (ToggleButton) getView().findViewById(R.id.metronome);

But when trying to open the fragment it crashes and the logcat shows that it was a 

java.lang.NullPointerException:Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference

Here's the Main Activity:
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BottomNavigationView bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottomView);
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId())
                {
                    case R.id.moosic:
                        rhythm fragmentone = new rhythm();
                        android.support.v4.app.FragmentTransaction fragmentTransaction3 = getSupportFragmentManager().beginTransaction();
                        fragmentTransaction3.replace(R.id.frame, fragmentone, "rhythm");
                        fragmentTransaction3.commit();
                        return true;
                    case R.id.call:
                        intervals fragmenttwo = new intervals();
                        android.support.v4.app.FragmentTransaction fragmentTransaction2 = getSupportFragmentManager().beginTransaction();
                        fragmentTransaction2.replace(R.id.frame, fragmenttwo, "intervals");
                        fragmentTransaction2.commit();
                        return true;
                    case R.id.thinga:
                        piano fragmentthree = new piano();
                        android.support.v4.app.FragmentTransaction fragmentTransaction1 = getSupportFragmentManager().beginTransaction();
                        fragmentTransaction1.replace(R.id.frame, fragmentthree, "piano");
                        fragmentTransaction1.commit();
                        return true;

                }
                return false;
            }
        });

    }

}

A:

getView() might be returning null. Use the below code to reference the toggle button
View view = inflater.inflate(R.layout.fragmentlayout, container, false);
toggleButton = (ToggleButton) view.findViewById(R.id.metronome);

