无故获取 NullPointerException
Getting NullPointerException for no reason
我无缘无故收到 NullPointerException。请不要将我重定向到另一个 post。我看过所有这些。我正在用 LibGDX.
制作游戏
这是错误
Exception in thread "LWJGL Application" java.lang.NullPointerException
at net.hasanbilal.pr.entities.Entity.getWeight(Entity.java:88)
at net.hasanbilal.pr.entities.Entity.update(Entity.java:25)
at net.hasanbilal.pr.entities.Porter.update(Porter.java:34)
at net.hasanbilal.pr.world.GMap.update(GMap.java:28)
at net.hasanbilal.pr.world.TiledGMap.update(TiledGMap.java:40)
at net.hasanbilal.pr.PrisonRevelations.render(PrisonRevelations.java:59)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:215)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.run(LwjglApplication.java:120)
我也会展示每个 class 错误点。
这是实体 Class。
package net.hasanbilal.pr.entities;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import net.hasanbilal.pr.world.GMap;
public abstract class Entity {
protected Vector2 pos;
protected EntityType t;
protected float velocityY = 0;
protected GMap m;
protected boolean grounded = false;
public void create (EntitySnapshot snapshot, EntityType type, GMap map) {
this.pos = new Vector2(snapshot.getX(),snapshot.getY());
this.t = t;
this.m = m;
}
public void update (float delta, float g) {
float newY = pos.y;
this.velocityY += g * delta * getWeight();
newY += this.velocityY * delta;
if (m.doesRectCollideWithMap(pos.x, newY, getWidth(), getHeight())) {
if (velocityY < 0) {
this.pos.y = (float) Math.floor(pos.y);
grounded = true;
}
this.velocityY = 0;
} else {
this.pos.y = newY;
grounded = false;
}
}
public abstract void render (SpriteBatch b);
protected void moveX(float amount) {
float newX = this.pos.x + amount;
if (!m.doesRectCollideWithMap(newX, pos.y, getWidth(), getHeight()))
this.pos.x = newX;
}
public EntitySnapshot getSaveSnapshot(){
return new EntitySnapshot(t.getId(), pos.x, pos.y);
}
public Vector2 getPos() {
return pos;
}
public float getX() {
return pos.x;
}
public float getY() {
return pos.y;
}
public EntityType getT() {
return t;
}
public float getVelocityY() {
return velocityY;
}
public boolean isGrounded() {
return grounded;
}
public int getWidth() {
return t.getWidth();
}
public int getHeight() {
return t.getHeight();
}
public float getWeight() {
return t.getWeight();
}
}
我是搬运工Class。
package net.hasanbilal.pr.entities;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import net.hasanbilal.pr.world.GMap;
public class Porter extends Entity {
private static final int SPEED = 80;
private static final int JUMP_VELOCITY = 5;
Texture img;
public void create (EntitySnapshot snapshot, EntityType type, GMap map) {
super.create(snapshot, type, map);
img = new Texture("porter.png");
}
@Override
public void render(SpriteBatch b) {
b.draw(img, pos.x, pos.y, getWidth(), getHeight());
}
public void update(float delta, float g) {
if (Gdx.input.isKeyPressed(Keys.SPACE) && grounded)
this.velocityY += JUMP_VELOCITY * getWeight();
else if (Gdx.input.isKeyPressed(Keys.SPACE) && !grounded && this.velocityY > 0)
this.velocityY += JUMP_VELOCITY * getWeight() * delta;
super.update(delta, g);
if (Gdx.input.isKeyPressed(Keys.LEFT))
moveX(-SPEED * delta);
if (Gdx.input.isKeyPressed(Keys.RIGHT))
moveX(SPEED * delta);
}
}
这是 GMap
package net.hasanbilal.pr.world;
import java.util.ArrayList;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import net.hasanbilal.pr.entities.Entity;
import net.hasanbilal.pr.entities.EntityLoader;
import net.hasanbilal.pr.entities.Porter;
public abstract class GMap {
protected ArrayList<Entity> entities;
public GMap() {
entities = new ArrayList<Entity>();
entities.addAll(EntityLoader.loadEntities("basic", this, entities));
}
public void render (OrthographicCamera c, SpriteBatch b) {
for (Entity entity : entities) {
entity.render(b);
}
}
public void update (float deltaTime) {
for (Entity entity : entities) {
entity.update(deltaTime, -9.8f);
}
}
public void dispose () {
EntityLoader.saveEntities("basic", entities);
}
/**
* Gets a tile by location
* @param layer
* @param x
* @param y
* @return
*/
public TileType getByLocation(int layer, float x, float y) {
return this.getByCoordinate(layer, (int) (x / TileType.TILE_SIZE), (int) (y / TileType.TILE_SIZE));
}
/**
* Gets Tile by coordinate
* @param layer
* @param col
* @param row
* @return
*/
public abstract TileType getByCoordinate(int layer, int col, int row);
public boolean doesRectCollideWithMap(float x, float y, int width, int height) {
if (x<0 || y < 0 || x + width > getPixelWidth() || y + height > getPixelHeight())
return true;
for (int row = (int) (y / TileType.TILE_SIZE); row < Math.ceil((y + height) / TileType.TILE_SIZE); row++) {
for (int col = (int) (x / TileType.TILE_SIZE); col < Math.ceil((x + width) / TileType.TILE_SIZE); col++) {
for (int layer = 0; layer < getLayers(); layer++) {
TileType type = getByCoordinate(layer, col, row);
if (type != null && type.isCollidable())
return true;
}
}
}
return false;
}
public abstract int getWidth();
public abstract int getHeight();
public abstract int getLayers();
public int getPixelWidth() {
return this.getWidth() * TileType.TILE_SIZE;
}
public int getPixelHeight() {
return this.getHeight() * TileType.TILE_SIZE;
}
}
这是 TiledGMap
的 class
package net.hasanbilal.pr.world;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapTile;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import net.hasanbilal.pr.entities.Entity;
import net.hasanbilal.pr.entities.Porter;
public class TiledGMap extends GMap {
TiledMap lvl1;
OrthogonalTiledMapRenderer otmr;
public TiledGMap() {
lvl1 = new TmxMapLoader().load("level1.tmx");
otmr = new OrthogonalTiledMapRenderer(lvl1);
}
@Override
public void render(OrthographicCamera c, SpriteBatch b) {
otmr.setView(c);
otmr.render();
b.setProjectionMatrix(c.combined);
b.begin();
super.render(c, b);
b.end();
}
@Override
public void update(float deltaTime) {
super.update(deltaTime);
}
@Override
public void dispose() {
lvl1.dispose();
}
@Override
public TileType getByCoordinate(int layer, int col, int row) {
Cell cell = ((TiledMapTileLayer) lvl1.getLayers().get(layer)).getCell(col, row);
if (cell !=null) {
TiledMapTile t = cell.getTile();
if (t != null) {
int id = t.getId();
return TileType.getTileTypeById(id);
}
}
return null;
}
@Override
public int getWidth() {
return ((TiledMapTileLayer) lvl1.getLayers().get(0)).getWidth();
}
@Override
public int getHeight() {
return ((TiledMapTileLayer) lvl1.getLayers().get(0)).getHeight();
}
@Override
public int getLayers() {
return lvl1.getLayers().getCount();
}
}
这是监狱启示录class
package net.hasanbilal.pr;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector3;
import net.hasanbilal.pr.world.GMap;
import net.hasanbilal.pr.world.TileType;
import net.hasanbilal.pr.world.TiledGMap;
public class PrisonRevelations extends ApplicationAdapter {
OrthographicCamera c;
SpriteBatch b;
GMap gm;
@Override
public void create () {
b = new SpriteBatch();
c = new OrthographicCamera();
c.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
c.update();
gm = new TiledGMap();
}
@Override
public void render () {
Gdx.gl.glClearColor(255, 255, 255, 1);
Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if (Gdx.input.isTouched()) {
c.translate(-Gdx.input.getDeltaX(), Gdx.input.getDeltaY());
c.update();
}
if (Gdx.input.justTouched()) {
Vector3 pos = c.unproject(new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0));
TileType t = gm.getByLocation(1, pos.x, pos.y);
if (t != null) {
System.out.println("You clicked on tile with id" + t.getId() + " " + t.getName()+ " " + t.isCollidable() + " " + t.getDamage());
}
}
c.update();
gm.update(Gdx.graphics.getDeltaTime());
gm.render(c, b);
}
@Override
public void dispose () {
b.dispose();
gm.dispose();
}
}
请帮忙。我要死了。这很快就要到期了。
您创建的参数名称似乎有误,请更改为:
public void create (EntitySnapshot snapshot, EntityType type, GMap map) {
this.pos = new Vector2(snapshot.getX(),snapshot.getY());
this.t = type;
this.m = map;
}
我无缘无故收到 NullPointerException。请不要将我重定向到另一个 post。我看过所有这些。我正在用 LibGDX.
制作游戏这是错误
Exception in thread "LWJGL Application" java.lang.NullPointerException
at net.hasanbilal.pr.entities.Entity.getWeight(Entity.java:88)
at net.hasanbilal.pr.entities.Entity.update(Entity.java:25)
at net.hasanbilal.pr.entities.Porter.update(Porter.java:34)
at net.hasanbilal.pr.world.GMap.update(GMap.java:28)
at net.hasanbilal.pr.world.TiledGMap.update(TiledGMap.java:40)
at net.hasanbilal.pr.PrisonRevelations.render(PrisonRevelations.java:59)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:215)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.run(LwjglApplication.java:120)
我也会展示每个 class 错误点。
这是实体 Class。
package net.hasanbilal.pr.entities;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import net.hasanbilal.pr.world.GMap;
public abstract class Entity {
protected Vector2 pos;
protected EntityType t;
protected float velocityY = 0;
protected GMap m;
protected boolean grounded = false;
public void create (EntitySnapshot snapshot, EntityType type, GMap map) {
this.pos = new Vector2(snapshot.getX(),snapshot.getY());
this.t = t;
this.m = m;
}
public void update (float delta, float g) {
float newY = pos.y;
this.velocityY += g * delta * getWeight();
newY += this.velocityY * delta;
if (m.doesRectCollideWithMap(pos.x, newY, getWidth(), getHeight())) {
if (velocityY < 0) {
this.pos.y = (float) Math.floor(pos.y);
grounded = true;
}
this.velocityY = 0;
} else {
this.pos.y = newY;
grounded = false;
}
}
public abstract void render (SpriteBatch b);
protected void moveX(float amount) {
float newX = this.pos.x + amount;
if (!m.doesRectCollideWithMap(newX, pos.y, getWidth(), getHeight()))
this.pos.x = newX;
}
public EntitySnapshot getSaveSnapshot(){
return new EntitySnapshot(t.getId(), pos.x, pos.y);
}
public Vector2 getPos() {
return pos;
}
public float getX() {
return pos.x;
}
public float getY() {
return pos.y;
}
public EntityType getT() {
return t;
}
public float getVelocityY() {
return velocityY;
}
public boolean isGrounded() {
return grounded;
}
public int getWidth() {
return t.getWidth();
}
public int getHeight() {
return t.getHeight();
}
public float getWeight() {
return t.getWeight();
}
}
我是搬运工Class。
package net.hasanbilal.pr.entities;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import net.hasanbilal.pr.world.GMap;
public class Porter extends Entity {
private static final int SPEED = 80;
private static final int JUMP_VELOCITY = 5;
Texture img;
public void create (EntitySnapshot snapshot, EntityType type, GMap map) {
super.create(snapshot, type, map);
img = new Texture("porter.png");
}
@Override
public void render(SpriteBatch b) {
b.draw(img, pos.x, pos.y, getWidth(), getHeight());
}
public void update(float delta, float g) {
if (Gdx.input.isKeyPressed(Keys.SPACE) && grounded)
this.velocityY += JUMP_VELOCITY * getWeight();
else if (Gdx.input.isKeyPressed(Keys.SPACE) && !grounded && this.velocityY > 0)
this.velocityY += JUMP_VELOCITY * getWeight() * delta;
super.update(delta, g);
if (Gdx.input.isKeyPressed(Keys.LEFT))
moveX(-SPEED * delta);
if (Gdx.input.isKeyPressed(Keys.RIGHT))
moveX(SPEED * delta);
}
}
这是 GMap
package net.hasanbilal.pr.world;
import java.util.ArrayList;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import net.hasanbilal.pr.entities.Entity;
import net.hasanbilal.pr.entities.EntityLoader;
import net.hasanbilal.pr.entities.Porter;
public abstract class GMap {
protected ArrayList<Entity> entities;
public GMap() {
entities = new ArrayList<Entity>();
entities.addAll(EntityLoader.loadEntities("basic", this, entities));
}
public void render (OrthographicCamera c, SpriteBatch b) {
for (Entity entity : entities) {
entity.render(b);
}
}
public void update (float deltaTime) {
for (Entity entity : entities) {
entity.update(deltaTime, -9.8f);
}
}
public void dispose () {
EntityLoader.saveEntities("basic", entities);
}
/**
* Gets a tile by location
* @param layer
* @param x
* @param y
* @return
*/
public TileType getByLocation(int layer, float x, float y) {
return this.getByCoordinate(layer, (int) (x / TileType.TILE_SIZE), (int) (y / TileType.TILE_SIZE));
}
/**
* Gets Tile by coordinate
* @param layer
* @param col
* @param row
* @return
*/
public abstract TileType getByCoordinate(int layer, int col, int row);
public boolean doesRectCollideWithMap(float x, float y, int width, int height) {
if (x<0 || y < 0 || x + width > getPixelWidth() || y + height > getPixelHeight())
return true;
for (int row = (int) (y / TileType.TILE_SIZE); row < Math.ceil((y + height) / TileType.TILE_SIZE); row++) {
for (int col = (int) (x / TileType.TILE_SIZE); col < Math.ceil((x + width) / TileType.TILE_SIZE); col++) {
for (int layer = 0; layer < getLayers(); layer++) {
TileType type = getByCoordinate(layer, col, row);
if (type != null && type.isCollidable())
return true;
}
}
}
return false;
}
public abstract int getWidth();
public abstract int getHeight();
public abstract int getLayers();
public int getPixelWidth() {
return this.getWidth() * TileType.TILE_SIZE;
}
public int getPixelHeight() {
return this.getHeight() * TileType.TILE_SIZE;
}
}
这是 TiledGMap
的 classpackage net.hasanbilal.pr.world;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapTile;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import net.hasanbilal.pr.entities.Entity;
import net.hasanbilal.pr.entities.Porter;
public class TiledGMap extends GMap {
TiledMap lvl1;
OrthogonalTiledMapRenderer otmr;
public TiledGMap() {
lvl1 = new TmxMapLoader().load("level1.tmx");
otmr = new OrthogonalTiledMapRenderer(lvl1);
}
@Override
public void render(OrthographicCamera c, SpriteBatch b) {
otmr.setView(c);
otmr.render();
b.setProjectionMatrix(c.combined);
b.begin();
super.render(c, b);
b.end();
}
@Override
public void update(float deltaTime) {
super.update(deltaTime);
}
@Override
public void dispose() {
lvl1.dispose();
}
@Override
public TileType getByCoordinate(int layer, int col, int row) {
Cell cell = ((TiledMapTileLayer) lvl1.getLayers().get(layer)).getCell(col, row);
if (cell !=null) {
TiledMapTile t = cell.getTile();
if (t != null) {
int id = t.getId();
return TileType.getTileTypeById(id);
}
}
return null;
}
@Override
public int getWidth() {
return ((TiledMapTileLayer) lvl1.getLayers().get(0)).getWidth();
}
@Override
public int getHeight() {
return ((TiledMapTileLayer) lvl1.getLayers().get(0)).getHeight();
}
@Override
public int getLayers() {
return lvl1.getLayers().getCount();
}
}
这是监狱启示录class
package net.hasanbilal.pr;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector3;
import net.hasanbilal.pr.world.GMap;
import net.hasanbilal.pr.world.TileType;
import net.hasanbilal.pr.world.TiledGMap;
public class PrisonRevelations extends ApplicationAdapter {
OrthographicCamera c;
SpriteBatch b;
GMap gm;
@Override
public void create () {
b = new SpriteBatch();
c = new OrthographicCamera();
c.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
c.update();
gm = new TiledGMap();
}
@Override
public void render () {
Gdx.gl.glClearColor(255, 255, 255, 1);
Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if (Gdx.input.isTouched()) {
c.translate(-Gdx.input.getDeltaX(), Gdx.input.getDeltaY());
c.update();
}
if (Gdx.input.justTouched()) {
Vector3 pos = c.unproject(new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0));
TileType t = gm.getByLocation(1, pos.x, pos.y);
if (t != null) {
System.out.println("You clicked on tile with id" + t.getId() + " " + t.getName()+ " " + t.isCollidable() + " " + t.getDamage());
}
}
c.update();
gm.update(Gdx.graphics.getDeltaTime());
gm.render(c, b);
}
@Override
public void dispose () {
b.dispose();
gm.dispose();
}
}
请帮忙。我要死了。这很快就要到期了。
您创建的参数名称似乎有误,请更改为:
public void create (EntitySnapshot snapshot, EntityType type, GMap map) {
this.pos = new Vector2(snapshot.getX(),snapshot.getY());
this.t = type;
this.m = map;
}