2014-02-19 3 views
2

아래 코드를 사용하여 텍스트를 화면에 표시하려고하지만이 코드 부분을 실행할 때마다 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); 
      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

코드에서 str의 값을 어디에 설정합니까? –

+0

"Hello World!"와 동일한 CharSequence str을가집니다. –

답변

2

create 메소드 내에서 글꼴을 인스턴스화해야합니다. 이 같은

뭔가 :

BitmapFont font = new BitmapFont(); 

공식 문서 here.

1

글꼴 인스턴스 변수에 메모리를 할당하지 마십시오.