I'm assuming the Image
in sheep.graphics
package refers to this or something closely related. Examining the source, you need to initialize your Game
object before creating Image
objects with resource id.
ExceptionInInitializerError Caused by: NullPointerException when using .png files in drawable folder in Android Studio
Question
Can somebody make scense of this? The image is in the drawable folder but when I run the code I get a NullPointerException. Please help!
package com.battlesheep.game.models;
import com.battlesheep.R;
import sheep.game.Sprite;
import sheep.graphics.Image;
public class BackgroundTile extends Sprite {
private static Image tileImage = new Image(R.drawable.backgroundtile);
public BackgroundTile(){
super(tileImage);
}
@Override
public void update(float dt){
super.update(dt);
}
public void setSize(float size){
this.setScale(size,size);
}
}
Here is the code calling the model that makes all the problems
package com.battlesheep.game.view;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import com.battlesheep.game.models.BackgroundTile;
import com.battlesheep.game.models.GameBoardModel;
import com.battlesheep.game.models.GamePiece;
import sheep.game.Sprite;
import sheep.game.State;
public class GameBoardView extends State {
Sprite sheep;
Sprite tile;
// CONSTRUCTOR
public GameBoardView(){
tile = new BackgroundTile();
sheep = new GamePiece();
}
Here is the activity initializing the game:
package com.battlesheep.activities;
import android.os.Bundle;
import android.app.Activity;
import sheep.game.Game;
import com.battlesheep.game.view.GameBoardView;
import com.battlesheep.game.models.GameBoardModel;
public class GameActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GameBoardModel gameBoardModel = new GameBoardModel();
GameBoardView gameBoard = new GameBoardView();
gameBoard.setModel(gameBoardModel);
// create game
Game game = new Game(this, null);
game.pushState(gameBoard);
// view game
setContentView(game);
}
}
Here is the error message:
03-17 13:34:10.330 2408-2408/com.battlesheep E/Trace﹕ error opening trace file: No such file or directory (2)
03-17 13:34:10.770 2408-2408/com.battlesheep V/class com.battlesheep.activities.LoginActivity﹕ Sending request
03-17 13:34:10.950 2408-2408/com.battlesheep W/dalvikvm﹕ Exception Ljava/lang/NullPointerException; thrown while initializing Lcom/battlesheep/game/models/BackgroundTile;
03-17 13:34:10.970 2408-2408/com.battlesheep W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x4133b450)
03-17 13:34:11.040 2408-2408/com.battlesheep E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.ExceptionInInitializerError
at com.battlesheep.game.view.GameBoardView.<init>(GameBoardView.java:26)
at com.battlesheep.activities.GameActivity.onCreate(GameActivity.java:20)
at android.app.Activity.performCreate(Activity.java:5013)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2029)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2090)
at android.app.ActivityThread.access$600(ActivityThread.java:136)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4802)
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:813)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:580)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at sheep.graphics.Image.<init>(Image.java:44)
at com.battlesheep.game.models.BackgroundTile.<clinit (BackgroundTile.java:11)
at com.battlesheep.game.view.GameBoardView.<init>(GameBoardView.java:26)
at com.battlesheep.activities.GameActivity.onCreate(GameActivity.java:20)
at android.app.Activity.performCreate(Activity.java:5013)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2029)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2090)
at android.app.ActivityThread.access$600(ActivityThread.java:136)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201) at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4802)
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:813)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:580)
at dalvik.system.NativeStart.main(Native Method)
The Image class
// IntelliJ API Decompiler stub source generated from a class file // Implementation of methods is not available
package sheep.graphics;
public class Image extends sheep.graphics.SpriteView {
private android.graphics.drawable.Drawable drawable;
public Image(int i) { /* compiled code */ }
public Image(android.graphics.drawable.Drawable drawable) { /* compiled code */ }
public void draw(android.graphics.Canvas canvas, float x, float y) { /* compiled code */ }
public void draw(android.graphics.Canvas canvas, android.graphics.Matrix transformation) { /* compiled code */ }
public void update(float dt) { /* compiled code */ }
public float getWidth() { /* compiled code */ }
public float getHeight() { /* compiled code */ }
public sheep.math.BoundingBox getBoundingBox() { /* compiled code */ }
}
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow