Some kind of error! Help me pls!

Hi, I don’t know why this happend and i would appreciate if someone can help me. Thats my code:

import static javax.swing.JOptionPane.*;

void settings() {
  size((displayWidth/4)*2, (displayHeight/3)*2);
}

void setup() {
  background (0);

  //imagenMapa = loadImage("Imagenes/Mapa");
  imagenPersonaje = loadImage ("Imagenes/Sprite");
}  

void draw () {
  background (0);

  Protagonista.mostrar();
}

void keyPressed() {
  final int k = keyCode;

  if (k == 'w' | k == 'W' | k == UP ) {
    Moverse = true;
    Arriba = true;
    Abajo = false;
    Derecha = false;
    Izquierda = false;
  }
  if (k == 's' | k == 'S' | k == DOWN ) {
    Moverse = true;
    Arriba = false;
    Abajo = true;
    Derecha = false;
    Izquierda = false;
  }
  if (k == 'a' | k == 'A' | k == LEFT ) {
    Moverse = true;
    Arriba = false;
    Abajo = false;
    Derecha = false;
    Izquierda = true;
  }
  if (k == 'd' | k == 'D' | k == RIGHT ) {
    Moverse = true;
    Arriba = false;
    Abajo = false;
    Derecha = true;
    Izquierda = false;
  }
}

void keyReleased() {
  if (Arriba == true) {
    Protagonista.RecorteX = Protagonista.RecorteIY + Ventana.Anchura;
    Protagonista.RecorteY = Protagonista.RecorteIY + (3*Ventana.Altura);
  }
  if (Abajo == true) {
    Protagonista.RecorteX = Protagonista.RecorteIX + Ventana.Anchura;
    Protagonista.RecorteY = Protagonista.RecorteIY;
  }
  if (Izquierda == true) {
    Protagonista.RecorteX = Protagonista.RecorteIX + Ventana.Anchura;
    Protagonista.RecorteY = Protagonista.RecorteIY + Ventana.Altura;
  }
  if (Derecha == true) {
    Protagonista.RecorteX = Protagonista.RecorteIX + Ventana.Anchura;
    Protagonista.RecorteY = Protagonista.RecorteIY + (2*Ventana.Altura);
  }
  Moverse = false;
  Arriba = false;
  Abajo = false;
  Derecha = false;
  Izquierda = false;
}
class Pantalla{
  int [] PosicionX = new int [16];
  int [] PosicionY = new int [12];
  int Anchura;
  int Altura;
  
  Pantalla (){
    for (int j=0; j<16; j++){
      this.PosicionY [j] = (height/12)*j;
       for (int i=0; i<12; i++){
        this.PosicionX [i] = (width/16)*i;}}
    this.Anchura = width/16;
    this.Altura = height/12;
  }
}

class CharaMove{
  int PosicionX;
  int PosicionY;
  int RecorteIX;
  int RecorteIY;
  int RecorteX;
  int RecorteY;
  
  CharaMove (int x, int y, int rix, int riy){
    this.PosicionX = x;
    this.PosicionY = y;
    this.RecorteIX = rix;
    this.RecorteIY = riy;
    this.RecorteX = RecorteIX + Ventana.Anchura;
    this.RecorteY = RecorteIY;
  } 
  
  void mostrar(){
    mover();
    copy(imagenPersonaje, this.RecorteX, this.RecorteY, 32, 32, this.PosicionX, this.PosicionY, Ventana.Anchura, Ventana.Altura);
  }
  
  void mover(){
    if (Moverse == true){
      if(Arriba == true){
        this.PosicionY = this.PosicionY - Ventana.Altura;
        this.RecorteY = RecorteIY + (3*Ventana.Altura);
        if (Pie == false){
          this.RecorteX = this.RecorteIX;
          Pie = true;
        }
        if (Pie == true){
          this.RecorteX = this.RecorteIX + (2*Ventana.Anchura);
        }
      }
      if(Abajo == true){
        this.PosicionY = this.PosicionY + Ventana.Altura;
        this.RecorteY = RecorteIY;
        if (Pie == false){
          this.RecorteX = this.RecorteIX;
          Pie = true;
        }
        if (Pie == true){
          this.RecorteX = this.RecorteIX + (2*Ventana.Anchura);
        }
      }
      if(Izquierda == true){
        this.PosicionX = this.PosicionX - Ventana.Anchura;
        this.RecorteY = RecorteIY + Ventana.Altura;
        if (Pie == false){
          this.RecorteX = this.RecorteIX;
          Pie = true;
        }
        if (Pie == true){
          this.RecorteX = this.RecorteIX + (2*Ventana.Anchura);
        }
      }
      if(Derecha == true){
        this.PosicionX = this.PosicionX + Ventana.Anchura;
        this.RecorteY = RecorteIY + (2*Ventana.Altura);
        if (Pie == false){
          this.RecorteX = this.RecorteIX;
          Pie = true;
        }
        if (Pie == true){
          this.RecorteX = this.RecorteIX + (2*Ventana.Anchura);
        }
      }
    }
  }
}
//Protagonista

