Ok, so I've got my mule 4 app all set up here, created a little java class to emulate a dice roll and hooked it up. It compiles, but I get this error when I try to hit it:
"Invocation of static Method 'rollDie(java.lang.String)' from Class 'objects.Dice' with arguments [java.lang.String sides] resulted in an error.
Expected arguments are [java.lang.String sides]"
I've got a little ENUM called Die
package objects;
public enum Die {
FOUR,SIX,EIGHT,TEN,TWELVE,TWENTY,HUNDRED;
}
and my main java class named Dice
package objects;
import java.util.Random;
public class Dice {
private static Random rand;
public Dice()
{
rand = new Random();
}
public static int rollDie(String sides)
{
Die die = getDie(sides);
// Die die = Die.TWENTY;
switch(die)
{
case FOUR:
return rand.nextInt(4)+1;
case SIX:
return rand.nextInt(6)+1;
case EIGHT:
return rand.nextInt(8)+1;
case TEN:
return rand.nextInt(10)+1;
case TWELVE:
return rand.nextInt(12)+1;
case TWENTY:
return rand.nextInt(20)+1;
case HUNDRED:
return rand.nextInt(100)+1;
default:
return -1;
}
}
private static Die getDie(String input)
{
if(input.equals("4"))
return Die.FOUR;
else if(input.equals("6"))
return Die.SIX;
else if(input.equals("8"))
return Die.EIGHT;
else if(input.equals("10"))
return Die.TEN;
else if(input.equals("12"))
return Die.TWELVE;
else if(input.equals("20"))
return Die.TWENTY;
else if(input.equals("100"))
return Die.HUNDRED;
else
return null;
}
}
My flow is just a listener on /roll with standard http listener. static invoke call with the class "objects.Dice" calling rollDie() and passing the arg like this
{
sides : "20"
}
Not sure what I'm doing wrong here. Any insight from anyone? I'm told I don't have to do the import thing with the mule-artifact.json file because of the newest version of mule 4 so not sure what else I can do to show this interface that it's getting the right type of input... help?
Thanks, Jon
Your Dice class is entirely static. There is no need for an instance.
Except.. your random
variable is only actually assigned when you make an instance.
Thus, this class makes no sense. It's designed to be a utility class, which is completely broken unless you make one instance, but instances are completely useless. Thus, to use it as written, you'd have to make an instance at some point, exactly once, toss this instance immediately into the garbage, and then continue to use the static methods.
Also, your environment is misconfigured - follow the instructions and enable verbose exception logging. Developing an app where a mistake gets you 'it did not work' and no further detail is, rather literally, hundreds of times more complicated than writing one where you do get some feedback.
To fix this specific issue, private static Random rand = new Random();
, and then just remove the constructor as it is useless.