0

The class is called Exposicion and has a String and an INT value, so I used it as an array to grab some input from the user.

class Exposicion {
public String nombreExpo;
public int duracionExpo;

Exposicion(String nombreExpo, int duracionExpo) {
    this.nombreExpo = nombreExpo;
    this.duracionExpo = duracionExpo;
}
}

With the Function SortExpo I plan to copy only the values of the array as long as the INT values don't add up to 180, but java flags an error when doing:

      arrExpoT[posHor].nombreExpo = arrExpoS[k].nombreExpo;

This is the whole function

void SortExpo(Exposicion[] arrExpoS,int posicion,Exposicion[] arrExpoT){

    int poshor=0;
    int total=0;
    for (int k = 0; k < posicion; k++) {
        if ( total < 180 || arrExpoS[poshor].nombreExpo != "TOMADO123") {
            arrExpoT[poshor].nombreExpo = arrExpoS[k].nombreExpo;
            arrExpoT[poshor].duracionExpo = arrExpoS[k].duracionExpo;
            arrExpoS[poshor].nombreExpo = "TOMADO123";
            total = total + arrExpoS[k].duracionExpo;
            poshor++;
        } else {
            k = posicion;
        }
    }
}

Error

I've added the .java file in this link

Also Main.java if this helps

csanchez84
  • 21
  • 4
  • 1
    Hey, First: you should add the code that initializes the arrays and calls SortExpo Second: you should print the error (although I'm guessing it's index out of bounds exception or null pointer exception) – Roy Shahaf Aug 21 '18 at 22:05
  • Also, in the first line of SortExpo poshor has a small h, later you use posHor with a capital h, is that also the case in your actual code? – Roy Shahaf Aug 21 '18 at 22:07
  • I'll correct the syntax issues. The error in null pointer exception, yes. – csanchez84 Aug 21 '18 at 22:12
  • Alright, please note that: a. your code seems like it doesn't do what you intend it to do (although you haven't cleared what that is) and b. You still don't have code that shows how you initialize the arrays but it's likely you are not initializing them well – Roy Shahaf Aug 21 '18 at 22:17
  • I've added a link with the .java file so you can check the whole text. Thank you. – csanchez84 Aug 21 '18 at 22:23
  • Warning: you are comparing `String`s with the `!=` operator. [Don't do that](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). – MC Emperor Aug 22 '18 at 06:31

1 Answers1

0

You are getting a NullPointerException because "expo1" and "sala1" variables are both null. You have to pass a reference to an object on both variables. Something like this:

class SalaExpo(){
   Exposicion[] expo1=new Exposicion[100];
}

public class ConsoleMenu {

   private SalaExpo sala1;

   void execute(){
      sala1 = new SalaExpo();
   }
}

Also you should poblate the sala1.expo1 array, like this (don't know if this is what you are intending but you should do this in order not to get a NullPointerException) :

void GuardarExpo(Exposicion[] arrExpoG,int posicion,Exposicion[] arrSala) {

     /*
        Bunch
         of
        code

     */

     arrExpoG[posicion] = new Exposicion(inputNombre,inputDuracion);
     arrSala[posicion]=arrExpoG[posicion];
}

Finally, you should use the variable "posicion" instead of "sala1.expo1.length" to pass as argument to the "imprimirExpo" method, since the array "sala1.expo1" has a length of 100, that means a lot of null elements since you are not poblating it all:

ImprimirExpo(sala1.expo1,posicion);

instead of:

ImprimirExpo(sala1.expo1,sala1.expo1.length);
LET1S3N
  • 28
  • 7