Java - ayuda!! Exception in thread "main" java.lang.NullPointerException

 
Vista:

ayuda!! Exception in thread "main" java.lang.NullPointerException

Publicado por Rocio (21 intervenciones) el 10/06/2022 00:27:07
Buenas noches.
Estoy trabajando en un juego con MVC. Lo estoy haciendo con vista en consola y me sale el error:

Exception in thread "main" java.lang.NullPointerException
at Vista.VistaConsola.menu(VistaConsola.java:32)
at Vista.VistaConsola.iniciar(VistaConsola.java:188)
at Controlador.ControladorJuego.<init>(ControladorJuego.java:53)



Les dejo mis clases:

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
package Modelo;
 
import java.rmi.RemoteException;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.awt.Color;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.rmi.Remote;
 
 
import Cartas.Cartas;
import Cartas.Mazo;
import Cartas.Palos;
import Modelo.Jugador;
 
public class Juego extends ObservableRemoto implements IJuego{
 
 
	public ArrayList<Jugador> jugadores = new ArrayList<>();
 
 
	public ArrayList<Jugador> jugador
 
 
	public Juego() {
		jugadorActual = 0;
		ronda = 1;
	}
 
 
 
 
	public void agregarJugador (String nombre) throws RemoteException  {
			jugadores.add(new Jugador(nombre));
			notificarObservadores(2);
 
	}
 
}
 
 
 
 
package Vista;
 
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Scanner;
 
import Vista.ControlVista;
import Controlador.ControladorJuego;
import Modelo.Jugador;
 
public class VistaConsola implements ControlVista {
 
	private static  ControladorJuego miControl;
 
	public void menu() throws RemoteException {
		System.out.println("-------------------------");
		System.out.println("-     Configuración     -");
		System.out.println("-------------------------");
		System.out.println("-        M E N U        -");
		System.out.println("-------------------------");
		System.out.println("- 1 - Agregar Jugador   -");
		System.out.println("- 2 - Mostrar Jugadores -");
		System.out.println("- 3 - Iniciar Juego     -");
		System.out.println("-------------------------");
		System.out.println(" ");
 
		Scanner a = new Scanner(System.in);
		int i = a.nextInt();
		switch (i) {
		case 1:
			miControl.agregarJugador(nombreJugador());
 
			break;
		case 2:
			miControl.mostrarJugadores();
			break;
		case 3:
			miControl.jugando();
			break;
		}
 
 
	}
public String nombreJugador()throws RemoteException {
		String nom="";
		System.out.println("Ingrese nombre de jugador ");
		Scanner sc = new Scanner(System.in);
 
		nom = sc.nextLine();
 
		return nom;
 
	}
 
	public void mostrarJugadores() throws RemoteException {
		ArrayList<Jugador> jugadores = miControl.getJugadores();
		System.out.println(jugadores);
 
	}
 
	public void iniciar() throws RemoteException{
		menu();
	}
 
 
package Controlador;
 
 
 
import Cartas.Cartas;
import Modelo.IJuego;
import Modelo.Juego;
import Modelo.Jugador;
import Vista.ControlVista;
import Vista.VistaConsola;
import Vista.VistaGrafica;
import ar.edu.unlu.rmimvc.cliente.IControladorRemoto;
import ar.edu.unlu.rmimvc.observer.IObservableRemoto;
 
public class ControladorJuego implements IControladorRemoto ;
 
 
 
	private   IJuego miJuego;
	private  ControlVista miVista;
 
	public ControladorJuego(ControlVista miVista) throws RemoteException  {
		this.miVista = miVista;
		miVista.iniciar();
 
 
	}
 
	public void agregarJugador(String nombre) throws RemoteException  {
			miJuego.agregarJugador(nombre);
	}
 
public String mostrarJugadores() throws RemoteException  {
		return miJuego.mostrarJugadores();
	}
 
public void actualizar(IObservableRemoto modelo, Object queCambio) throws RemoteException{
		int cambio = (int) queCambio;
		switch (cambio) {
		case 1:
			break;
		case 2:
			System.out.println("Jugador agregado con exito");
 
			break;
}
}
 
 
 
 
public interface ControlVista {
	void menu() throws RemoteException;
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 755
Bronce
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

ayuda!! Exception in thread "main" java.lang.NullPointerException

Publicado por Yamil (2315 intervenciones) el 10/06/2022 03:28:58
En VistaConsola nunca creas una instancia de la clase ControladorJuego. Deberia ser algo como
private static ControladorJuego miControl; = new ControladorJuego(this)
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

ayuda!! Exception in thread "main" java.lang.NullPointerException

Publicado por Rocio (21 intervenciones) el 13/06/2022 01:30:38
Hola! gracias por tu respuesta, la instancia la creo en la clase AppCliente. Olvide mencionar que estoy trabajando con rmi.

import java.util.ArrayList;



import java.rmi.RemoteException;
import java.util.ArrayList;

import javax.swing.JOptionPane;
import Controlador.ControladorJuego;
import Vista.ControlVista;
import Vista.VistaConsola;
import Vista.VistaGrafica;
import ar.edu.unlu.rmimvc.RMIMVCException;
import ar.edu.unlu.rmimvc.cliente.Cliente;
import ar.edu.unlu.rmimvc.Util;
import Modelo.IJuego;
import Modelo.Juego;




//import cliente.Cliente;
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
public class AppCliente {
 
		public  IJuego miJuego;
 
		public static void main(String[] args) throws RemoteException {
			ArrayList<String> ips = Util.getIpDisponibles();;
			String ip = (String) JOptionPane.showInputDialog(
					null,
					"Seleccione la IP en la que escuchará peticiones el cliente", "IP del cliente",
					JOptionPane.QUESTION_MESSAGE,
 
 
					null,
					ips.toArray(),
					null
			);
			String port = (String) JOptionPane.showInputDialog(
					null,
					"Seleccione el puerto en el que escuchará peticiones el cliente", "Puerto del cliente",
					JOptionPane.QUESTION_MESSAGE,
					null,
					null,
					9999
			);
			String ipServidor = (String) JOptionPane.showInputDialog(
					null,
					"Seleccione la IP en la corre el servidor", "IP del servidor",
					JOptionPane.QUESTION_MESSAGE,
					null,
					null,
					null
			);
			String portServidor = (String) JOptionPane.showInputDialog(
					null,
					"Seleccione el puerto en el que corre el servidor", "Puerto del servidor",
					JOptionPane.QUESTION_MESSAGE,
					null,
					null,
					8888
			);
 
			ControlVista vista = new VistaConsola();
			ControladorJuego controlador = new ControladorJuego(vista);
			Cliente c = new Cliente(ip, Integer.parseInt(port), ipServidor, Integer.parseInt(portServidor));
			vista.setControlador(controlador);
			try {
 
				c.iniciar(controlador);
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
 
				e.printStackTrace();
			} catch (RMIMVCException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar