Error in Processing program

Error in Processing program
Error in Processing program, if you are a genius - come prove it :slight_smile:
Hi everyone, i am currently creating a game in which i have encountered a problem. My sketch isn’t able to run, and i have no idea why. I get the NullPointerEception when trying to run my sketch. When force quitting the java start-up program, my console shows this “Could not run the sketch (Target VM failed to initialise).” By the way, the game i am trying to create is a picture memory game.

Here is my code:

Cards[] Globalcards = new Cards[12];
int[] x= new int[12];
int[] y = new int[12];
int[] f = new int[12];



void setup() {
  int myX = 40;
  int myY = 40;
  
  size(500,500);
  
  
  for(int i = 0; i<12; i++){
    x[i] = myX;
    y[i] = myY;
    f[i] = i + 1;
    if (myX<390){
      myX += 100;
    }
    else if (myX>390){
      myX = 40;
      myY += 120;
    }
  }
  
  shuffle();
  
  for(int i = 1; i<12; i++){
    Globalcards[i] = new Cards (x[i], y[i], f[i]);
  }
}
  


void draw() {
  for(int i = 0; i<12; i++){
    Globalcards[i].display();

  }
  
  if (mousePressed){
    flipme();
  }
}

void flipme(){
  for(int i = 0; i<12; i++){
    if (mouseX > x[i] && mouseX<(x[i]+50) && mouseY>(y[i]+120)){
      Globalcards[i].displayFront();
    }
  }
}

void shuffle(){
  int z = 0;
  int u = 0;
  for (int i = 0; i<12; i++){
    u = int(random(0,12));
    z = f[i];
    f[i] = f[u];
    f[u] = z;
  }
}

Here is also the class Cards code i am running with it:

class Cards {
  PImage cardImage;
  int show = 0;
  int cardX = 0;
  int cardY = 0;
  int face = 0;
  String[] cardTheme = {"japan_style_elements_vector_graphics.png", "e7ca329b9c.png", "83c5bf9c8b.png", "83c5bfa28b.png", "605210998b.png", "83c5bfa48b.png" };
  
  Cards(int x, int y, int f){
    cardX = x;
    cardY = y;
    face = f;
  }
  void display(){
    cardImage = loadImage(cardTheme[show]);
    image(cardImage, cardX, cardY);
  }
  void displayFront(){
    show = face;
  }
}

Finally i have noticed that the program highlights this specific line when it fails to run:

Globalcards[i].display();

I would be grateful for any suggestions or help provided. Thanks.

you need to make ALL the cards you want use later FIRST

 for(int i = 1; i<12; i++){
    Globalcards[i] = new Cards (x[i], y[i], f[i]);
  }

not fit to

for(int i = 0; i<12; i++){
    Globalcards[i].display();
  }

so Globalcards[0] is missing

//__________________________
sorry, you edit the title at the same time

Error in Processing program,if you are a genius - come prove it :slight_smile:

i never think i am a genius like you

1 Like

Thanks for the reply, i don’t quite understand what you mean… could you please elaborate.

You never set Globalcards[0] to be anything!

You have this loop in setup():

  for(int i = 1; i<12; i++){
    Globalcards[i] = new Cards (x[i], y[i], f[i]);
  }

When you actually need this loop:

  for(int i = 0; i<12; i++){
    Globalcards[i] = new Cards (x[i], y[i], f[i]);
  }

Notice that the 1 becomes a 0. This minor change ensures that, in this loop, Globalcards[0] is also assigned to be something.

1 Like

Thanks so much, the the code now actually runs. though i realised one of my pictures is way too big… so its covering the whole screen :open_mouth:

HI @Smithy, can you repost your question please? it seems like there was an error when you edited your post

@Smithy has made 3 edits and I suspect he has deliberately destroyed the question. We used to get this a lot on the old forum but this one is nice, click on the pencil in his post and you can see the original question. :grin:

Perhaps a moderator can restore the original.

Why would someone deliberately destroy his question ? I don’t get it.

Maybe it is an assignment and OP doesn’t want his/her teacher to notice the post.

I’m happy you have geniuses here on the forum :slight_smile:

I have restored the post and locked it from future edits. @Smithy

Self-vandalism – especially after forum members have contributed public answers to a public question – is against forum policy. People answer for the good of all, and self-vandalism makes those answers impossible to understand. Self-vandalism also supports cheating on classwork, and this forum does not support cheating – advice should be incorporated into student work in a way that does not need to be a secret. If you want to remove a post with no substantial answers – or if you have a concern about privacy – please flag your post for help from a moderator.

6 Likes