So I keep getting a null pointer exception and i have no clue why. For some reason cam is null, but i initialize it the same way i do ScaledCam and it works fine. I think something is messed up from the the path it takes from the Main class to Play class, but i cant find it
this is the Main class were i initialize the cam
public class Main extends ApplicationAdapter {
public static final String Title = "Mead Walla";
public static final int Width = 360;
public static final int Height = 240;
public static final int Scale = 2;
private SpriteBatch sb;
private OrthographicCamera cam;
private OrthographicCamera hudCam;
private GameStateHandler gsh;
public static ContentHandler res;
public void create() {
//initialize handlers
res = new ContentHandler();
gsh = new GameStateHandler(this);
//load content
try {
res.loadTexture("player/testPlayer.png", "testPlayer");
}
catch(Exception e) {
System.out.println("Cannot find file: testPlayer");
Gdx.app.exit();
}
//initialize LibGDX elements
sb = new SpriteBatch();
cam = new OrthographicCamera();
cam.setToOrtho(false, Width, Height);
hudCam = new OrthographicCamera();
hudCam.setToOrtho(false, Width, Height);
Gdx.input.setInputProcessor(new InputHandler());
}
public void render() {
Gdx.graphics.setTitle(Title + " -- FPS: " + Gdx.graphics.getFramesPerSecond());
gsh.update(Gdx.graphics.getDeltaTime());
gsh.render();
InputHandler.update();
sb.setProjectionMatrix(hudCam.combined);
sb.begin();
sb.draw(res.getTexture("testPlayer"), 0, 0);
sb.end();
}
public SpriteBatch getSpriteBatch() {
return sb;
}
public OrthographicCamera getCam() {
return cam;
}
public OrthographicCamera getHudCam() {
return hudCam;
}
the state class should get the cam from main and set it to the cam in state
public abstract class State {
protected GameStateHandler gsh;
protected Main game;
protected SpriteBatch sb;
protected OrthographicCamera cam;
protected OrthographicCamera hudCam;
protected State(GameStateHandler gsh) {
this.gsh = gsh;
game = gsh.getGame();
sb = game.getSpriteBatch();
cam = game.getCam();
hudCam = game.getHudCam();
}
public abstract void handleInput();
public abstract void update(float dt);
public abstract void render();
public abstract void dispose();
}
this is the play class were i try to use the cam to render the TileMap but when i get to setView(cam) its null, if i use ScaledCam it runs its just not the right view
public class Play extends State {
//Box2D elements
private World world;
private BodyDef bdef;
private Body body;
private Body boxBody;
private FixtureDef fdef;
private PolygonShape shape;
private Box2DDebugRenderer b2dr;
private OrthographicCamera ScaledCam;
private TiledMap tileMap;
private OrthogonalTiledMapRenderer tmr;
//Box2D variables
private static float ppm = B2D.PPM;
private static float gravityX = 0;
private static float gravityY = -9.81f;
public Play(GameStateHandler gsh) {
super(gsh);
//initialize elements
world = new World(new Vector2(gravityX, gravityY), true);
bdef = new BodyDef();
fdef = new FixtureDef();
shape = new PolygonShape();
b2dr = new Box2DDebugRenderer();
ScaledCam = new OrthographicCamera();
ScaledCam.setToOrtho(false, Main.Width / ppm, Main.Height / ppm);
world.setContactListener(new ContactHandler());
//test platform body definition
bdef.position.set(160 / ppm, 120 / ppm);
bdef.type = BodyType.StaticBody;
body = world.createBody(bdef);
//test platform fixture definition
shape.setAsBox(50 / ppm, 5 / ppm);
fdef.shape = shape;
fdef.filter.categoryBits = B2D.Bit_One;
fdef.filter.maskBits = B2D.Bit_Two;
body.createFixture(fdef).setUserData("platform");
//test box body definition
bdef.position.set(160 / ppm, 200 / ppm);
bdef.type = BodyType.DynamicBody;
boxBody = world.createBody(bdef);
//test box fixture definition
shape.setAsBox(5 / ppm, 5 / ppm);
fdef.shape = shape;
fdef.filter.categoryBits = B2D.Bit_Two;
fdef.filter.maskBits = B2D.Bit_One;
boxBody.createFixture(fdef).setUserData("box");
//test box sensor
shape.setAsBox(2 / ppm, 2 / ppm, new Vector2(0, -5 / ppm), 0);
//set to same stuff between shape and create in player
fdef.isSensor = true;
boxBody.createFixture(fdef).setUserData("sensor");
//load tile map
try {
tileMap = new TmxMapLoader().load("map/test.tmx");
}
catch(Exception e) {
System.out.println("Cannot find file: map/test.tmx");
Gdx.app.exit();
}
tmr = new OrthogonalTiledMapRenderer(tileMap);
}
public void update(float dt) {
handleInput();
world.step(dt, 6, 2);
}
public void render() {
//clear screen
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
//draw tile map
tmr.setView(cam);
tmr.render();
//draw box2d world
b2dr.render(world, ScaledCam.combined);
}
public void handleInput() {}
public void dispose() {}
}
I can not find why its null. any help would be appreciated.