2016-10-30 100 views
0

我在第10章的工作出書學習Libgdx遊戲開發的處理添加音樂和聲音的遊戲時LibGDX空指針異常。我試着用Googling來查看是否有其他人遇到了與添加音樂相同的空指針問題,但沒有運氣。試圖加載音樂

本質上試圖從一個叫做資產在我的主遊戲運行不同類的音樂曲目加載。

我看到資產被加載,它打印出的控制檯上。所以我不認爲這是沒有找到該文件的問題。

com.woodgdx.game.Assets: # of assets loaded: 9 
com.woodgdx.game.Assets: asset: ../core/assets/sounds/jump_with_feather.wav 
com.woodgdx.game.Assets: asset: ../core/assets/canyonbunny.pack.png 
com.woodgdx.game.Assets: asset: ../core/assets/music/keith303_-_brand_new_highscore.mp3 
com.woodgdx.game.Assets: asset: ../core/assets/sounds/live_lost.wav 
com.woodgdx.game.Assets: asset: ../core/assets/canyonbunny.pack2.png 
com.woodgdx.game.Assets: asset: ../core/assets/sounds/pickup_feather.wav 
com.woodgdx.game.Assets: asset: ../core/assets/sounds/jump.wav 
com.woodgdx.game.Assets: asset: ../core/assets/canyonbunny.pack.atlas 
com.woodgdx.game.Assets: asset: ../core/assets/sounds/pickup_coin.wav 

所以,我真的不知道是怎麼回事......

錯誤本身是..

Exception in thread "LWJGL Application" java.lang.NullPointerException 
    at com.woodgdx.game.woodGdxGame.create(woodGdxGame.java:29) 
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:147) 
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:124) 

主要類

package com.woodgdx.game; 

import com.badlogic.gdx.Application; 
import com.badlogic.gdx.Game; 
import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.assets.AssetManager; 
import com.woodgdx.game.Assets; 
import com.woodgdx.game.screens.MenuScreen; 
import com.woodgdx.game.util.AudioManager; 
import com.woodgdx.game.util.GamePreferences; 

/** 
* The main game file with the main 
* engine methods 
* @author xxxx 
*/ 
public class woodGdxGame extends Game 
{ 
    @Override 
    public void create() 
    { 
     // Set Libgdx log level 
     Gdx.app.setLogLevel(Application.LOG_DEBUG); 
     // Load assets 
     Assets.instance.init(new AssetManager()); 
     // Load preferences for audio settings and start playing music 
     GamePreferences.instance.load(); 
     AudioManager.instance.play(Assets.instance.music.song01); 
     // Start game at menu screen 
     setScreen(new MenuScreen(this)); 
    } 
} 

出現的錯誤在這條線..

AudioManager.instance.play(Assets.instance.music.song01); 

我的資產類中包含這個內部類。這本書的方式,當我將它製作成自己的課程時,我得到了同樣的錯誤。

/** 
    * Music for game loaded 
    * @author xxxx 
    * 
    */ 
    public class AssetMusic 
    { 
     //Our music object 
     public final Music song01; 

     /** 
     * Retrieves the music file for the music 
     * @param am 
     */ 
     public AssetMusic(AssetManager am) 
     { 
      song01 = am.get("../core/assets/music/keith303_-_brand_new_highscore.mp3", Music.class); 
     } 
    } 

然後這裏是AudioManager類,它們在這裏扮演一個小角色。

/** 
* Manages the playing and organization 
* of audio files 
* @author xxxx 
* 
*/ 
public class AudioManager 
{ 
    public static final AudioManager instance = new AudioManager(); 

    private Music playingMusic; 

    // singleton: prevent instantiation from other classes 
    private AudioManager() 
    { 
    } 

    /** 
    * Play sound with default vol, pitch, and pan (0) 
    * @param sound 
    */ 
    public void play(Sound sound) 
    { 
     play(sound, 1); 
    } 

    /** 
    * Play sound with default pitch and pan (0) but different vol 
    * @param sound 
    * @param volume 
    */ 
    public void play(Sound sound, float volume) 
    { 
     play(sound, volume, 1); 
    } 

    /** 
    * Play sound with default pan (0) with different pitch and vol 
    * @param sound 
    * @param volume 
    * @param pitch 
    */ 
    public void play(Sound sound, float volume, float pitch) 
    { 
     play(sound, volume, pitch, 0); 
    } 

    /** 
    * Plays sound with a vol, pitch, and pan activated 
    * @param sound 
    * @param volume 
    * @param pitch 
    * @param pan 
    */ 
    public void play(Sound sound, float volume, float pitch, float pan) 
    { 
     //If sound is muted, dont play 
     if (!GamePreferences.instance.sound) 
      return; 
     sound.play(GamePreferences.instance.volSound * volume, pitch, pan); 
    } 

    /** 
    * Plays music on loop 
    * @param music 
    */ 
    public void play(Music music) 
    { 
     stopMusic(); 
     playingMusic = music; 
     if (GamePreferences.instance.music) 
     { 
      music.setLooping(true); 
      music.setVolume(GamePreferences.instance.volMusic); 
      music.play(); 
     } 
    } 

    /** 
    * Ends the music loop of doom 
    */ 
    public void stopMusic() 
    { 
     if (playingMusic != null) 
      playingMusic.stop(); 
    } 

    /** 
    * Checks settings for muted music, vol level 
    */ 
    public void onSettingsUpdated() 
    { 
     //No music available 
     if (playingMusic == null) 
      return; 
     //Sets volume 
     playingMusic.setVolume(GamePreferences.instance.volMusic); 
     //Checks if music is not muted 
     if (GamePreferences.instance.music) 
     { 
      //If music isn't playing, play it 
      if (!playingMusic.isPlaying()) 
       playingMusic.play(); 
     } 
     //Mute music if selected so 
     else 
     { 
      playingMusic.pause(); 
     } 
    } 
} 
+1

您應該使用封裝的init()方法,但無論問題是'資產#music'爲空,因爲沒有別的在該行與工作不取消引用」 T A常數(我假設'資產#instance'的製造方式爲'AudioManager'相同) – Rogue

+0

啊好吧,是啊,結束了東西與Libgdx有初始化方法。是的,我基本上一直在複製書本代碼,我正在學習其他課程,而且我沒有時間花時間爲它編寫代碼,併爲其編寫了良好的軟工程原則。 – user2052752

+0

儘管謝謝你的幫助,但卻帶給了我正確的方向。 – user2052752

回答

0

忘記爲Assets類的初始化添加音樂和聲音。我認爲這是與文件的東西,但它只是沒有初始化。

一下添加到資產

sounds = new AssetSounds(assetManager); 
music = new AssetMusic(assetManager); 
+0

現在您知道NullPointerException的解決方案了,請在發佈到StackOverflow之前嘗試解決該特定異常。原因總是一樣的 –