# Soundfehler



## juli1234 (30. Nov 2017)

Hallo. Ich soll in der Schule das Programmieren mit Netbeans und der Programmiersprache Java erlernen. Jedoch möchte mein Lehrer mir nicht viel erklären, da wir es uns "selber erarbeiten" sollen.
An sich bin ich mit meinem Spiel fast fertig. Wir haben eine Art Tutorial bekommen und sollten manche Sachen einfach nur abtippen, was ich dann auch getan habe.
Trotzallem bekomme ich eine Fehlermeldung. Kann mir jemand sagen, wieso und wie ich diesen Fehler beheben kann? Danke im voraus.



```
package gamepanel;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.ListIterator;
import java.util.Vector;
import javax.imageio.ImageIO;
import javax.swing.Timer;

public class GamePanel extends JPanel implements Runnable, KeyListener, ActionListener{
private static final long serialVersionUID = 1L;
JFrame frame;
long delta = 0;
long last = 0;
long fps = 0;
long gameover = 0;

Pacman copter;
Vector<Sprite>actors;
Vector<Sprite> painter;

boolean up;
boolean down;
boolean left;
boolean right;
boolean started;
int speed = 50;



Timer timer;
BufferedImage[] ghost;
BufferedImage[] explosion;
BufferedImage background;

SoundLib soundlib;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new GamePanel(800,600);
        // TODO code application logic here
    }
    public GamePanel (int w, int h) {
        this.setPreferredSize(new Dimension(w,h));
        this.setBackground(Color.BLACK);
        frame=new JFrame("GameDemo");
        frame.setLocation(100,100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(this);
        frame.addKeyListener(this);
        frame.pack();
        frame.setVisible(true);
     
 
        Thread th = new Thread(this);
        th.start();
    }
 
    private void doInitializations(){
        last = System.nanoTime();
        gameover = 0;
     
        BufferedImage[] pacman = loadPics("gamepanel/pacman.gif", 4);
        ghost = loadPics("gamepanel/ghost.gif",8);
        background = loadPics("gamepanel/background.jpg",1) [0];
        explosion = loadPics("gamepanel/explosion.gif",5);
     
        actors = new Vector<Sprite>();
        painter = new Vector<Sprite>();
        copter = new Pacman(pacman,400,300,100,this);
        actors.add(copter);
     
        soundlib = new SoundLib();
        soundlib.loadSound("bumm", "sound/boom.wav");
        soundlib.loadSound("rocket", "sound/ghost_start.wav");
        soundlib.loadSound("pacman", "sound/pacman.wav");
     
        createApples();
     
        timer = new Timer (3000,this);
        timer.start();
        started = true;
    

    }
 
    private void createApples(){
     
        BufferedImage[] bi = loadPics("gamepanel/apple.gif", 1);
     
        for(int y=10;y<getHeight();y+=50){
            int x = (int)(Math.random()*getWidth());
            Apple apple = new Apple(bi,x,y,1000,this);
            actors.add(apple);
        }
    }
 
    private void createGhost(){
        int x = 0;
        int y = (int) (Math.random()*getHeight());
        int hori = (int) (Math.random()*2);
     
        if (hori==0){
            x = -30;
        }else{
            x = getWidth()+30;
        }
     
        Ghost gho = new Ghost (ghost,x,y,100,this);
        if(x<0){
            gho.setHorizontalSpeed(100);
        }else{
            gho.setHorizontalSpeed(-100);
        }
     
        ListIterator<Sprite> it = actors.listIterator();
        it.add(gho);
        soundlib.playSound("ghost");
    }
 
    public void createExplosion(int x, int y){
        ListIterator<Sprite> it = actors.listIterator();
        it.add(new Explosion(explosion,x,y,100,this));
        soundlib.playSound("bumm");
    }
 
 
 
@Override
public void run() {
    while (frame.isVisible()){
        computeDelta();
     
        if(isStarted()){
            checkKeys();
            doLogic();
            moveObjects();
            cloneVectors();
        }
     
        repaint();
     
        try{
            Thread.sleep(10);
        } catch (InterruptedException e) {}
    }
}

@SuppressWarnings("unchecked")
private void cloneVectors(){
    painter = (Vector<Sprite>) actors.clone();
}



private void computeDelta() {
 
    delta = System.nanoTime() - last;
    last = System.nanoTime();
    fps = ((long) 1e9)/delta;
 
}
@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
 
    g.drawImage(background, 0, 0, this);
 
    g.setColor(Color.red);
    g.drawString("FPS: "+Long.toString(fps), 20, 10);

    if(!started){
        return;
    }
     
    for(ListIterator<Sprite> it = painter.listIterator();it.hasNext();){
      Sprite r = it.next();
      r.drawObjects(g);
     }

}

private void moveObjects(){
 
    for(ListIterator<Sprite> it = actors.listIterator();it.hasNext();){
        Sprite r = it.next();
        r.move(delta);
    }
}
private void doLogic(){
 
    for(ListIterator<Sprite> it = actors.listIterator();it.hasNext();){
        Sprite r = it.next();
        r.doLogic(delta);
     
        if(r.remove){
            it.remove();
        }
    }
 
    for(int i= 0;i < actors.size();i++){
        for(int n = i+1; n<actors.size(); n++){
         
            Sprite s1 = actors.elementAt(i);
            Sprite s2 = actors.elementAt (n);
         
            s1.collidedWith(s2);
        }
    }
 
    if(copter.remove && gameover==0){
        gameover = System.currentTimeMillis();
    }
 
    if(gameover>0){
        if(System.currentTimeMillis()-gameover>3000){
            stopGame();
        }
    }
 
 
 
    System.out.println(actors.size());
}

private void startGame(){
    doInitializations();
    setStarted(true);
    soundlib.loopSound("pacman");
}


private void stopGame(){
    timer.stop();
    setStarted(false);
    soundlib.stopLoopingSound();
}


private void checkKeys (){
 
    if(up){
      copter.setVerticalSpeed(-speed);
   
    }
    if(down){
        copter.setVerticalSpeed(speed);
    }
    if(right){
        copter.setHorizontalSpeed(speed);
    }
    if(left){
        copter.setHorizontalSpeed(-speed);
    }
    if(!up&&!down){
        copter.setVerticalSpeed(0);
    }
    if(!left&&!right){
        copter.setHorizontalSpeed(0);
    }
    }

private BufferedImage[] loadPics(String path, int pics){
 
    BufferedImage[] anim = new BufferedImage[pics];
    BufferedImage source = null;
 
    URL pic_url = getClass().getClassLoader().getResource(path);
 
    try{
        source = ImageIO.read(pic_url);
    }   catch (IOException e) {}
 
    for(int x=0;x<pics;x++){
        anim[x] = source.getSubimage(x*source.getWidth()/pics, 0,
                source.getWidth()/pics, source.getHeight());
    }
 
    return anim;
 
}
@Override
public void keyPressed(KeyEvent e) {
 
    if(e.getKeyCode()==KeyEvent.VK_UP){
        up = true;
    }
 
    if(e.getKeyCode()==KeyEvent.VK_DOWN){
        down = true;
    }
 
    if(e.getKeyCode()==KeyEvent.VK_LEFT){
        left = true;
    }
 
    if(e.getKeyCode()==KeyEvent.VK_RIGHT){
        right = true;
    }
}

@Override
public void keyReleased(KeyEvent e){
 
    if(e.getKeyCode()==KeyEvent.VK_UP){
        up = false;
    
    }
    if(e.getKeyCode()==KeyEvent.VK_DOWN){
        down = false;
    }
    if(e.getKeyCode()==KeyEvent.VK_LEFT){
        left = false;
    }     
    if(e.getKeyCode()==KeyEvent.VK_RIGHT){
        right = false;
    }
    if(e.getKeyCode()==KeyEvent.VK_ENTER){
        if(!isStarted()){
            startGame();
        }
    }
 
    if(e.getKeyCode()==KeyEvent.VK_ESCAPE){
        if(isStarted()){
            stopGame();
        }else{
            frame.dispose();
        }
    }
}

@Override
public void keyTyped(KeyEvent e) {
}

public boolean isStarted() {
    return started;
}
public void setStarted(boolean stared) {
    this.started = started;
}

@Override
public void actionPerformed(ActionEvent e) {
     if(isStarted() && e.getSource().equals(timer)){
         createGhost();
     }
  
}
}
```





