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

2021/10/6

using a variable from another class in the world class

1
2
3
xixEmilyxix xixEmilyxix

2021/10/14

#
danpost wrote...
Remove lines 46 and 47 from here and add the following after line 6 above:
beeWorld.runSimulation();
ive done this and changed the signs from || to &&. It is still giving an error which is: java.lang.NullPointerException at BeeWorld.runSimulation(BeeWorld.java:59) at StartButton.act(StartButton.java:26)
danpost danpost

2021/10/14

#
Please show runSimulation method and indicate which line is line 59 in BeeWorld.
xixEmilyxix xixEmilyxix

2021/10/14

#
    public void runSimulation()
    {
         int weatherValue = WeatherInput.weatherValue;

         if(weatherValue >= -40 && weatherValue <= 15)
         {
             //if the weather value is between -40 and 15 then the low temp simulation will run
             lowTemperature();
         } else if(weatherValue >= 16 && weatherValue <= 27)
         {
             //if the weather value is between 16 and 27 then the regular temp simulation will run
             regularTemperature();
         } else if(weatherValue >= 28 && weatherValue <= 40)
         {
             //if the weather value is between 28 and 40 then the high temp simulation will run
             highTemperature();
         }     
    }
line 59: int weatherValue = WeatherInput.weatherValue;
danpost danpost

2021/10/14

#
You need to create the WeatherInput object and place it into the world before you can execute the runSimulation method.
xixEmilyxix xixEmilyxix

2021/10/14

#
d0 i just need to add WeatherInput WeatherInput = new WeatherInput() above that the int weatherValue = WeatherInput.weatherValue;
danpost danpost

2021/10/14

#
xixEmilyxix wrote...
d0 i just need to add WeatherInput WeatherInput = new WeatherInput() above that the int weatherValue = WeatherInput.weatherValue;
In your entire project, you should only have ONE variable (as a field, aka instance member):
WeatherInput weatherInput;
Also, only ONCE in the project should you have the assignment:
weatherInput = new WeatherInput();
If you do have multiple fields, you will need to make sure that they will ALL contain the SAME instance of WeatherInput. This can be ensured by making sure that the string "new WeatherInput" only appears one time, and one time only; and making sure that ALL multiple fields as assigned properly (that none are left 'null'). I was wondering if it is really necessary to have a WeatherInput field in your MainMenu class at all. It would be much simpler to just have ONE in your BeeWorld, creating and assigning it in the BeeWorld constructor.
xixEmilyxix xixEmilyxix

2021/10/14

#
how would i assign it in my beeworld class if the input box is in the main menu?
danpost danpost

2021/10/14

#
xixEmilyxix wrote...
how would i assign it in my beeworld class if the input box is in the main menu?
Okay, maybe it would be better to just pass the weatherValue to the BeeWorld and not have a WeatherInput field in BeeWorld. In BeeWorld, first change
WeatherInput weatherInput;
to
int weatherValue;
Then change the constructor declaration from
public BeeWorld()
to
public BeeWorld(int weatherVal)
Inside the constructor, before:
runSimulation();
add this line:
weatherValue = weatherVal;
In runSimulation, remove first line. In StartButton class, change
BeeWorld beeWorld = new BeeWorld();
to
BeeWorld beeWorld = new BeeWorld(weatherInput.weatherValue);
(I began all variable names with lowercase letters, which they, by convention, should be; to be unconventional, you may have to change them back to uppercase)
xixEmilyxix xixEmilyxix

2021/10/14

#
i now have this in BeeWorld:
public class BeeWorld extends World
{
    //declare variables
    int beeValue;
    Flower PinkFlower;
    Flower PurpleFlower;
    Flower YellowFlower;
    StartButton StartButton;
    int WeatherInput;
    Hive Hive;
    Bee Forager;
    Bee Worker;
    Bee Queen;
    /**
     * Constructor for objects of class BeeWorld.
     * 
     */
    public BeeWorld(int weatherVal)
    {
        //set background to main screen when button is pressed
        super(864, 540, 1);
        setBackground(new GreenfootImage("Background.png"));
        
        //add the add day button
        AddDay AddDay = new AddDay ();
        addObject (AddDay, getWidth (), getHeight()); 
        AddDay.setLocation(820,500);
        
        //add the bee counter
        
        showText(("Bees: " + beeValue),  810, 20);
        
        //add the day counter
        showText("Day: 0", 820, 50);
        
        //add the honey counter
        showText("Honey: 0", 810, 110);
        
        //add the help button
        HelpButton HelpButton = new HelpButton ();
        addObject (HelpButton, getWidth (), getHeight ()); 
        HelpButton.setLocation(100,190);
        
        weatherValue = weatherVal;
        runSimulation();
    }
    
