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

2011/12/13

A very strange nullPointer...

1
2
darkmist255 darkmist255

2011/12/13

#
Well, this is a bit of an odd bug, but thanks to the magic of comments I've been able to track down the problem. It happens when I do the following line of code:
injury.setVolume(volumeselect.volumeLevel);
However, it doesn't happen all the time. If I do this in the Spike class, all is fine. Here is the reference in the Spike class:
    Room room = (Room)getWorld();
    VolumeSelect volumeselect = room.volumeselect;
    GreenfootSound injury = new GreenfootSound("Injury.wav");
But if I use the exact same setVolume line in the Stats class (which would make it part of the damagePlayer() method, so much nicer for me :D) I get a nullPointer exception. Here is the reference in the Stats class:
    Room room = (Room)getWorld();
    VolumeSelect volumeselect = room.volumeselect;
    GreenfootSound injury = new GreenfootSound("Injury.wav");
What could be the problem, as they are both actors in the world.
kiarocks kiarocks

2011/12/13

#
what is volume select?
darkmist255 darkmist255

2011/12/13

#
in public class Room extends World public static VolumeSelect volumeselect = new VolumeSelect(); ... addObject(volumeselect, 750, 40); It is an object with a sound icon that when clicked changes the volume.
AwesomeNameGuy AwesomeNameGuy

2011/12/13

#
Is stats an actor?
darkmist255 darkmist255

2011/12/13

#
Yes, and it's in the world as well.
AwesomeNameGuy AwesomeNameGuy

2011/12/13

#
I assume you are using addedToWorld() to get the reference to room. The only pointer that can be null is the room itself, can't be volumeselect because that wouldn't cause an exception if it is null, at least not in the code you posted. EDIT: oh wait, maybe it would. Hmm let me take another look.
AwesomeNameGuy AwesomeNameGuy

2011/12/13

#
Is stats a subclass of another actor?
kiarocks kiarocks

2011/12/13

#
sounds like the volume select thing. Have you tried this code in the world? (excluding the room parts)
darkmist255 darkmist255

2011/12/13

#
@AwesomeNameGuy I'm not using addedToWorld, though I have tried using that. No difference. And no it just extends Actor. @kiarocks I thought it was the volume select thing, but why would the volume select work from Spikes while it doesn't work from Stats?
AwesomeNameGuy AwesomeNameGuy

2011/12/13

#
It's tough to tell from the code posted. It's either injury or volumeselect. Just make sure they are both refrenceing an object by the time line 01 runs and make sure you aren't redefining or derefrencing them anywhere in your stats class. Either that or you can post your scenario with the offending code commmented out and I'll glance at it and see if I can track it down.
kiarocks kiarocks

2011/12/13

#
Yea, I can do the same.
kiarocks kiarocks

2011/12/13

#
Does Spikes extend any thing
darkmist255 darkmist255

2011/12/13

#
Spikes extends Actor, so nothing there. Here's some more code, just ask if you want me to post anything more, but this is everything relevant:
public class VolumeSelect extends Actor
{
    public int volumeLevel = 100;
    
    public void act() 
    {
        checkClick();
    }    
    
    public void checkClick()
    {
        if(Greenfoot.mouseClicked(this))
        {
            if(volumeLevel == 100)
            {
                setImage("VolumeOff.png");
                volumeLevel = 0;
            }
            else if(volumeLevel == 50)
            {
                setImage("VolumeFull.png");
                volumeLevel = 100;
            }
            else if(volumeLevel == 0)
            {
                setImage("VolumeHalf.png");
                volumeLevel = 50;
            }         
        }
    }
}
The lines that are commented out are the working code.
public class Spikes extends Actor
{
    Room room = (Room)getWorld();
    Stats stats = room.stats;
    VolumeSelect volumeselect = room.volumeselect;
    GreenfootSound injury = new GreenfootSound("Injury.wav");
    
    private int delay = 0;
    
    public void addedToWorld(World world)
    {
        injury.setVolume(0);
        injury.play();
    }
    
    public void act() 
    {
        if(delay > 0)
        {
            delay = (delay - 1);
        }
    }
    
    public void spikeDamagePlayer()
        {
            if(delay <= 0)
            {
                //make it damage player
                stats.damagePlayer(1);
                delay = 40;
                
                //injury.setVolume(volumeselect.volumeLevel);
                //injury.play();
            }
        }
}
public class Stats extends Actor
{
    public double shotDuration = 60; //How long the shots can go before decaying. Call it Range
    public double rechargeTime = 20; //The time it takes to recharge a shot
    public double shotRechargeRate = 1; //How fast does the shot recharge
    public double moveSpeed = 5;
    public double damageLevel = 1;
    
    public int health = 10; //The player's health
    
    Room room = (Room)getWorld();
    VolumeSelect volumeselect = room.volumeselect;
    GreenfootSound injury = new GreenfootSound("Injury.wav");
    
    public void addedToWorld(World world)
    {
        //load the injury sound
        injury.setVolume(0);
        injury.play();
    }
    
    public void damagePlayer(int damage)
    {
        health = health - damage;
        injury.setVolume(volumeselect.volumeLevel);
        injury.play();
    }
}
davmac davmac

2011/12/13

#
In Spikes, line 3, you use "getWorld()" while the actor is being constructed, i.e. before the actor has been added to the world, at which time it will return null. So, line 3 sets "room" to null, and line 4 throws a NullPointerException because you're trying to access a variable through a null reference. You need to override the addedToWorld() method and do your initialisation there instead. The problem has nothing to do with the line of code you thought. If you check the stack trace, you will see that it tells you exactly which line the exception occurs on.
kiarocks kiarocks

2011/12/14

#
Stick lines 11 and 12 where you have addedToWorld.
There are more replies on the next page.
1
2