Ayuda con error, al usar arraylist
Publicado por Andres (5 intervenciones) el 19/11/2018 02:32:40
Tengo problemas al usar la opción 2 del switch, ya que no se muestran los 2 equipos, solo se muestra el segundo. Tengo entendido que es porque el arraylist guarda el ultimo valor, pero no entiendo porque, seria de gran ayuda si alguien me podría indicar que linea cambiar o mejorar.
Me manda el siguiente error en consola:
La linea 135 es:
Dejo todo el código esperando respuestas, gracias de antemano.
AppPartidoBaby
Entidad.PartidoBaby
Entidad.Team
Entidad.Player
Me manda el siguiente error en consola:
1
2
3
4
Exception in thread "main" java.lang.NullPointerException
at AppPartidoBaby.main(AppPartidoBaby.java:135)
C:\Users\andres\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 27 seconds)
La linea 135 es:
1
nomj=objPartido.getListaTeam().get(i).getArrPlayer()[j].getNombre();
Dejo todo el código esperando respuestas, gracias de antemano.
AppPartidoBaby
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
import java.util.Scanner;
import Entidad.*;
import java.util.ArrayList;
import java.util.List;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class AppPartidoBaby {
public static void main(String[] args) throws IOException {
Scanner sc=new Scanner(System.in);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PartidoBaby objPartido=new PartidoBaby();
Player [] jug=new Player [6];
int opc,s,e,p,t;
String nomj="";
String apej="";
String nomt="";
float tamj=0;
do {
do {
menu();
opc=sc.nextInt();
} while (!(opc>0 && opc<6));
switch(opc)
{
case 1:
System.out.println("\n");
System.out.println("Ingrese Datos Del Partido: ");
System.out.println("----------------------------");
sc.nextLine();
System.out.println("Ingrese la fecha: ");
System.out.print("Dia del partido: ");
byte d=sc.nextByte();
System.out.print("Mes del partido(Ingrese el numero del mes): ");
byte m=sc.nextByte();
System.out.print("Año del partido(Agregar los dos ultimos digitos de 2000, por ejemplo: ingresa 10 se guarda 2010): ");
byte a=sc.nextByte();
objPartido.setFecha(d,m,a);
objPartido.getFecha();
sc.nextLine();
System.out.println("Ingrese los datos del Partido");
System.out.print("Ingrese nombre del partido: ");
objPartido.setNombre(sc.nextLine());
System.out.println("Ingrese los equipos que participaran en el partido: ");
//Primer equipo
System.out.println("Datos del primer equipo");
System.out.print("Ingrese el nombre del equipo: ");
objPartido.getObjTeam().setNombre(sc.nextLine());
System.out.print("Ingrese la ciudad del equipo: ");
objPartido.getObjTeam().setCiudad(sc.nextLine());
System.out.println("*****************************");
//Jugadores del primer equipo
for(int i=0;i<2;i++){
System.out.println("Ingrese el jugador "+(i+1)+" del equipo");
System.out.println("------------------------------------");
System.out.print("Ingrese el nombre del jugador: ");
nomj=br.readLine();
objPartido.getObjTeam().getObjPlayer().setNombre(nomj);
System.out.print("Ingrese el apellido del jugador: ");
apej=br.readLine();
objPartido.getObjTeam().getObjPlayer().setApellido(apej);
System.out.print("Ingrese el tamaño del jugador: ");
tamj=sc.nextByte();
objPartido.getObjTeam().getObjPlayer().setTamanio(tamj);
System.out.println("------------------------------------");
jug[i]=new Player(nomj,apej,tamj);
objPartido.getObjTeam().setArrPlayer(jug);
objPartido.getListaTeam().add(objPartido.getObjTeam());
}
sc.nextLine();
//Segundo equipo
System.out.println("Datos del segundo equipo");
System.out.print("Ingrese el nombre del equipo: ");
objPartido.getObjTeam().setNombre(sc.nextLine());
System.out.print("Ingrese la ciudad del equipo: ");
objPartido.getObjTeam().setCiudad(sc.nextLine());
System.out.println("*****************************");
//Jugadores del Segundo equipo
for(int i=0;i<2;i++){
System.out.println("Ingrese el jugador "+(i+1)+" del equipo");
System.out.println("------------------------------------");
System.out.print("Ingrese el nombre del jugador: ");
nomj=br.readLine();
objPartido.getObjTeam().getObjPlayer().setNombre(nomj);
System.out.print("Ingrese el apellido del jugador: ");
apej=br.readLine();
objPartido.getObjTeam().getObjPlayer().setApellido(apej);
System.out.print("Ingrese el tamaño del jugador: ");
tamj=sc.nextByte();
objPartido.getObjTeam().getObjPlayer().setTamanio(tamj);
System.out.println("------------------------------------");
jug[i]=new Player(nomj,apej,tamj);
objPartido.getObjTeam().setArrPlayer(jug);
objPartido.getListaTeam().add(objPartido.getObjTeam());
}
sc.nextLine();
break;
case 2:
System.out.println("Mostramos informacion:");
for (int i=0;i<3;i++){
nomt=objPartido.getListaTeam().get(i).getNombre();
System.out.println("Nombre equipo: "+nomt);
for(int j=0;j<objPartido.getListaTeam().size();j++){
nomj=objPartido.getListaTeam().get(i).getArrPlayer()[j].getNombre();
apej=objPartido.getListaTeam().get(i).getArrPlayer()[j].getApellido();
tamj=objPartido.getListaTeam().get(i).getArrPlayer()[j].getTamanio();
System.out.println("Jugador "+(j+1)+": "+nomj+" "+apej);
}
}
break;
case 3:
break;
case 4:
break;
case 5:
System.out.println("¿Seguro que quiere salir del programa?(1 para salir, 2 para seguir)");
System.out.print("Ingrese opcion: ");
s=sc.nextInt();
if(s==1){
opc=5;
}else{
opc=6;
}
}
} while (opc !=5);
}
}
Entidad.PartidoBaby
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
package Entidad;
import java.util.ArrayList;
public class PartidoBaby {
private String _nombre;
private byte _dia;
private byte _mes;
private byte _anio;
private String mes;
private ArrayList<Team> _listaTeam;
private Team objTeam;
public PartidoBaby() {
_listaTeam=new ArrayList<>();
objTeam=new Team();
}
public String getNombre() {
return _nombre;
}
public void setNombre(String _nombre) {
this._nombre = _nombre;
}
public byte getDia() {
return _dia;
}
public void setDia(byte _dia) {
this._dia = _dia;
}
public byte getMes() {
return _mes;
}
public void setMes(byte _mes) {
this._mes = _mes;
}
public byte getAnio() {
return _anio;
}
public void setAnio(byte _anio) {
this._anio = _anio;
}
public void getFecha() {
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("El partido queda fijado para el dia "+_dia+" de "+mes+" del 20"+_anio);
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}
public String setFecha(byte d,byte m,byte a) {
this._dia = d;
this._mes = m;
this._anio = a;
String fecha = _dia+" "+_mes+" "+_anio;
return fecha;
}
public ArrayList<Team> getListaTeam() {
return _listaTeam;
}
public void setListaTeam(ArrayList<Team> _listaTeam) {
this._listaTeam = _listaTeam;
}
public Team getObjTeam() {
return objTeam;
}
public void setObjTeam(Team objTeam) {
this.objTeam = objTeam;
}
Entidad.Team
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
package t;
public class Team {
private String _nombre;
private String _ciudad;
private Player[] arrPlayer;
private Player objPlayer;
public Team() {
arrPlayer = new Player[6];
objPlayer= new Player();
}
public Team(String _nombre, String _ciudad) {
this._nombre = _nombre;
this._ciudad = _ciudad;
this.arrPlayer = arrPlayer;
arrPlayer = new Player[6];
objPlayer= new Player();
}
public String getCiudad() {
return _ciudad;
}
public void setCiudad(String _ciudad) {
this._ciudad = _ciudad;
}
public String getNombre() {
return _nombre;
}
public void setNombre(String _nombre) {
this._nombre = _nombre;
}
public Player[] getArrPlayer() {
return arrPlayer;
}
public void setArrPlayer(Player[] v) {
this.arrPlayer = v;
}
public Player getObjPlayer() {
return objPlayer;
}
public void setObjPlayer(Player objPlayer) {
this.objPlayer = objPlayer;
}
}
Entidad.Player
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
public class Player {
private String _nombre;
private String _apellido;
private float _tamanio;
public Player() {
}
public Player(String _nombre, String _apellido, float _tamanio) {
this._nombre = _nombre;
this._apellido = _apellido;
this._tamanio = _tamanio;
}
public float getTamanio() {
return _tamanio;
}
public void setTamanio(float _tamanio) {
this._tamanio = _tamanio;
}
public String getNombre() {
return _nombre;
}
public void setNombre(String _nombre) {
this._nombre = _nombre;
}
public String getApellido() {
return _apellido;
}
public void setApellido(String _apellido) {
this._apellido = _apellido;
}
}
Valora esta pregunta
0