0

I have following code (coords calculations for grenade trajectory)

public class GrenadeTrajectory
{
    public int speedValue;
    public static boolean shotStart;
    public boolean stop;
    ArrayList<Integer> xA = new ArrayList<Integer>();
    ArrayList<Integer> yA = new ArrayList<Integer>();
    Executor executor = Executors.newSingleThreadExecutor();
    Worms worms = new Worms();
    MapSettings MapSettings;
    public void startTask()
    {  
        stop = false;
        executor.execute(new Runnable() 
        {
            @Override
            public void run() 
            {
                int TRAJECTORY_STEPS = 10;
                double alpha = 45.0;
                double x0 = MapSettings.mp_player1_weapon_position_x;
                double y0 = MapSettings.mp_player1_weapon_position_y;
                double v0 = 50.0;
                double g = 8.91;
                double c = Math.sqrt(Math.pow((worms.getWeaponDirectionPosition(true) + MapSettings.mp_player1_weapon_position_x), 2));
                double degrees = Math.acos((worms.getWeaponDirectionPosition(true) - MapSettings.mp_player1_weapon_position_x) / Math.pow(worms.getWeaponDirectionPosition(true) - MapSettings.mp_player1_starting_position_x, 2) - Math.pow(MapSettings.mp_player1_starting_position_y - worms.getWeaponDirectionPosition(false), 2));
                double[] arrayX = new double[TRAJECTORY_STEPS];
                double[] arrayY = new double[TRAJECTORY_STEPS];
                for (int t = 0; t < TRAJECTORY_STEPS; t++)
                {

                    arrayX[t] = x0+v0*Math.cos(degrees)*t;
                    arrayY[t] = y0+v0*Math.sin(degrees)*t-0.5*g*t*t;
                    System.out.format("t=%d x=%f y=%f", t, arrayX[t], arrayY[t]);
                }
                Thread.currentThread().stop();  
            }  
        });
    }
    public void setCons(Worms w, MapSettings cfg)
    {
        xA.clear();
        yA.clear();
        worms = w;
    }
    public void stopTask() throws InterruptedException
    {     
        stop = true; 
    }  
}

And I'm getting following errors / exceptions:

Exception in thread "main" java.lang.StackOverflowError
    at java.util.concurrent.AbstractExecutorService.<init>(AbstractExecutorService.java:71)
    at java.util.concurrent.ThreadPoolExecutor.<init>(ThreadPoolExecutor.java:1305)
    at java.util.concurrent.ThreadPoolExecutor.<init>(ThreadPoolExecutor.java:1198)
    at java.util.concurrent.Executors.newSingleThreadExecutor(Executors.java:134)
    at com.worms.ShotPower.<init>(ShotPower.java:15)
    at com.worms.Worms.<init>(Worms.java:31)
    at com.worms.GrenadeTrajectory.<init>(GrenadeTrajectory.java:19)

And those exceptions I have there like 1000 times. Error is on line

 double c = Math.sqrt(Math.pow((worms.getWeaponDirectionPosition(true) + MapSettings.mp_player1_weapon_position_x), 2));

What am I doing wrong and how can I fix this? Thanks.

esea123
  • 45
  • 1
  • 6
  • It is not a point where you get exception. It is in `ShotPower` class constructor called by `Worms` class constructor and from that class at line `Worms worms = new Worms();` look there please... – Vadim Jan 14 '17 at 13:04
  • ShotPower constructor is empty, and it is being called just in Worms class. I dont probably understand what do you mean and how should I fix it, sorry. – esea123 Jan 14 '17 at 13:15
  • Sorry it is not in constructors it is during class instance variables initiate phase. (you can see in Stack Trace) i.e. when `new GrenadeTrajectory()` called it calls `new Worms()` -> then `Worms` class initiating and calls `new ShotPower()` -> then before ShotPower constructor calls you got an exception. – Vadim Jan 14 '17 at 16:38

0 Answers0