Just starting to code, and code returns java.lang.reflect.InvocationTargetException error

Just starting to do an elemental version of 2048, as a practice on recreations on other popular games. After coding the function structure and screen appearance, this error comes out of nowhere upon startup.


The sketch path is not set.
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
	at processing.core.PApplet.runSketch(PApplet.java:10845)
	at processing.core.PApplet.main(PApplet.java:10613)
Caused by: java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at processing.core.PApplet.runSketch(PApplet.java:10839)
	... 1 more
Caused by: java.lang.RuntimeException: Files must be loaded inside setup() or after it has been called.
	at processing.core.PApplet.createInputRaw(PApplet.java:7126)
	at processing.core.PApplet.createInput(PApplet.java:7098)
	at processing.core.PApplet.loadBytes(PApplet.java:7397)
	at sketch_2048_Elemental.<init>(sketch_2048_Elemental.java:63)
	... 6 more
RuntimeException: java.lang.reflect.InvocationTargetException

Can anyone rewrite part of my code and tell me which part am I doing wrong? Any help would be greatly appreciated!

EDIT: I’ve been absolutely dumb and forgot to include the code itself, but here it is now:

  /* The Grid Must Be 11 by 11
     
     119 and 120 shall be named "Avogadron (Av)" and "Davium (Da)"
     
     Goal: Element 48 (Cadmium)
     Ultimatum: Element 120 (Davium)
  */

  PImage logoImg; //LOGO DECLARATION
  int scoreBG = #bbada0;
  int scoreText = #eee4da;
  int menuBG = #eb9a5b;
  int grayText = #756e66;
  
  PFont clearSans_Bold; //BOLD FONT DECLARATION
  PFont clearSans; //REGULAR FONT DECLARATION
  
  float logoSize = 146;
  
  float indentX = 39;
  float indentY = 22;
  float outdentX = indentX+logoSize+136+186+15+186;
  float outdentY = 914;
  
  float gridDif = (outdentX+indentX-29)/13+(outdentX+indentX-29)/13*20/(248)-1;
  float gridSize = (outdentX+indentX-23)/13;
  


void setup (){
  size(747,953);
  
  float xDPI = width/6.3125;
  float yDPI = height/8.4375;
  println("DPI: " + xDPI + ", " + yDPI +"\n(" + round(xDPI) + ", " + round(yDPI) + ")\n"); //print dpi values
  println("Dimensions of screen: " + width + ", " + height);
  
  textAlign(CENTER,CENTER);
  noStroke();
  
  clearSans_Bold = createFont("ClearSans-Bold.ttf",1000);
  clearSans = createFont("ClearSans-Regular.ttf",1000);

  logoImg = loadImage("CaCd Elemental 2048 logo.png");
}

  int[] score = int(loadBytes("score.dat"));
  int[] best = int(loadBytes("best.dat"));
  byte[] scoreByte = {byte(score[0])};
  byte[] bestByte = {byte(best[0])};
  
  
void checkHighscore(){
    if(score[0] >= best[0]){
    best[0] = score[0];
  }
}

int scoreIncrement(int incVal) {
    score[0] = score[0] + incVal;
    checkHighscore();
    return score[0];
  }


void moveAction(){
  if(keyCode == UP){
    score[0] = 0;
    best[0] = 0;
  }
  
  if(keyCode == DOWN){
    
  }
  
  if(keyCode == LEFT){
    
  }
  
  if(keyCode == RIGHT){
    
  }
  
  //This is where move codes go
    
}

void moveBackground(){
    moveAction();
    checkHighscore();
    
    saveBytes("score.dat",scoreByte);
    saveBytes("best.dat",bestByte);
}

void move(){
  moveBackground();
}

void keyPressed(){
  if(key == CODED){
    if(keyCode == UP){
      move();
    }
    
    if(keyCode == DOWN){
      move();
    }
    
    if(keyCode == LEFT){
      move();
    }
    
    if(keyCode == RIGHT){
      move();
    }
  }
}

