Q:

Getting exception error when setting custom font in fragments android

Here is the code, Its basically a simple menu driven app, I am trying to use custom font inside a fragment and that is causing a problem.
I have successfully used this font in an activity, the menu activity that launches another activity inside which this fragment is inflated.
I am not sure if this is what that is causing the problem so help me out please.  
package com.cbs.CoronaTheCarnival;

    import android.app.Fragment;
    import android.graphics.Typeface;
    import android.os.Bundle;
    import android.view.View;
    import android.view.LayoutInflater;
    import android.view.ViewGroup;
    import android.widget.TextView;

    /**
     * Created by aditya on 2/4/14.
     */
    public class AboutFragment extends Fragment{

        View rootView;
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
            View rootView = inflater.inflate(R.layout.fragment_about, container, false);
            initFonts();
            return rootView;

        }

         public void initFonts(){

            TextView tv_title = (TextView) rootView.findViewById(R.id.tv_about_title);
            Typeface changeFont = Typeface.createFromAsset(getActivity().getAssets(), "TRACK.OTF");
            tv_title.setTypeface(changeFont);
            TextView tv_theme = (TextView) rootView.findViewById(R.id.tv_about_theme);
            tv_theme.setTypeface(changeFont);

        }

    }

Here is the logcat, it says that exception error, I have no idea why its not working, Please help me out, its killing me and its 4:21 in the morning.
02-06 04:19:41.534    3591-3591/com.cbs.CoronaTheCarnival E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cbs.CoronaTheCarnival/com.cbs.CoronaTheCarnival.HomeActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5041)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.cbs.CoronaTheCarnival.AboutFragment.initFonts(AboutFragment.java:27)
            at com.cbs.CoronaTheCarnival.AboutFragment.onCreateView(AboutFragment.java:20)
            at android.app.Fragment.performCreateView(Fragment.java:1695)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
            at android.app.BackStackRecord.run(BackStackRecord.java:682)
            at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
            at android.app.Activity.performStart(Activity.java:5113)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5041)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)

A:

In your initFonts method, you do this :
TextView tv_title = (TextView) rootView.findViewById(R.id.tv_about_title);

Here, rootView is null because it was never correctly initialized. You seem to confusing your 2 rootView variables :
View rootView; // this is one variable
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View rootView = inflater.inflate(R.layout.fragment_about, container, false);
        // this is not the same rootView !
        // here, rootView != this.rootView
        initFonts();
        return rootView;
    }

public void initFonts(){
    TextView tv_title = (TextView) rootView.findViewById(R.id.tv_about_title);
    // this is like using this.rootView.findViewById
    // ...
}

the rootView inside your onCreateView method is not visible by the initFonts method, so it will use the View you declared on top of onCreateView, the first one, which is never initialized correctly. To solve this, you can move the code of initFonts back in onCreateView, pass the view as an argument of your initFonts method, or initialize your variable in onCreateView like this :
this.rootView = rootView;