```
package gamepanel;




import java.applet.*;
import java.net.*;
import java.util.*;


public class SoundLib {
   
    Hashtable<String, AudioClip> sounds;
    Vector<AudioClip> loopingClips;
   
   
   
    public SoundLib(){
        sounds = new Hashtable<String, AudioClip>();
        loopingClips = new Vector<AudioClip>();
    }
   
    public void loadSound(String name, String path){
       
        if(sounds.containsKey(name)){
            return;
        }
       
        URL sound_url = getClass().getClassLoader().getResource(path);
        sounds.put(name, (AudioClip)Applet.newAudioClip(sound_url));
    }
   
    public void playSound(String name){
        AudioClip audio = sounds.get(name);
        audio.play();
}
    public void loopSound(String name){
    AudioClip audio = sounds.get(name);
    loopingClips.add(audio);
    audio.loop();
   
}
    public void stopLoopingSound(String name){
        for (AudioClip c:loopingClips){
       c.stop();
   }
   
}
}
```


----------



## mrBrown (30. Nov 2017)

juli1234 hat gesagt.:


> Trotzallem bekomme ich eine Fehlermeldung.


Und welche?


----------



## juli1234 (30. Nov 2017)

Also das steht in der Konsole/ Ausgabefenster, wenn ich mein Spiel starten möchte.


