-1

I have a score on my canvas that changes when the user collects a star. I want to change the font on the score text to my custom font but the game crashes when I run it. The error I get is " java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference"

My question is does anybody know what I'm missing? I'm not too sure where I'm going wrong. Any help at all would be appreciated.

My code:

Paint scorePaint;

  Typeface typeFace = Typeface.createFromAsset(getAssets(), "fonts/PressStart2P-Regular.ttf");

    scorePaint.setTypeface(typeFace);
    scorePaint = new Paint();
    scorePaint.setColor(Color.WHITE);
    scorePaint.setTextSize(70);
    scorePaint.setTextAlign(Paint.Align.LEFT);



canvas.drawText("" + score, 0, 110, scorePaint);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Eoin
  • 252
  • 2
  • 15
  • It's very difficult to debug a crash without a stack trace. See [Unfortunately MyApp has stopped. How can I solve this?](/q/23353173) for Android-specific advice, and [What is a stack trace, and how can I use it to debug my application errors?](/q/3988788) for advice on what to do once you have the stack trace. If you still need help, edit your question to include the **complete stack trace**, as well as **which line of your code** the stack trace points to. – Ryan M Sep 02 '23 at 09:46
  • Here, you're _probably_ calling this at the wrong time, and `getAssets()` is throwing, but it's impossible to say without a stack trace or the rest of the code. – Ryan M Sep 02 '23 at 09:46

1 Answers1

-1

"fonts/PressStart2P-Regular.ttf" Checkout the file name ! may be its not the correct name use small letter