This site requires JavaScript, please enable it in your browser!
Greenfoot back
kasperk22
kasperk22 wrote ...

2014/5/2

NullPointerException with Sounds

kasperk22 kasperk22

2014/5/2

#
Hello guys. I am currently working on sounds on my car. I want it to play a sound when it's idle. And another when it's driving. And another when it's in reverse. And finally a sound for "turbomode". but it does not work everytime i drive, reverse & ect. here is my code:
import greenfoot.*;  
//*  
  
public class Køremand extends SmoothMover  
{  
    int speed = 0;
    int drive1 =2;  
    int turbo1 =5;
    int reverse1 =-1;
    int idle1 =0;
    private KøremandPoint køremandPoint;
    GreenfootSound drive = new GreenfootSound("drive.wav");   
    GreenfootSound turbo = new GreenfootSound("turbo.wav");   
    GreenfootSound idle = new GreenfootSound("idle.wav");   
    GreenfootSound reverse = new GreenfootSound("reverse.wav");   
    GreenfootSound hit = new GreenfootSound("Hit.wav"); 
  
    public Køremand(KøremandPoint køremandpoint)
    {
        køremandPoint = køremandpoint;
    }
    
   public void act()   
    {  
       moving();
       checkCollision();
    }  
    private void moving()
    {
         int change = 0;  
        if (Greenfoot.isKeyDown("6")) change++;   
        if (Greenfoot.isKeyDown("4")) change--;  
        turn(change);  //turn(amount) is basically setRotation(getRotation()+amount)
        int change1=0;  
        if (Greenfoot.isKeyDown("8")) change1 = drive1;  
        if (Greenfoot.isKeyDown("5")) change1 = reverse1;  
        if (Greenfoot.isKeyDown("backspace")) change1 = turbo1;  
        move(change1);  

        if (change1 != speed)  
        {  
            if (speed == idle1 && idle.isPlaying()) idle.stop();  
            if (speed == drive1 && drive.isPlaying()) drive.stop();  
            if (speed == turbo1 && turbo.isPlaying()) turbo.stop();  
            if (speed == reverse1 && reverse.isPlaying()) reverse.stop();  
            speed = change1;  
            if (speed == idle1) idle.playLoop();  
            if (speed == drive1) drive.playLoop();  
            if (speed == turbo1) turbo.playLoop();  
            if (speed == reverse1) reverse.playLoop(); 
    }
    }
    
          private void checkCollision()
    {
        Actor zombie = getOneIntersectingObject(Zombie.class);
     if (zombie != null)
     {
        getWorld().removeObject(zombie);
        getWorld().addObject (new Splat(), getX(), getY());
        hit.play();
        køremandPoint.setValue(køremandPoint.getValue() + 1);
     }
    }
}  
kasperk22 kasperk22

2014/5/3

#
No one?
Pointifix Pointifix

2014/5/3

#
i got a very similar problem a while ago, the thing i have done was to add a little bit of quitness to the end of each sound, this had reduce the chance of getting a null pointer exeption in my scenario. Greenfoot Sound is a little bit buggy i think but just my opinion. regards
davmac davmac

2014/5/3

#
No one?
Your question is way too vague. What do you mean by "does not work" exactly? Your title mentions a NullPointerException, what is the stack trace?
You need to login to post a reply.