0

I'm trying to make an array of objects within the class "objects"(placeholder as I don't know what to call these objects yet,) and when I try to run the program it throws two exceptions...

  1. ArrayOutOfBoundsException:0
  2. Could not run the sketch(Target VM failed to initialize)
objects[] obs;
int count;
void setup(){
  fullScreen();
  background(0);
  textSize(70);
  textAlign(CENTER);
  frameRate(60);
  obs = new objects[count];
int objectNumber;
  int index = 0;
  for(objectNumber = 0;objectNumber <=4;objectNumber++){
   obs[index++] = new objects(random(0,width),random(0,height),2);


  }
}
 /* this is a break from the code(im skipping code that really shouldn't affect this(it doesn't call or edit the array or any previously mentioned variables*/
class objects{
    float objectSpeed = 12;
 float xPos;
 float yPos;
 objects(float tempxPos,float tempyPos, int tempSpeed){
   xPos = tempxPos;
   yPos = tempyPos;
   objectSpeed = tempSpeed;


 }
 void update(){
   yPos = yPos+objectSpeed;


 }
 void Display(){
 fill(255);
ellipse(xPos,yPos,20,20);

 }


}
/* more skipping in the draw block, again doesn't call or edit anything previously declared*/

function draw(){

for(objects ob : obs ){
ob.update();
ob.Display();

}

}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578

1 Answers1

0

You've got a couple things going on.

First, you forgot to give your count variable a value. In Java, this variable will be assigned a default value of 0. That means this line creates an array of size 0:

obs = new objects[count];

In Java, arrays are not resizable. That also means you can't do this:

for(objectNumber = 0;objectNumber <=4;objectNumber++){
  obs[index++] = new objects(random(0,width),random(0,height),2);

You're trying to add more elements than the array has indexes for. This doesn't work in Java.

Instead, you need to give your array a predefined length, and then only loop using that length.

Shameless self-promotion: here is a tutorial on arrays. You can also find more info in the Processing reference and on Google.

After you fix that, you're also mixing some JavaScript syntax here:

function draw(){

This syntax doesn't work in Java. You need to define your function using a return type. Specifically the draw() function has a void return type:

void draw(){

While we're at it, please get into the habit of following standard naming conventions and indentation. Variables and functions should start with a lower-case letter, and classes should start with an upper-case letter. Properly indenting your code makes it easier to read, which makes it easier to help you.

The best advice I can give you is to start smaller. Start with a simple Processing sketch that just does something simple, like show a single ellipse. Then add one small thing to that, and continue working in small steps. You're trying to do a lot all at one time, which as you've learned will only lead to headaches.

Finally, please don't insult yourself. It's fine to be a beginner at something. I strongly recommend editing your post to remove the places in your post where you call yourself names.

Good luck.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • OMG, I paraphrased the code which is why the function Is In there, im so sorry! I feel stupid now XD anyways thank you, this was very helpful. the reason im not starting simpler is because ive been working with processing.js and p5.js so I know my way around it a little bit better than I would have otherwise, but yeah your probably right, thank you! – soconfoosion Mar 31 '19 at 01:16