```
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at sun.applet.AppletAudioClip.<init>(AppletAudioClip.java:65)
    at java.applet.Applet.newAudioClip(Applet.java:313)
    at gamepanel.SoundLib.loadSound(SoundLib.java:39)
    at gamepanel.GamePanel.doInitializations(GamePanel.java:90)
    at gamepanel.GamePanel.startGame(GamePanel.java:243)
    at gamepanel.GamePanel.keyReleased(GamePanel.java:337)
    at java.awt.Component.processKeyEvent(Component.java:6494)
    at java.awt.Component.processEvent(Component.java:6310)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Window.processEvent(Window.java:2025)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1954)
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:806)
    at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1074)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:945)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:771)
    at java.awt.Component.dispatchEventImpl(Component.java:4760)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at sun.applet.AppletAudioClip.<init>(AppletAudioClip.java:65)
    at java.applet.Applet.newAudioClip(Applet.java:313)
    at gamepanel.SoundLib.loadSound(SoundLib.java:39)
    at gamepanel.GamePanel.doInitializations(GamePanel.java:90)
    at gamepanel.GamePanel.startGame(GamePanel.java:243)
    at gamepanel.GamePanel.keyReleased(GamePanel.java:337)
    at java.awt.Component.processKeyEvent(Component.java:6494)
    at java.awt.Component.processEvent(Component.java:6310)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Window.processEvent(Window.java:2025)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1954)
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:806)
    at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1074)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:945)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:771)
    at java.awt.Component.dispatchEventImpl(Component.java:4760)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
```


----------



## krgewb (30. Nov 2017)

Du schreibst

```
soundlib.stopLoopingSound();
```
aber die Methode verlangt einen String als Parameter.

```
public void stopLoopingSound(String name){
...
}
```


----------



## juli1234 (30. Nov 2017)

Danke. Ich habe gar nicht darauf geachet...


----------



## truesoul (1. Dez 2017)

Hallo. 

Der Lehrer macht es sich ja besonders einfach.

Dies Spiel kommt mir bekannt vor siehe 
https://www.java-forum.org/thema/tutorials-von-mitgliedern.14545/

Ein Tipp. Mit Vektoren wirst du nicht Glücklich da ein Lesen und schreiben im selben Moment ein ConcurrentModificationException verursachen wird. Und das mit großer Wahrscheinlichkeit. Auch wenn du ein Anfänger bist solltest du mit Arrays arbeiten und das Hinzufügen, Lesen und Entfernen von Objekten selber programmieren. Dazu solltest du dich in synchronized einlesen. 

Grüße


----------

