How do I use findViewById() in onCreate without it returning null?
<p>I don't know how to use <code>findViewById(R.id.nav_timetable);</code> without it returning null and creating an NPE. What I'm trying to do is make the TimetableFragment get loaded by default and then people can use the drawer to go to other fragments.</p>

<pre><code>@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    setupDrawer(toolbar);

    drawer.openDrawer(GravityCompat.START); // Opened this on first run so they know what's in it.

    // TODO Change this later to a home page and remove goToTimetablePage();
    // goToTimetablePage();
    goToFragmentFromNavMenuItem((MenuItem) findViewById(R.id.nav_timetable));
}

@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
    // Handle navigation view item clicks here.

    goToFragmentFromNavMenuItem(menuItem);
    return true;
}

private void goToFragmentFromNavMenuItem(MenuItem menuItem) {
    // Create a new fragment and specify the fragment to show based on nav item clicked
    Fragment fragment = null;
    Class fragmentClass;

    switch (menuItem.getItemId()) {
        case R.id.nav_slideshow: // TODO fragmentClass = SlideshowFragment.class; break;
        case R.id.nav_share: // TODO fragmentClass = ShareFragment.class; break;
        case R.id.nav_timetable:
            fragmentClass = TimetableFragment.class;
            // goToTimetablePage();
            break;
        default:
            fragmentClass = TimetableFragment.class;
            // TODO Change this later to a home page:
            // TODO fragmentClass = HomeFragment.class; Remove "goToTimetablePage();"
    }


    //noinspection TryWithIdenticalCatches
    try {
        fragment = (Fragment) fragmentClass.newInstance();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

    FragmentManager fragmentManager = getSupportFragmentManager();

    fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();

    menuItem.setChecked(true);
    setTitle(menuItem.getTitle());
    drawer.closeDrawer(GravityCompat.START);
}
</code></pre>

<p>Error message:</p>

<pre><code>java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gmail.gogobebe2.thedayahead/com.gmail.gogobebe2.thedayahead.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'int android.view.MenuItem.getItemId()' on a null object reference
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
  at android.app.ActivityThread.-wrap11(ActivityThread.java)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:148)
  at android.app.ActivityThread.main(ActivityThread.java:5417)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int android.view.MenuItem.getItemId()' on a null object reference
  at com.gmail.gogobebe2.thedayahead.MainActivity.goToNavFragment(MainActivity.java:123)
  at com.gmail.gogobebe2.thedayahead.MainActivity.onCreate(MainActivity.java:59)
  at android.app.Activity.performCreate(Activity.java:6237)
  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
  at android.app.ActivityThread.-wrap11(ActivityThread.java) 
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
  at android.os.Handler.dispatchMessage(Handler.java:102) 
  at android.os.Looper.loop(Looper.java:148) 
  at android.app.ActivityThread.main(ActivityThread.java:5417) 
  at java.lang.reflect.Method.invoke(Native Method) 
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
</code></pre>

<p>I tried using the <code>onCreateView</code> method but it just made stackoverflow messages for some reason.</p>

<p>EDIT: Here's all the code to clear up any confusion: <a href="https://github.com/gogobebe2/TheDayAhead" rel="nofollow">https://github.com/gogobebe2/TheDayAhead</a></p>

<p>EDIT2: Make sure you don't worry about the <code>goToTimetablePage()</code> method. It's unused at the moment and I plan on moving it to the <code>TimetableFragment</code> fragment.</p>

<p>EDIT3: I changed the method name to <code>goToFragmentFromNavMenuItem()</code> so that it better describes it's actions to clear things up. I also want to let you guys know that the <code>onNavigationItemSelected(MenuItem menuItem)</code> method works perfectly with the <code>goToFragmentFromNavMenuItem()</code> method, it's just that it doesn't work when used inside <code>onCreate(Bundle savedInstanceState)</code> because of the <code>findViewById(R.id.nav_timetable)</code> returning <code>null</code> all the time.</p>

<p>Also, this is my first Android app I've ever made and I'm kinda learning as I make it. I didn't even know what fragments were last night so I'm really new to Android, please don't assume any knowledge.</p>