CharaMove Protagonista;{
  this.Protagonista.PosicionX = 0;
  this.Protagonista.PosicionY = 0;
  this.Protagonista.RecorteIX = 96;
  this.Protagonista.RecorteIY = 0;
}
PImage imagenPersonaje;
//PImage imagenMapa;

float [] pantallaX= new float [16];
float [] pantallaY= new float [12];

boolean Moverse = false, Arriba = false, Abajo = false, Izquierda = false, Derecha = false, Pie= false;
Pantalla Ventana;

And I get this:





java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
	at processing.core.PApplet.runSketch(PApplet.java:10741)
	at processing.core.PApplet.main(PApplet.java:10511)
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:10735)
	... 1 more
Caused by: java.lang.NullPointerException
	at BaseJuegoRol_Setup.<init>(BaseJuegoRol_Setup.java:212)
	... 6 more```
1 Like

if you use
// to disable lines
and

/*
parts of the
code
*/
you find out pretty soon
whats the problem.
disable this,

//Protagonista
/*
CharaMove Protagonista;{
  this.Protagonista.PosicionX = 0;
  this.Protagonista.PosicionY = 0;
  this.Protagonista.RecorteIX = 96;
  this.Protagonista.RecorteIY = 0;
}
*/

and at the beginning use

CharaMove protagonista;

void settings() {
  size((displayWidth/4)*2, (displayHeight/3)*2);
}

void setup() {
  background (0);
  protagonista = new CharaMove(0,0,96,0);

//..
}  

and you got to your next problem to solve…

1 Like

I guess this part of the code causes the problem:

//Protagonista
CharaMove Protagonista;{
  this.Protagonista.PosicionX = 0;
  this.Protagonista.PosicionY = 0;
  this.Protagonista.RecorteIX = 96;
  this.Protagonista.RecorteIY = 0;
}

And I guess you are trying to make a new CharaMove pointer, and set some initial values. However, the syntax is weird. Notice the ; - it usually means that “this action has ended” and that the next thing is a new action completely unrelated to the last one. As a result, that block of code above gets interpreted as:

CharaMove Protagonista;

{
  this.Protagonista.PosicionX = 0;
  this.Protagonista.PosicionY = 0;
  this.Protagonista.RecorteIX = 96;
  this.Protagonista.RecorteIY = 0;
}

And, the part in {...} is executed directly onto the sketch class itself, not on Protagonista. So this points to the sketch class itself, and this.Protagonista points to just Protagonista, so, I think that this and {...} brackets are redundant here. So, it turns into:

CharaMove Protagonista;

Protagonista.PosicionX = 0;
Protagonista.PosicionY = 0;
Protagonista.RecorteIX = 96;
Protagonista.RecorteIY = 0;

The next problem part of the problem is that you don’t initialize Protagonista - you only create a pointer to an object of such class, which doesn’t have any data in it. As a result, it points to nothing, i.e. null, which is why at the end of your error log there’s a java.lang.NullPointerException.

You need to do CharaMove Protagonista = new CharaMove(...);, where ... is whatever the constructor of that class needs, to actually make a new CharaMove.

Your constructor is:

  CharaMove (int x, int y, int rix, int riy){
    this.PosicionX = x;
    this.PosicionY = y;
    this.RecorteIX = rix;
    this.RecorteIY = riy;
    this.RecorteX = RecorteIX + Ventana.Anchura;
    this.RecorteY = RecorteIY;
  } 

Which takes 4 int values and sets the according ones there, which you were trying to do with that {...} block above right after defining CharaMove Protagonista;. It’s better to use the constructor for this in your case, so you get: CharaMove Protagonista = new CharaMove(0,0,96,0);.

And even then, it’s highly recommended to set your objects and stuff inside of functions like void setup(), so you don’t create it too early. So, as a final result of all of this, you should have something like this:

CharaMove Protagonista; //Create a global pointer to that object, which is set to null

void setup(){
...
Protagonista = new CharaMove(0,0,96,0); //Create a new object and set that pointer to it, while also setting needed variables
...
}
2 Likes