我想使用下面的代码在屏幕上显示文本但是每当我运行这部分代码时,它都会出现错误,NullPointerException。
batch.begin();
batch.draw(paper, pos.x, pos.y);
batch.draw(trash, position.x, position.y);
font.setColor(1.0f, 1.0f, 1.0f,1.0f);
font.draw(batch, str, 25, 160);
batch.end();
更准确地说,导致错误的代码部分是:
batch.begin();
font.setColor(1.0f, 1.0f, 1.0f,1.0f);
font.draw(batch, str, 25, 160);
batch.end();
如果需要,整个程序看起来像这样
package com.me.fixGame;
import java.util.Random;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
public class fixGame implements ApplicationListener {
SpriteBatch batch;
SpriteBatch spriteBatch;
Texture trash;
Texture paper;
SpriteBatch spritebatch;
Vector2 position;
Vector2 size;
Vector2 size2;
Vector2 pos;
Rectangle bounds;
Rectangle bounds2;
int score = 6;
CharSequence str = "Hello World!";
BitmapFont font;
boolean collision = false;
@Override
public void create() {
spriteBatch = new SpriteBatch();
trash = new Texture(Gdx.files.internal("data/trash.png"));
paper = new Texture(Gdx.files.internal("data/paper1.jpg"));
position = new Vector2(100, 50);
pos = new Vector2(54, 14);
batch = new SpriteBatch();
size2 = new Vector2(trash.getWidth() ,trash.getHeight() );
//size2.y = trash.getHeight();
//size2.x = trash.getWidth();
size = new Vector2(paper.getWidth() ,paper.getHeight());
bounds= new Rectangle(pos.x, pos.y, size.x, size.y);
bounds2= new Rectangle(position.x, position.y, size2.x, size2.y);
}
@Override
public void dispose() {
}
public void update(){
bounds.set(pos.x, pos.y, size.x, size.y);
bounds2.set(position.x, position.y, size2.x, size2.y);
position.x = position.x -2- Gdx.input.getAccelerometerX();
}
@Override
public void render() {
if(bounds.overlaps(bounds2)){
System.out.println("good job");
}else if(pos.y > 675){
score= score-1;
System.out.println("one point lost your score is "+ score);
}
update();
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
pos.y=pos.y-12;
if(pos.y<0){
pos.y = 700;
Random randomGenerator = new Random();
pos.x = randomGenerator.nextInt(500);
}
if(position.x>400){
position.x=position.x-30;
}
if(position.x<0){
position.x=-position.x;
}
if(Gdx.input.isKeyPressed(Keys.D)){
position.x= 10+position.x;
}
if(Gdx.input.isKeyPressed(Keys.A)){
position.x= position.x-10;
}
batch.begin();
batch.draw(paper, pos.x, pos.y);
batch.draw(trash, position.x, position.y);
font.setColor(1.0f, 1.0f, 1.0f,1.0f);
font.draw(batch, str, 25, 160);
batch.end();
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
}
这是我得到的例外:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.me.fixGame.fixGame.render(fixGame.java:113)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:206)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:113)
答案 0 :(得分:2)
答案 1 :(得分:1)
您永远不会为您的字体实例变量分配内存。