0

So, I've been doing this java class for several months now, and I've been assigned a project before the end of the year. I'm trying to rewrite some old code to use vectors and arrays, but... I get the topic title error. Here is the relevant code:

public static double VectorX(int len, double angle) {
      return Math.cos(angle)*len;
}
public static double VectorY(int len, double angle) {
      return Math.sin(angle)*len;
}
public static class Projectile {
    public int x;
    public int y;
    public double angle;
    public int speed;
    public boolean Player;
}

...

public static Projectile[] Shoot = new Projectile[0];

public static double RadianAngle(int x1, int y1, int x2, int y2) {
      return Math.atan2(x2-x1, y2-y1);
    }

...

for (int i = 1; i <= Shoot.length; i++)
        {
            Shoot[i].x += VectorX(Shoot[i].speed, Shoot[i].angle);
            Shoot[i].y += VectorY(Shoot[i].speed, Shoot[i].angle);
        }

...

if (Cooldown == 75 || Cooldown == 25)
            {
                Projectile Hey = new Projectile();
                Hey.x = EX;
                Hey.y = EY;
                Hey.Player = false;
                Hey.speed = 2;
                Hey.angle = RadianAngle(Hey.x, Hey.y, X, Y);
                Projectile[] Shoot2 = new Projectile[Shoot.length + 1];
                for (int l = 0; l <= Shoot.length - 1; l++)
                {
                    Shoot2[l] = Shoot[l];
                }
                Shoot2[Shoot2.length - 1] = Hey;
                Shoot = Shoot2;
            }

I've no idea what is going on. I imported these Vector functions from a C#-based language that I am well versed in, but translated them to java. I am getting the error at

            Shoot[i].x += VectorX(Shoot[i].speed, Shoot[i].angle);
            Shoot[i].y += VectorY(Shoot[i].speed, Shoot[i].angle);

Could you guys give me a hand?

Codebender
  • 14,221
  • 7
  • 48
  • 85
Dimentio
  • 21
  • 6
  • I think the error is coming from the fact that the array's size is 0. But I am pretty sure that I am increasing the size? – Dimentio Dec 18 '15 at 01:04
  • have you tried some debugger? The learning experience is invaluable. Write inputs and expected outputs at every step, at some point you should see the light. That is the every day of a programmer - don't outsource it, there is no shortcut to becoming a good programmer. – user3743222 Dec 18 '15 at 01:10
  • Comments: 1) You have significant style problems. You have methods that start with a capital letter. You have variables / fields whose names start with a capital letters. Major badness. 2) *"I'm trying to rewrite some old code to use vectors ... "* : if you mean `Vector`, then you should use `ArrayList`. `Vector` is a legacy class with synchronization overheads that you (probably) don't need. – Stephen C Dec 18 '15 at 01:15

3 Answers3

0
for (int i = 1; i <= Shoot.length; i++)

should be

for (int i = 0; i < Shoot.length; i++)

the reason why

int[] arr = new int[]{ 0, 1, 2 };

arr.length will now equal 3, my array does not have an index at 3, instead, it has indexes at 0, 1, and 2.

Matt Clark
  • 27,671
  • 19
  • 68
  • 123
  • I'll try that. Thanks! – Dimentio Dec 18 '15 at 01:05
  • 1
    As pointed out by _Sparta_, your array was defined to have a `0` size. Now, this loop will never run because `0 !< 0`. – Matt Clark Dec 18 '15 at 01:10
  • Okay, it was running a second ago, but now I'm getting another error: `for (int i = 0; i < Shoot.length - 1; i++) {` `if (Shoot[i].x > 0 && Shoot[i].x < 190 && Shoot[i].y > 0 && Shoot[i].y < 190 && Shoot[i].Player == true) c.drawString(Attack, Shoot[i].x, Shoot[i].y);` Null pointer exception. – Dimentio Dec 18 '15 at 01:13
0

You're getting an ArrayIndexOutOfBoundsException because you are initializing the Shoot array with a size of 0 :

public static Projectile[] Shoot = new Projectile[0];

so the call to

Shoot[0].x += VectorX(Shoot[0].speed, Shoot[0].angle);

in your loop is invalid.

Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67
  • huh, i guess that is an issue too. i think the OutOfBound exception came from the `>=` in the for loop - with my change the exception will go away and his loop will just be skipped right over because it is, as you said, a size of 0. – Matt Clark Dec 18 '15 at 01:07
  • You're probably right. He should try that out. – Mohammed Aouf Zouag Dec 18 '15 at 01:08
0

Your definition for the shoot array creates an array of length 0: Shoot = new Projectile[0];

Yet when you iterate over the array, you are setting the bounds of your loop variable incorrectly:

for (int i = 1; i <= Shoot.length; i++)

Arrays are zero-indexed (meaning an array of length 1 will have index [0] and no more). So starting your loop index at 1 (int i = 1) is bad to begin with. Then you are looping too far. Say you have 3 elements in your array, the indexes will be 0, 1 & 2. And your array.length will be 3, but you never want to index 3. So your loop condition needs to be < array.length, not <= array.length.

Ian
  • 1,475
  • 2
  • 15
  • 31