Java.lang.nullpointer Exception (font)

So lets say i have a activity like that :

public class display_score extends Activity implements OnClickListener {

    Button back;
    TextView txt1;
    TextView txt2;
    TextView txt3;
    TextView txt4;
    TextView txt5;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.display_highscore);


       back = (Button) findViewById(R.id.button1);
        back.setOnClickListener(this);



        txt1 = (TextView) findViewById(R.id.textView1);
        txt2 = (TextView) findViewById(R.id.textView2);
        txt3 = (TextView) findViewById(R.id.textView3);
        txt4 = (TextView) findViewById(R.id.textView4);
        txt5 = (TextView) findViewById(R.id.textView5);



        txt1.setText("1. "+score.scores.getString("NAME","")+" "+String.valueOf(score.scores.getInt("SCORE",0)));
        txt2.setText("2. "+score.scores.getString("NAME2","")+" "+String.valueOf(score.scores.getInt("SCORE2",0)));
        txt3.setText("3. "+score.scores.getString("NAME3","")+" "+String.valueOf(score.scores.getInt("SCORE3",0)));
        txt4.setText("4. "+score.scores.getString("NAME4","")+" "+String.valueOf(score.scores.getInt("SCORE4",0)));
        txt5.setText("5. "+score.scores.getString("NAME5","")+" "+String.valueOf(score.scores.getInt("SCORE5",0)));


        txt1.setTypeface(Typeface.createFromAsset(getAssets(),"game_over.ttf"));
        txt2.setTypeface(Typeface.createFromAsset(getAssets(),"game_over.ttf"));
        txt3.setTypeface(Typeface.createFromAsset(getAssets(),"game_over.ttf"));
        txt4.setTypeface(Typeface.createFromAsset(getAssets(),"game_over.ttf"));
        txt5.setTypeface(Typeface.createFromAsset(getAssets(),"game_over.ttf"));

        txt1.setTextColor(Color.BLACK);
        txt2.setTextColor(Color.BLACK);
        txt3.setTextColor(Color.BLACK);
        txt4.setTextColor(Color.BLACK);
        txt5.setTextColor(Color.BLACK);


    }

    @Override
    public void onClick(View v) {
        finish();

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }




}

and a layout like that :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom"
    android:orientation="vertical" 
    android:background="@drawable/background">


    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="1"
        android:textAppearance="?android:attr/textAppearanceLarge" />


    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="2"
        android:textAppearance="?android:attr/textAppearanceLarge" />


    <TextView
        android:id="@+id/textView3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="3"
        android:textAppearance="?android:attr/textAppearanceLarge" />


    <TextView
        android:id="@+id/textView4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="4"
        android:textAppearance="?android:attr/textAppearanceLarge" />



    <TextView
        android:id="@+id/textView5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="5"
        android:textAppearance="?android:attr/textAppearanceLarge" />


    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Back" />

</LinearLayout>

Then I get this exception :

04-24 22:02:43.101: E/AndroidRuntime(10956): FATAL EXCEPTION: main
04-24 22:02:43.101: E/AndroidRuntime(10956): java.lang.RuntimeException: Unable to start activity ComponentInfo{run.alexander.fuchs/run.alexander.fuchs.display_score}: java.lang.NullPointerException
04-24 22:02:43.101: E/AndroidRuntime(10956):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
04-24 22:02:43.101: E/AndroidRuntime(10956):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
04-24 22:02:43.101: E/AndroidRuntime(10956):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
04-24 22:02:43.101: E/AndroidRuntime(10956):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
04-24 22:02:43.101: E/AndroidRuntime(10956):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 22:02:43.101: E/AndroidRuntime(10956):    at android.os.Looper.loop(Looper.java:137)
04-24 22:02:43.101: E/AndroidRuntime(10956):    at android.app.ActivityThread.main(ActivityThread.java:4424)
04-24 22:02:43.101: E/AndroidRuntime(10956):    at java.lang.reflect.Method.invokeNative(Native Method)
04-24 22:02:43.101: E/AndroidRuntime(10956):    at java.lang.reflect.Method.invoke(Method.java:511)
04-24 22:02:43.101: E/AndroidRuntime(10956):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
04-24 22:02:43.101: E/AndroidRuntime(10956):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
04-24 22:02:43.101: E/AndroidRuntime(10956):    at dalvik.system.NativeStart.main(Native Method)
04-24 22:02:43.101: E/AndroidRuntime(10956): Caused by: java.lang.NullPointerException
04-24 22:02:43.101: E/AndroidRuntime(10956):    at run.alexander.fuchs.display_score.onCreate(display_score.java:48)
04-24 22:02:43.101: E/AndroidRuntime(10956):    at android.app.Activity.performCreate(Activity.java:4465)
04-24 22:02:43.101: E/AndroidRuntime(10956):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-24 22:02:43.101: E/AndroidRuntime(10956):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
04-24 22:02:43.101: E/AndroidRuntime(10956):    ... 11 more

That's very strange because every laout is there the font is there and so on :(

Even the shared prefferances should work !!

Answers


Your exception stack trace points to display_score.java:48, but without the package and import statements included in your code, I can't tell which line that is. If it's this line

txt1.setTypeface(Typeface.createFromAsset(getAssets(),"game_over.ttf"));

then perhaps this SO question might help solve it: set font at runtime, Textview. You might need to put your font files into the assets/fonts directory if they're not already there.


I found out what the problem was

The SharedPreferences weren't loaded correctly so it failed loading


Need Your Help

Why does this get-or-create function never seem to insert?

oracle plsql

I'm writing a function for batch-importing data into our org-chart. It seems to work fine for retrieving entries that already exists, but when an entry does not already exist, and it's supposed to ...

I am trying to display detail list of may json response in UITableViewcell

ios objective-c json uitableview

Hi I am trying to display detail information of json data in second table view but their is some mistake in my logic so please help me my json response is in image and I am trying to to dispaly cha...