I have a variable that needs access throughout all classes it is declared in class Buttons but i can't get MaleButton and FemaleButton to access it (they both extend from Buttons) do i have to make it a public variable or?
private int Male = 0;
private int Female = 0;if (Greenfoot.mouseClicked(this))
{
Male++;
if(Male != 1)
{
getWorld().removeObject(this);
}
}if (Greenfoot.mouseClicked(this))
{
Female++;
if(Female != 1)
{
getWorld().removeObject(this);
}
}protected int Male = 0; protected int Female = 0;
protected int Male = 0; protected int Female = 0;
public class MaleButton extends Buttons {}if (Male == 1) // which needs to acquire the button object to get access to the Male field // you would be using something like this if (getWorld().getObjects(MaleButton.class).isEmpty())
public class MaleButton extends Buttons {}if (Male == 1) // which needs to acquire the button object to get access to the Male field // you would be using something like this if (getWorld().getObjects(MaleButton.class).isEmpty())
public class FemaleButton extends Buttons
{
/**
* Act - do whatever the FemaleButton wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if (Greenfoot.mouseClicked(this))
{
getWorld().removeObject(this);
}
if (getWorld().getObjects(MaleButton.class).isEmpty())
{
getWorld().removeObject(this);
}
}
}public class MaleButton extends Buttons
{
/**
* Act - do whatever the MaleButton wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if (Greenfoot.mouseClicked(this))
{
getWorld().removeObject(this);
}
if (getWorld().getObjects(MaleButton.class).isEmpty())
{
getWorld().removeObject(this);
}
}
}java.lang.NullPointerException at MaleButton.act(MaleButton.java:21) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211)
// change your MaleButtton class to this:
public class MaleButton extends Buttons {}
// change your FemaleButton class to this:
public class FemaleButton extends Buttons {}
// change your Buttons class to this:
import greenfoot.*;
public class Buttons extends Actor
{
public void act()
{
if (Greenfoot.mouseClicked(this)) getWorld().removeObject(this);
}
}// in World subclass if (getObjects(MaleButton.class).isEmpty()) // in Actor subclass if (getWorld.getObjects(MaleButton.class).isEmpty())