Java 具有libgdx的NullPointerException

Java 具有libgdx的NullPointerException,java,android,nullpointerexception,libgdx,Java,Android,Nullpointerexception,Libgdx,我想使用下面的代码使文本显示在屏幕上,但每当我运行这部分代码时,它就会给出一个错误,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);

我想使用下面的代码使文本显示在屏幕上,但每当我运行这部分代码时,它就会给出一个错误,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() {
    }
}

您需要在create方法中实例化字体

大概是这样的:

BitmapFont font = new BitmapFont();

官方文档。

您从不为字体实例变量分配内存

在代码中,您在哪里设置str的值???我有一个CharSequence str,它等于“helloworld!”
BitmapFont font = new BitmapFont();