0

Ok, I've been following this tutorial and implementing my game. But, I'm getting a null pointer exception on the font object..I've loaded the font.ttf file in the assets/font folder and here's the code. Kindly help. Thanks...

In my gameScene class :

      private void createHUD()
 {
      gameHUD = new HUD();
      scoreText = new Text(20, 420, resourcesManager.font, "Score: 0123456789", new TextOptions(HorizontalAlign.LEFT), vbom);
      scoreText = new Text(20, 420,mFont, "Score: 0123456789", new TextOptions(HorizontalAlign.LEFT), vbom);
      scoreText.setSkewCenter(0, 0);    
      scoreText.setText("Score: 0");
      gameHUD.attachChild(scoreText);
      camera.setHUD(gameHUD);
}

In my ResourcesManager class:

     private void loadMenuFonts()
    {
     FontFactory.setAssetBasePath("font/");
         final ITexture mainFontTexture = new BitmapTextureAtlas(activity.getTextureManager(), 256, 256, TextureOptions.BILINEAR);

     font = FontFactory.createStrokeFromAsset(activity.getFontManager(), mainFontTexture, activity.getAssets(), "font.ttf", 50, true, Color.WHITE, 2, Color.BLACK);
     font.load();
     }

And the logcat:

     05-12 15:57:37.478: E/AndroidRuntime(20431): java.lang.NullPointerException:    Attempt to invoke interface method 'org.andengine.opengl.font.Letter   org.andengine.opengl.font.IFont.getLetter(char)' on a null object reference
     05-12 15:57:37.478: E/AndroidRuntime(20431):   at org.andengine.opengl.font.FontUtils.measureText(FontUtils.java:95)
     05-12 15:57:37.478: E/AndroidRuntime(20431):   at org.andengine.opengl.font.FontUtils.measureText(FontUtils.java:70)
     05-12 15:57:37.478: E/AndroidRuntime(20431):   at org.andengine.opengl.font.FontUtils.measureText(FontUtils.java:49)
     05-12 15:57:37.478: E/AndroidRuntime(20431):   at org.andengine.entity.text.Text.setText(Text.java:201)
     05-12 15:57:37.478: E/AndroidRuntime(20431):   at org.andengine.entity.text.Text.<init>(Text.java:159)
     05-12 15:57:37.478: E/AndroidRuntime(20431):   at org.andengine.entity.text.Text.<init>(Text.java:146)
     05-12 15:57:37.478: E/AndroidRuntime(20431):   at org.andengine.entity.text.Text.<init>(Text.java:138)
     05-12 15:57:37.478: E/AndroidRuntime(20431):   at org.andengine.entity.text.Text.<init>(Text.java:110)
     05-12 15:57:37.478: E/AndroidRuntime(20431):   at org.andengine.entity.text.Text.<init>(Text.java:102)
     05-12 15:57:37.478: E/AndroidRuntime(20431):   at com.packagename.scene.GameScene.createHUD(GameScene.java:55)
     05-12 15:57:37.478: E/AndroidRuntime(20431):   at com.packagename.scene.GameScene.createScene(GameScene.java:39)
     05-12 15:57:37.478: E/AndroidRuntime(20431):   at com.packagename.base.BaseScene.<init>(BaseScene.java:36)
     05-12 15:57:37.478: E/AndroidRuntime(20431):   at com.packagename.scene.GameScene.<init>(GameScene.java:26)
     05-12 15:57:37.478: E/AndroidRuntime(20431):   at com.packagename.manager.SceneManager$1.onTimePassed(SceneManager.java:112)
     05-12 15:57:37.478: E/AndroidRuntime(20431):   at org.andengine.engine.handler.timer.TimerHandler.onUpdate(TimerHandler.java:98)
     05-12 15:57:37.478: E/AndroidRuntime(20431):   at org.andengine.engine.handler.UpdateHandlerList.onUpdate(UpdateHandlerList.java:47)
user2125722
  • 1,289
  • 3
  • 18
  • 29

2 Answers2

0

Try

BitmapTextureAtlas=new BitmapTextureAtlas(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);

Instead of

final ITexture mainFontTexture = new BitmapTextureAtlas(activity.getTextureManager(), 256, 256, TextureOptions.BILINEAR);
JCodes13
  • 488
  • 5
  • 17
0

Ok, finally got the error ! The mistake was when the text was being created in the GameScene class, the font had to be accessed from the ResourceManager class. But the font wasn't getting loaded as the function loadMenuFonts() wasn't being called. So moving that code snippet into the the loadGameFonts() function ensured the loadGameFonts() function was called when the function loadGameResources() function was called..

     public void loadGameResources()
{
    loadGameGraphics();
    loadGameFonts();
    loadGameAudio();
}

And the loadGameFonts() function :

     private void loadGameFonts()
{
    FontFactory.setAssetBasePath("font/");
    final ITexture mainFontTexture = new   BitmapTextureAtlas(activity.getTextureManager(), 256, 256, TextureOptions.BILINEAR);
    font = FontFactory.createStrokeFromAsset(activity.getFontManager(), mainFontTexture, activity.getAssets(), "font.ttf", 50, true, Color.WHITE, 2, Color.BLACK);
    //mainFontTexture.load();
    font.load();
}
user2125722
  • 1,289
  • 3
  • 18
  • 29