Attempt to invoke virtual method TabLayout.setupWithViewPager(ViewPager) on a null object reference
<p>M a newbie in android, please lend me some help.</p>

<p>Here is my code.</p>

<pre><code>import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import com.creativehomos.fragments.books;
import com.creativehomos.fragments.interaction;
import com.creativehomos.fragments.portfolio;
import com.creativehomos.fragments.team;

import java.util.ArrayList;
import java.util.List;

public class after_login extends AppCompatActivity {

ViewPager viewPager;
TabLayout tabLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_after_login);

    viewPager = (ViewPager) findViewById(R.id.viewPager);
    tabLayout = (TabLayout) findViewById(R.id.tabs);
    initViewPager();
    tabLayout.setupWithViewPager(viewPager);

}

private void initViewPager() {

    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

    adapter.addFrag(new books(), "Books");
    adapter.addFrag(new portfolio(), "Portfolio");
    adapter.addFrag(new interaction(), "Interaction");
    adapter.addFrag(new team(), "Team");
    viewPager.setAdapter(adapter);

}

public class ViewPagerAdapter extends FragmentPagerAdapter {

    private final List&lt;Fragment&gt; mFragmentList = new ArrayList&lt;&gt;();
    private final List&lt;String&gt; mFragmentTitleList = new ArrayList&lt;&gt;();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFrag(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

}
</code></pre>

<p>everytime I try to run the activity it always gives an exception </p>

<pre><code>java.lang.RuntimeException: Unable to start activity ComponentInfo{com.creativehomos.projectb/com.creativehomos.projectb.after_login}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.TabLayout.setupWithViewPager(android.support.v4.view.ViewPager)' 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 virtual method 'void android.support.design.widget.TabLayout.setupWithViewPager(android.support.v4.view.ViewPager)' on a null object reference
                                                                          at com.creativehomos.projectb.after_login.onCreate(after_login.java:32)
                                                                          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 searched for the problem, and was unable to find the solution, but yes the problem is that the cause of the problem is tabLayout and not the viewPager.</p>

<p>Thanks in advance. </p>