ayuda mi valor no se muestra porque es null
Publicado por Danni (12 intervenciones) el 04/12/2021 19:17:51
tengo un problema a la hora que quiero mostrar un dato con un método que esta en otra clase me sale este error
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "proyecto.Locker.costoLocker()" because the return value of "proyecto.Gimnasio.obtenerLocker(int)" is null
at proyecto.Menu.registrarRentaLocker(Menu.java:126)
at proyecto.Menu.desplegarMenu(Menu.java:41)
at proyecto.Principal.main(Principal.java:10)
me podrían decir porque ocurre eso?
se que hay muchos errores pero me podrían decir porque ocurre eso?
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "proyecto.Locker.costoLocker()" because the return value of "proyecto.Gimnasio.obtenerLocker(int)" is null
at proyecto.Menu.registrarRentaLocker(Menu.java:126)
at proyecto.Menu.desplegarMenu(Menu.java:41)
at proyecto.Principal.main(Principal.java:10)
me podrían decir porque ocurre eso?
se que hay muchos errores pero me podrían decir porque ocurre eso?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package proyecto;
import java.util.Scanner;
public class Locker {
private String nombreCliente;
private String tamanio;
private String tipoContratacion;
public Locker(){
}
public Locker(String nombreCliente, String tamanio,String tipoContratacion){
this.nombreCliente=nombreCliente;
if(tamanio.equalsIgnoreCase("chico") || tamanio.equalsIgnoreCase("mediano") || tamanio.equalsIgnoreCase("grande"))
this.tamanio=tamanio;
if(tipoContratacion.equalsIgnoreCase("Trimestre")|| tipoContratacion.equalsIgnoreCase("Tri")|| tipoContratacion.equalsIgnoreCase("trimestre") || tipoContratacion.equalsIgnoreCase("Mensual") || tipoContratacion.equalsIgnoreCase("Men") || tipoContratacion.equalsIgnoreCase("mensual")){
this.tipoContratacion=tipoContratacion;
}
}
public String getNombre(){
return nombreCliente;
}
public void setNombre(){
this.nombreCliente = nombreCliente;
}
public String getTamanio(){
return tamanio;
}
public void setTamanio(String tamanio){
if(tamanio.equalsIgnoreCase("chico") || tamanio.equalsIgnoreCase("mediano") || tamanio.equalsIgnoreCase("grande"))
this.tamanio=tamanio;
}
public void setTipoContratacion(String tipoContratacion){
if(tipoContratacion.equalsIgnoreCase("Trimestre")|| tipoContratacion.equalsIgnoreCase("Tri") || tipoContratacion.equalsIgnoreCase("Mensual") || tipoContratacion.equalsIgnoreCase("Men") || tipoContratacion.equalsIgnoreCase("mensual"))
this.tipoContratacion=tipoContratacion;
}
public String getTipoContratacion(){
return tipoContratacion;
}
public float costoLocker(){
float costo=0f;
if (tipoContratacion.equalsIgnoreCase("Trimestre") || tipoContratacion.equalsIgnoreCase("trimestre") || tipoContratacion.equalsIgnoreCase("Tri")) {
if (tamanio.equalsIgnoreCase("Chico") || tamanio.equalsIgnoreCase("chico")) {
costo = 120f;
} else if (tamanio.equalsIgnoreCase("Mediano") || tamanio.equalsIgnoreCase("mediano")) {
costo = 160f;
} else if (tamanio.equalsIgnoreCase("Grande") || tamanio.equalsIgnoreCase("grande")) {
costo = 195f;
}
}else if (tipoContratacion.equalsIgnoreCase("Mensual") || tipoContratacion.equalsIgnoreCase("Men") || tipoContratacion.equalsIgnoreCase("mensual") ){
if (tamanio.equalsIgnoreCase("Chico")|| tamanio.equalsIgnoreCase("chico")) {
costo = 50f;
} else if (tamanio.equalsIgnoreCase("Mediano") || tamanio.equalsIgnoreCase("mediano")) {
costo = 60f;
} else if (tamanio.equalsIgnoreCase("Grande") || tamanio.equalsIgnoreCase("grande")) {
costo = 75f;
}
}else{
costo=0f;
}
return costo;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
public class Gimnasio {
private String nombreGimnasio;
private Locker lockers[];
private Locker locker;
public Gimnasio(String nombreGimnasio){
this.nombreGimnasio=nombreGimnasio;
lockers = new Locker[30];
}
public Locker obtenerLocker(int posicion) {
if (posicion>1 &&posicion<=lockers.length){
if (lockers[posicion-1]!=null){
return lockers[posicion-1];
}else{
return null;
}
}else{
return null;
}
}
public Locker obtenerLocker(String nombre) {
for (Locker i : lockers) {
if (i.getNombre().equalsIgnoreCase(nombre)) {
return i;
}
}
return null;
}
public int getSiguienteDisponible(){
for (int i = 0; i < lockers.length; i++) {
if(i>0 && i<29){
return i+1;
}
}
return -1;
}
public void setLocker(Locker locker){
for (int i = 0; i < lockers.length; i++) {
this.locker=locker;
getSiguienteDisponible();
}
}
public void eliminarLocker(int posicion){
lockers[posicion-1]=null;
}
public void mostrarLockersOcupados(){
int contador=0;
for (int i = 0; i < lockers.length; i++) {
String ocupado;
if(lockers[i]!=null){
ocupado="XXX";
}
else{
ocupado="XXX";
}
while (i>=0 && i<30){
System.out.printf("|%10s %10s |", "", i+1+" "+ocupado);
System.out.println("");
i++;
}
}
System.out.println("");
}
public void mostrarOcupacionTipoContrato(){
for (int i = 0; i < lockers.length; i++) {
String contratacionLista;
while (i>0 && i<10){
System.out.printf("|%10s |", "", i+1);
System.out.println("");
i++;
if(lockers[i]==null){
contratacionLista ="XXX";
}
else if(lockers[i].getTipoContratacion().equalsIgnoreCase("Trimestal")){
contratacionLista="Trimestral";
}
else if(lockers[i].getTipoContratacion().equalsIgnoreCase("Mensual")){
contratacionLista="Mensual";
}
}
System.out.println("");
}
public void mostrarListadoLockers() {
int lockerVacios = 0, lockerOcupados = 0;
String claseStr, destinoStr;
System.out.println("Transportes Intergalácticos de Cajeme, S.A.");
System.out.println("Listado ocupacion lockers");
System.out.printf("%20s %20s %20s %20s %20s \n", "Locker", "Nombre del cliente", "Tipo de contratacion", "Tamaño renta", "Costo renta");
for (int i = 0; i < lockers.length; i++) {
System.out.printf("%-20s %-20s $%-20s $%-15.2f\n", lockers[i].getNombre(), lockers[i].getTipoContratacion(), lockers[i].getTamanio(), lockers[i].costoLocker());
if (lockers[i] == null) {
lockerVacios = lockerVacios + 1;
System.out.printf("%20s %20s %20s %20s %20s %20s %20s \n", "***Disponible", "", "", "", "", "", "");
} else {
lockerOcupados = lockerOcupados + 1;
System.out.printf("%20s %20s %20s %20s \n", lockers[i].getNombre(), lockers[i].getTipoContratacion(), lockers[i].getTamanio(), lockers[i].costoLocker());
}
}
System.out.println("");
System.out.println("Asientos vacios: " + lockerVacios);
System.out.println("Asientos ocupados " + lockerOcupados);
System.out.println("Total de asientos: " + (lockerVacios + lockerOcupados));
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package proyecto;
import java.util.Scanner;
public class Menu {
private Gimnasio gimnasio;
private Locker lokers;
public Menu(){
Gimnasio gimnasio = new Gimnasio("Potros ITSON");
this.gimnasio=gimnasio;
}
public void desplegarMenu(){
Scanner tec = new Scanner (System.in);
char opcion;
do{
System.out.println("-------------------------------------------");
System.out.println(gimnasio);
System.out.println("-------------------------------------------");
System.out.println(" Menu Principal");
System.out.println("-------------------------------------------");
System.out.println("1._ Reistrar renta de locker");
System.out.println("2._ Eliminar renta de locker");
System.out.println("3._ Mostrar informacion del locker por cliente");
System.out.println("4._ Editar cliente registrado");
System.out.println("5._ Mostrar mapa de ocupacion por tipo de contratacion");
System.out.println("6._ Mostrar listado de lockers");
System.out.println("7._ Terminar operacion");
System.out.println("-------------------------------------------");
System.out.println("Teclee su opcion: ");
opcion=tec.nextLine().charAt(0);
System.out.println("");
switch(opcion){
case'1':
registrarRentaLocker( );
break;
case'2':
eliminarRentaLocker();
break;
case'3':
mostrarLockerCliente();
break;
case'4':
editarClienteRegistrado();
break;
case'5':
mostrarMapaOcupacion();
break;
case'6':
listadoLockers();
break;
case'7':
break;
default:
System.out.println("*** opcion no valida, ingrese una del 1 al 7 ");
System.out.println("Presione Enter Para Continuar ");
tec.nextLine();
System.out.println("INGRESE LA OPCION DESEADA: ");
opcion=tec.nextLine().charAt(0);
break;
}
}while(opcion>='1' && opcion<= '7');
}
public void registrarRentaLocker() {
Scanner tec = new Scanner(System.in);
System.out.println(" ***Registro de renta ");
char opcion = 'S';
while (opcion == 'S' || opcion == 's') {
int posicion = 0;
//REGRESA AL MENU PRINCIPAL
if (posicion == -1) {
break;
}
//OBTIENE EL ESTADO DE LOS ASIENTOS EJECUTIVOS
if (posicion >= 0 && posicion < 30) {
if (gimnasio.obtenerLocker(posicion) != null) {
System.out.println("*** *** Número Del locker Disponible");
System.out.println("Presione Enter Para Continuar");
tec.nextLine();
tec.nextLine();
System.out.println("Desea Continuar Con El Registro De Renta? (S/N)");
opcion = tec.next().charAt(0);
while (opcion != 'S' && opcion != 'N' && opcion != 's' && opcion != 'n') {
System.out.println("*** Error: Respuesta Inválida.");
System.out.println("Presione Enter Para Continuar");
tec.nextLine();
tec.nextLine();
System.out.println("Desea Continuar Con El Registro DE Reservaciones? (S/N)");
opcion = tec.next().charAt(0);
}
if (opcion == 'S' || opcion == 's') {
} else if (opcion == 'N' || opcion == 'n') {
break;
}
}
// SE SOLICITAN LOS DATOS DEL cliente
if (opcion == 'S' || opcion == 's') {
System.out.println("Ingrese El Nombre Del cliente: ");
String nombreCliente = tec.next();
System.out.println("Ingrese El tamaño del locker: ");
String tamanio = tec.next();
System.out.println("periodo de renta: ");
String tipoContratacion = tec.next();
float costo=0f;
//SE MUESTRAN LOS DATOS DEL CLIENTE
gimnasio.getSiguienteDisponible();
System.out.println(" **********Confirmacion ");
System.out.println("Cliente: " + nombreCliente);
System.out.println("Tamaño: " + tamanio);
System.out.println("Contratacion: " + tipoContratacion);
System.out.println("Costo: " + gimnasio.obtenerLocker(posicion).costoLocker());
System.out.println("Locker asignado: " + gimnasio.getSiguienteDisponible());
System.out.println("");
// SE CONFIRMA LA RENTA
System.out.println("confirmar el registro? (S/N): ");
char confirmar = tec.next().charAt(0);
while (confirmar != 'S' && confirmar != 'N' && confirmar != 's' && confirmar != 'n') {
System.out.println("*** Error: Respuesta inválida.");
System.out.println("Presione Enter Para Continuar");
tec.nextLine();
tec.nextLine();
System.out.println("Desea Confirmar El Registro De Reservaciones (S/N).");
confirmar = tec.next().charAt(0);
}
//SE REALIZA LA RRENTA
if (confirmar == 'S' || confirmar == 's') {
// SE CREA AL CLIENTE
Locker locker = new Locker(nombreCliente, tamanio, tipoContratacion);
gimnasio.setLocker(locker);
System.out.println("Reservacion Realizada.");
// SE PREGUNTA PARA CONTINUAR EN EL METODO RENTA O REGRESAR A EL MENU PRINCIPAL
System.out.println("Desea Continuar Con El Registro De Reservaciones, (S/N).");
opcion = tec.next().charAt(0);
while (opcion != 'S' && opcion != 'N' && opcion != 's' && opcion != 'n') {
System.out.println("*** Error: Respuesta Inválida.");
System.out.println("Presione Enter Para Continuar");
tec.nextLine();
tec.nextLine();
System.out.println("Desea Continuar Con El Registro De Reservaciones, (S/N).");
opcion = tec.next().charAt(0);
}
if (opcion == 'S' || opcion == 's') {
} else if (opcion == 'N' || opcion == 'n') {
break;
}
} else if (confirmar == 'N' || confirmar == 'n') {
System.out.println("Reservacion No Realizada.");
// SE PREGUNTA PARA CONTINUAR EN EL METODO RENTA O REGRESAR A EL MENU PRINCIPAL
System.out.println("Desea Continuar Con El Registro De Reservaciones, (S/N).");
opcion = tec.next().charAt(0);
while (opcion != 'S' && opcion != 'N' && opcion != 's' && opcion != 'n') {
System.out.println("*** Error: Respuesta Inválida.");
System.out.println("Presione Enter Para Continuar");
tec.nextLine();
tec.nextLine();
System.out.println("Desea Continuar Con El Registro De Reservaciones, (S/N).");
opcion = tec.next().charAt(0);
}
if (opcion == 'S' || opcion == 's') {
} else if (opcion == 'N' || opcion == 'n') {
break;
}
}
}
}
}
}
Valora esta pregunta


0