2015-04-05 98 views
1

我最近開始Java編碼,並決定在製作遊戲 我已經開始用2D平臺遊戲有裂紋,並發現了一些音頻.java文件在網上和NullPointerException異常 - 音頻 - 2D的Java平臺遊戲

加入他們

但是,當過我運行遊戲,我得到一個NullPointerException及其真的討厭我,爲什麼它不工作

這裏是我的我的音頻文件代碼:

package me.EuanGraphics.Audio; 

import javax.sound.sampled.AudioFormat; 
import javax.sound.sampled.AudioInputStream; 
import javax.sound.sampled.AudioSystem; 
import javax.sound.sampled.Clip; 

public class AudioPlayer { 

private Clip clip; 

public AudioPlayer(String s) { 

    try { 

     AudioInputStream ais = 
       AudioSystem.getAudioInputStream(
         getClass().getResourceAsStream(s) 
         ); 
     AudioFormat baseFormat = ais.getFormat(); 
     AudioFormat decodeFormat = new AudioFormat (
       AudioFormat.Encoding.PCM_SIGNED, 
       baseFormat.getSampleRate(), 
       16, 
       baseFormat.getChannels(), 
       baseFormat.getChannels() * 2, 
       baseFormat.getSampleRate(), false 
      ); 
      AudioInputStream dais = 
        AudioSystem.getAudioInputStream(
          decodeFormat, ais); 
      clip = AudioSystem.getClip(); 
      clip.open(dais); 


    } 
    catch(Exception e) { 
     e.printStackTrace(); 
    } 

} 

public void play() { 
    if(clip == null) return; 
    stop(); 
    clip.setFramePosition(0); 
    clip.start(); 
} 

public void stop() { 
    if (clip.isRunning()) clip.stop(); 
} 

public void close() { 
    stop(); 
    clip.close(); 
} 

}

,這裏是我的遊戲我的菜單狀態,是爲了調出音頻文件並播放它,當你做出一個選擇

package me.EuanGraphics.GameState; 

import java.awt.Color; 
import java.awt.Font; 
import java.awt.Graphics2D; 
import java.awt.image.BufferedImage; 

import javax.imageio.ImageIO; 

import me.EuanGraphics.Entity.PlayerSave; 
import me.EuanGraphics.Handler.Keys; 
import me.EuanGraphics.Tilemap.Background; 
import me.EuanGraphics.Audio.AudioPlayer; 

public class MenuState extends GameState { 

private BufferedImage head; 
private Background menubg; 
private AudioPlayer menuoption; 
private AudioPlayer menuselect; 

private int currentChoice = 0; 
private String[] options = { 
     "Start", 
     "Quit" 
}; 

private Color titleColor; 
private Font titleFont; 

private Font font; 
private Font font2; 

public MenuState(GameStateManager gsm) { 
    super(gsm); 

    try { 

     //Load float head 
     head = ImageIO.read(getClass().getResourceAsStream("/HUD/Hud.gif")).getSubimage(0, 12, 12, 11); 
     menubg = new Background("/Backgrounds/Menu.png", 0); 

     //Titles and fonts 
     titleColor = Color.BLACK; 
     titleFont = new Font("Time New Roman", Font.PLAIN, 20); 
     font = new Font("Arial", Font.PLAIN, 14); 
     font2 = new Font("Arial", Font.PLAIN, 10); 


    } 
    catch(Exception e) { 
     e.printStackTrace(); 
    } 
} 


public void init() { 

    menuoption = new AudioPlayer("/SFX/menuoption.wav"); 
    menuselect = new AudioPlayer("/SFX/menuselect.mp3"); 
} 

public void update() { 

    //Check Keys 
    handleInput(); 

} 


public void draw(Graphics2D g) { 

    //Draw Background 
    menubg.draw(g); 

    // Draw Title 
    g.setColor(titleColor); 
    g.setFont(titleFont); 
    g.drawString("Dragontale: Remastered", 55, 90); 

    // Draw Menu Options 
    g.setFont(font); 
    g.setColor(Color.BLACK); 
    g.drawString("Start", 145, 165); 
    g.drawString("Quit", 145, 185); 

    // Draw Floating Heads 
    if(currentChoice == 0) g.drawImage(head, 125, 154, null); 
    else if(currentChoice == 1) g.drawImage(head, 125, 174, null); 

    //Other 
    g.setFont(font2); 
    g.drawString("2015 Euan P.", 10, 232); 

} 

private void select() { 
    if(currentChoice == 0) { 
     PlayerSave.init(); 
    } 
    else if(currentChoice == 1) { 
     System.exit(0); 
    } 
} 

@Override 
public void handleInput() { 
    if(Keys.isPressed(Keys.ENTER)) select(); 
    if(Keys.isPressed(Keys.UP)) { 
     if(currentChoice > 0) { 
      menuoption.play(); 
      currentChoice--; 
     } 
    } 
    if(Keys.isPressed(Keys.DOWN)) { 
     if(currentChoice < options.length - 1) { 
      menuoption.play(); 
      currentChoice++; 
} 
} 
} 
} 

它給我的,我說的是代碼打一個錯誤所述的AudioFile EG menuoption.play()

由於 - 尤安

+1

你確定在'menuoption.play()'之前調用'init()'嗎?看起來'menuoption'沒有被初始化。 – kba 2015-04-06 00:04:16

+0

我沒有init消息,它在menustate的init部分設置了音頻 – EuanDraws 2015-04-06 02:28:08

+0

你的'MenuState'類有一個初始化函數,它初始化'menuoption',但它似乎沒有被調用,因此'menuoption'是一個空指針。 – kba 2015-04-06 05:00:46

回答

0

寫作:

private AudioPlayer menuoption; 

等於

private AudioPlayer menuoption=null; 

當您嘗試調用一個方法從= null的對象中,您將得到一個NullPointerException。 因爲在調用menuoption.play()之前你沒有調用init()(事實上,你不是在任何地方調用init()),你正在嘗試使用它。 嘗試在MenuState的構造函數的末尾添加init()。