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

2015/2/27

Return method isn't working from outside?

Lambda Lambda

2015/2/27

#
Hey, I'm trying to access int method in world Class. Whenever I do that, I'm getting an error. Here's code I have: code in my actor class:
BridgeWorld world = (BridgeWorld) getWorld();
int test = world.getRandomNumber(100, 200);
code in my world class:
    public int getRandomNumber(int start, int end)
    {
       int number = Greenfoot.getRandomNumber(end - start);

       return number + start;
    }
davmac davmac

2015/2/27

#
Whenever I do that, I'm getting an error.
Which is...?
Lambda Lambda

2015/2/27

#
The error was the following:
java.lang.NullPointerException
But the problem was in first line of the code I provided. Replaced
BridgeWorld world = (BridgeWorld) getWorld();
with:
BridgeWorld world = new BridgeWorld();
And there is no error now
davmac davmac

2015/2/27

#
Here's another option. Declare the method static, and then you don't need any reference to use it:
public static int getRandomNumber(int start, int end)
{
   int number = Greenfoot.getRandomNumber(end - start);
 
   return number + start;
}
int test = BridgeWorld.getRandomNumber(100, 200);
Lambda Lambda

2015/2/27

#
Oh! That's useful! Thank you!
You need to login to post a reply.