void gameScreen(){
  background(#faf8ef);
  textFont(clearSans_Bold,13);
  
  //Draw Logo
  image(logoImg,39,22);
  
  //Draw score[0] Container
  fill(scoreBG);
  rect(indentX+logoSize+136,indentY,186,111,7);
  fill(scoreText);
  text("SCORE",indentX+logoSize+136+186/2,indentY+22);
  
  //Draw best[0] Container
  fill(scoreBG);
  rect(indentX+logoSize+(136+186)+15,indentY,186,111,7);
  fill(scoreText);
  text("BEST",indentX+logoSize+136+186+15+186/2,indentY+22);
  
  //Draw Menu Container
  fill(menuBG);
  rect(indentX+logoSize+136,indentY+111+9,186+15+186,30,7);
  fill(255);
  text("MENU",indentX+logoSize+136+(186+21+186)/2,indentY+111+9+5+30/2);
  
  //Draw Instructions
  fill(grayText);
  textAlign(LEFT,CENTER);
  text("Join the element tiles together and get to the Cadium(Cd) tile!",indentX,208);
  textFont(clearSans_Bold);
  textAlign(CENTER);
  fill(255);
  
  //Draw Grid
  fill(scoreBG);
  rect(indentX,238,outdentX-indentX+1-5,gridDif+11*gridSize-5,2);
  fill(scoreText);
  
  for(int k=0; k<11; k++){
  for(int i=0; i<11; i++){
  rect(indentX+11+i*gridDif,238+11+k*gridDif,gridSize,gridSize,2);
  }
  }
}
void draw(){
  gameScreen();
  if(keyPressed){
    keyPressed();
  }
  
}
1 Like

Move these lines inside setup():

int[] score = int(loadBytes("score.dat"));
int[] best = int(loadBytes("best.dat"));
byte[] scoreByte = {byte(score[0])};
byte[] bestByte = {byte(best[0])};

Why? loadBytes() is defined as a both a member method and a static method. Your code references the member method which cannot be called outside setup() because sketchPath has not yet been set.

3 Likes

Then it returns this: score cannot be resolved to a variable, I spent quite some time trying to find out what’s wrong with the types but I can’t seem to find the error. Here’s the code:

  /* The Grid Must Be 11 by 11
     
     119 and 120 shall be named "Avogadron (Av)" and "Davium (Da)"
     
     Goal: Element 48 (Cadmium)
     Ultimatum: Element 120 (Davium)
  */

  PImage logoImg; //LOGO DECLARATION
  int scoreBG = #bbada0;
  int scoreText = #eee4da;
  int menuBG = #eb9a5b;
  int grayText = #756e66;
  
  PFont clearSans_Bold; //BOLD FONT DECLARATION
  PFont clearSans; //REGULAR FONT DECLARATION
  
  float logoSize = 146;
  
  float indentX = 39;
  float indentY = 22;
  float outdentX = indentX+logoSize+136+186+15+186;
  float outdentY = 914;
  
  float gridDif = (outdentX+indentX-29)/13+(outdentX+indentX-29)/13*20/(248)-1;
  float gridSize = (outdentX+indentX-23)/13;
  


void setup (){
  size(747,953);
  
  float xDPI = width/6.3125;
  float yDPI = height/8.4375;
  println("DPI: " + xDPI + ", " + yDPI +"\n(" + round(xDPI) + ", " + round(yDPI) + ")\n"); //print dpi values
  println("Dimensions of screen: " + width + ", " + height);
  
  textAlign(CENTER,CENTER);
  noStroke();
  
  clearSans_Bold = createFont("ClearSans-Bold.ttf",1000);
  clearSans = createFont("ClearSans-Regular.ttf",1000);

  logoImg = loadImage("CaCd Elemental 2048 logo.png");
  
  int[] score = int(loadBytes("score.dat"));
  int[] best = int(loadBytes("best.dat"));
  byte[] scoreByte = {byte(score[0])};
  byte[] bestByte = {byte(best[0])};
}
  
void checkHighscore(){
    if(score[0] >= best[0]){
    best[0] = score[0];
  }
}

int scoreIncrement(int incVal) {
    score[0] = score[0] + incVal;
    checkHighscore();
    return score[0];
  }


void moveAction(){
  if(keyCode == UP){
    score[0] = 0;
    best[0] = 0;
  }
  
  if(keyCode == DOWN){
    
  }
  
  if(keyCode == LEFT){
    
  }
  
  if(keyCode == RIGHT){
    
  }
  
  //This is where move codes go
    
}

void moveBackground(){
    moveAction();
    checkHighscore();
    
    saveBytes("score.dat",scoreByte);
    saveBytes("best.dat",bestByte);
}

void move(){
  moveBackground();
}

void keyPressed(){
  if(key == CODED){
    if(keyCode == UP){
      move();
    }
    
    if(keyCode == DOWN){
      move();
    }
    
    if(keyCode == LEFT){
      move();
    }
    
    if(keyCode == RIGHT){
      move();
    }
  }
}

void gameScreen(){
  background(#faf8ef);
  textFont(clearSans_Bold,13);
  
  //Draw Logo
  image(logoImg,39,22);
  
  //Draw score[0] Container
  fill(scoreBG);
  rect(indentX+logoSize+136,indentY,186,111,7);
  fill(scoreText);
  text("SCORE",indentX+logoSize+136+186/2,indentY+22);
  
  //Draw best[0] Container
  fill(scoreBG);
  rect(indentX+logoSize+(136+186)+15,indentY,186,111,7);
  fill(scoreText);
  text("BEST",indentX+logoSize+136+186+15+186/2,indentY+22);
  
  //Draw Menu Container
  fill(menuBG);
  rect(indentX+logoSize+136,indentY+111+9,186+15+186,30,7);
  fill(255);
  text("MENU",indentX+logoSize+136+(186+21+186)/2,indentY+111+9+5+30/2);
  
  //Draw Instructions
  fill(grayText);
  textAlign(LEFT,CENTER);
  text("Join the element tiles together and get to the Cadium(Cd) tile!",indentX,208);
  textFont(clearSans_Bold);
  textAlign(CENTER);
  fill(255);
  
  //Draw Grid
  fill(scoreBG);
  rect(indentX,238,outdentX-indentX+1-5,gridDif+11*gridSize-5,2);
  fill(scoreText);
  
  for(int k=0; k<11; k++){
  for(int i=0; i<11; i++){
  rect(indentX+11+i*gridDif,238+11+k*gridDif,gridSize,gridSize,2);
  }
  }
}
void draw(){
  gameScreen();
  if(keyPressed){
    keyPressed();
  }
  
}

According to the debugger, line 53 (

    if(score[0] >= best[0]){
    best[0] = score[0];
  }
}

)
is the line causing that problem.

1 Like

sorry, possibly the tip from @micycle was correct, but not complete,

if you want to use that values further, ( outside setup )
you might need to declare the variables as global,
but still fill them in setup.

int[] score;

void setup() {
  score = int(loadBytes("score.dat"));
}

void draw() [
 println(score[0]);
}
3 Likes

Thank you so much! I now understand the importance of setting up the variable in the right place for the right type!

1 Like