    public void runSimulation()
    {
        if(weatherValue >= -40 && weatherValue <= 15)
         {
             //if the weather value is between -40 and 15 then the low temp simulation will run
             lowTemperature();
         } else if(weatherValue >= 16 && weatherValue <= 27)
         {
             //if the weather value is between 16 and 27 then the regular temp simulation will run
             regularTemperature();
         } else if(weatherValue >= 28 && weatherValue <= 40)
         {
             //if the weather value is between 28 and 40 then the high temp simulation will run
             highTemperature();
         }     
    }
and this in startbutton: public void act() { if(Greenfoot.mouseClicked(this)) { BeeWorld beeWorld = new BeeWorld(weatherInput.weatherValue); beeWorld.runSimulation(); beeWorld.WeatherInput = ((MainMenu)getWorld()).WeatherInput; Greenfoot.setWorld(beeWorld); } } and in MainMenu:
    public MainMenu()
    {    
        // Create the main menu
        super(864, 540, 1);
        Greenfoot.start();
        setBackground(new GreenfootImage("MainMenu.png"));
        
        //add the help button
        HelpButton HelpButton = new HelpButton ();
        addObject (HelpButton, getWidth (), getHeight ()); 
        HelpButton.setLocation(100,190);       
        
        //add start button to main menu
        StartButton StartButton = new StartButton ();
        addObject (StartButton, getWidth (), getHeight ()); 
        StartButton.setLocation(150,350);
        
        //add weather input box to main menu
        addObject (WeatherInput, getWidth (), getHeight ()); 
        WeatherInput.setLocation(400,250);
    }
and im getting the following errors: BeeWorld: weatherValue = weatherVal; - cannot find symbol variable weather MainMenu: addObject (WeatherInput, getWidth (), getHeight ()); - cannot find symbol variable WeatherInput WeatherInput.setLocation(400,250); - non static method setLocation cannot be referenced from a static context StartButton: BeeWorld beeWorld = new BeeWorld(weatherInput.weatherValue); - cannot find symbol variable WeatherInput beeWorld.WeatherInput = ((MainMenu)getWorld()).WeatherInput; - cannot find symbol variable WeatherInput so i'm not sure how to get them to know WeatherInput?
danpost danpost

2021/10/14

#
In BeeWorld code, change line 9 to:
int weatherValue;
In StartButton, change act to:
public void act()
{
    if (Greenfoot.mouseClicked(this))
    {
        setWorld(new BeeWorld(((MainMenu)getWorld()).weatherInput.weatherValue));
    }
}
In the addObject line, use "weatherInput".
xixEmilyxix xixEmilyxix

2021/10/14

#
im still getting the same error in the mainmenu for
        addObject (weatherInput, getWidth (), getHeight ()); 
        WeatherInput.setLocation(400,250);
and getting an error in startbutton
setWorld(new BeeWorld(((MainMenu)getWorld()).weatherInput.weatherValue));
saying cannot find symbol - method setWorld(BeeWorld) and also cannot find symbol variable weatherInput
danpost danpost

2021/10/14

#
xixEmilyxix wrote...
im still getting the same error in the mainmenu for
        addObject (weatherInput, getWidth (), getHeight ()); 
        WeatherInput.setLocation(400,250);
Line 2 should be:
weatherInput.setLocation(400,250);
and getting an error in startbutton
setWorld(new BeeWorld(((MainMenu)getWorld()).weatherInput.weatherValue));
saying cannot find symbol - method setWorld(BeeWorld) and also cannot find symbol variable weatherInput
Use:
Greenfoot.setWorld(new BeeWorld(((MainMenu)getWorld()).weatherInput.weatherValue));
danpost danpost

2021/10/14

#
Also, in MainMenu, do you have this field?
WeatherInput weatherInput = new WeatherInput();
xixEmilyxix xixEmilyxix

4 days ago

#
thank you its all fixed now
You need to login to post a reply.
1
2
3