elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Sigue las noticias más importantes de seguridad informática en el Twitter! de elhacker.NET


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  Ayudaaaaa. Exception in thread "main" java.lang.NullPointerException en java
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Ayudaaaaa. Exception in thread "main" java.lang.NullPointerException en java  (Leído 6,723 veces)
Ruusa

Desconectado Desconectado

Mensajes: 32


Ver Perfil
Ayudaaaaa. Exception in thread "main" java.lang.NullPointerException en java
« en: 13 Junio 2022, 03:28 am »

Buenas noches.
    Estoy trabajando en un juego con MVC y rmi. Lo estoy haciendo con vista en consola y me sale el error cuando quiero agregar un jugador al array de jugadores desde la clase VistaConsola:
   
    Exception in thread "main" java.lang.NullPointerException
       at Vista.VistaConsola.menu(VistaConsola.java:37)
       at Vista.VistaConsola.iniciar(VistaConsola.java:196)
       at Controlador.ControladorJuego.<init>(ControladorJuego.java:53)
       at AppCliente.main(AppCliente.java:66)
   
   
   
    Les dejo mis clases:
   
   
   
   

Código
  1. public class Juego extends ObservableRemoto implements IJuego{
  2.  
  3.  
  4.  
  5.    public ArrayList<Jugador> jugadores = new ArrayList<>();
  6.  
  7.  
  8.     public ArrayList<Jugador> jugador
  9.  
  10.  
  11.     public Juego() {
  12.     jugadorActual = 0;
  13.     ronda = 1;
  14.     }
  15.  
  16.    public void iniciar() throws RemoteException {
  17.     estado=INICIANDO_JUEGO;
  18.     jugadorActual=0;
  19.     ronda=1;
  20.       notificarObservadores(1);
  21.     }
  22.  
  23.  
  24.  
  25.     public void agregarJugador (String nombre) throws RemoteException  {
  26.     jugadores.add(new Jugador(nombre));
  27.     notificarObservadores(2);
  28.  
  29.     }
  30.  
  31.    }
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.    public class VistaConsola implements ControlVista {
  39.  
  40.     private static  ControladorJuego miControl;
  41.  
  42.     public void menu() throws RemoteException {
  43.     System.out.println("-------------------------");
  44.     System.out.println("-     Configuración     -");
  45.     System.out.println("-------------------------");
  46.     System.out.println("-        M E N U        -");
  47.     System.out.println("-------------------------");
  48.     System.out.println("- 1 - Agregar Jugador   -");
  49.     System.out.println("- 2 - Mostrar Jugadores -");
  50.     System.out.println("- 3 - Iniciar Juego     -");
  51.     System.out.println("-------------------------");
  52.     System.out.println(" ");
  53.  
  54.     Scanner a = new Scanner(System.in);
  55.     int i = a.nextInt();
  56.     switch (i) {
  57.     case 1:
  58.     miControl.agregarJugador(nombreJugador());
  59.  
  60.     break;
  61.     case 2:
  62.     miControl.mostrarJugadores();
  63.     break;
  64.     case 3:
  65.     miControl.jugando();
  66.     break;
  67.     }
  68.  
  69.  
  70.     }
  71.    public String nombreJugador()throws RemoteException {
  72.     String nom="";
  73.     System.out.println("Ingrese nombre de jugador ");
  74.     Scanner sc = new Scanner(System.in);
  75.  
  76.     nom = sc.nextLine();
  77.  
  78.     return nom;
  79.  
  80.     }
  81.  
  82.     public void mostrarJugadores() throws RemoteException {
  83.     ArrayList<Jugador> jugadores = miControl.getJugadores();
  84.     System.out.println(jugadores);
  85.  
  86.     }
  87.  
  88.     public void iniciar() throws RemoteException{
  89.     menu();
  90.     }
  91.  
  92.    public void setControlador(ControladorJuego controlador) throws RemoteException{
  93.     miControl = controlador;
  94.     }
  95.    }
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.    public class ControladorJuego implements IControladorRemoto ;
  105.  
  106.  
  107.  
  108.     private   IJuego miJuego;
  109.     private  ControlVista miVista;
  110.  
  111.     public ControladorJuego(ControlVista miVista) throws RemoteException  {
  112.     this.miVista = miVista;
  113.     miVista.iniciar();
  114.  
  115.  
  116.     }
  117.  
  118.     public void agregarJugador(String nombre) throws RemoteException  {
  119.     miJuego.agregarJugador(nombre);
  120.     }
  121.  
  122.    public String mostrarJugadores() throws RemoteException  {
  123.     return miJuego.mostrarJugadores();
  124.     }
  125.  
  126.    public void actualizar(IObservableRemoto modelo, Object queCambio) throws RemoteException{
  127.     int cambio = (int) queCambio;
  128.     switch (cambio) {
  129.     case 1:
  130.     break;
  131.     case 2:
  132.     System.out.println("Jugador agregado con exito");
  133.  
  134.     break;
  135.    }
  136.    }
  137.  
  138.  
  139.  
  140.  
  141.    public interface ControlVista {
  142.     void menu() throws RemoteException;
  143.    }
  144.  
  145.  
  146.  
  147.  
  148.     public class AppCliente {
  149.  
  150.     public  IJuego miJuego;
  151.  
  152.     public static void main(String[] args) throws RemoteException {
  153.     ArrayList<String> ips = Util.getIpDisponibles();;
  154.     String ip = (String) JOptionPane.showInputDialog(
  155.     null,
  156.     "Seleccione la IP en la que escuchará peticiones el cliente", "IP del cliente",
  157.     JOptionPane.QUESTION_MESSAGE,
  158.  
  159.  
  160.     null,
  161.     ips.toArray(),
  162.     null
  163.     );
  164.     String port = (String) JOptionPane.showInputDialog(
  165.     null,
  166.     "Seleccione el puerto en el que escuchará peticiones el cliente", "Puerto del cliente",
  167.     JOptionPane.QUESTION_MESSAGE,
  168.     null,
  169.     null,
  170.     9999
  171.     );
  172.     String ipServidor = (String) JOptionPane.showInputDialog(
  173.     null,
  174.     "Seleccione la IP en la corre el servidor", "IP del servidor",
  175.     JOptionPane.QUESTION_MESSAGE,
  176.     null,
  177.     null,
  178.     null
  179.     );
  180.     String portServidor = (String) JOptionPane.showInputDialog(
  181.     null,
  182.     "Seleccione el puerto en el que corre el servidor", "Puerto del servidor",
  183.     JOptionPane.QUESTION_MESSAGE,
  184.     null,
  185.     null,
  186.     8888
  187.     );
  188.  
  189.     ControlVista vista = new VistaConsola();
  190.     ControladorJuego controlador = new ControladorJuego(vista);
  191.     Cliente c = new Cliente(ip, Integer.parseInt(port), ipServidor, Integer.parseInt(portServidor));
  192.     vista.setControlador(controlador);
  193.     //vista.iniciar();
  194.     try {
  195.  
  196.     c.iniciar(controlador);
  197.     } catch (RemoteException e) {
  198.     // TODO Auto-generated catch block
  199.  
  200.     e.printStackTrace();
  201.     } catch (RMIMVCException e) {
  202.     // TODO Auto-generated catch block
  203.     e.printStackTrace();
  204.     }
  205.     }
  206.     }
   


Si alguien me puede ayudar se lo agradeceria, no tengo idea de cual puede ser el error!





En línea

rub'n


Desconectado Desconectado

Mensajes: 1.217


(e -> λ("live now")); tatuar -> λ("α");


Ver Perfil WWW
Re: Ayudaaaaa. Exception in thread "main" java.lang.NullPointerException en java
« Respuesta #1 en: 15 Junio 2022, 13:06 pm »

Actualiza el codigo con un nuevo log.

Para ver donde esta el null

Y Organiza mas esas clases, que faltan muchas xD, haces dificil que te ayuden asi socio.


En línea



rubn0x52.com KNOWLEDGE  SHOULD BE FREE.
If you don't have time to read, you don't have the time (or the tools) to write, Simple as that. Stephen ki
sapito169


Desconectado Desconectado

Mensajes: 628



Ver Perfil
Re: Ayudaaaaa. Exception in thread "main" java.lang.NullPointerException en java
« Respuesta #2 en: 16 Junio 2022, 00:05 am »

falta la clase cliente no se puede ayuda si no esta completa
En línea

[JMS]

Desconectado Desconectado

Mensajes: 4



Ver Perfil WWW
Re: Ayudaaaaa. Exception in thread "main" java.lang.NullPointerException en java
« Respuesta #3 en: 22 Junio 2022, 18:01 pm »

Hola amig@. Te falta codigo para poder analizar el error. Pero por lo pronto veo que no intancias el atributo mijuego en la clase ControladorJuego. Y puede que te esté ahí el problema. Pero si puedes subir todas las clases e interfaces mejor.
En línea

Ruusa

Desconectado Desconectado

Mensajes: 32


Ver Perfil
Re: Ayudaaaaa. Exception in thread "main" java.lang.NullPointerException en java
« Respuesta #4 en: 22 Junio 2022, 21:12 pm »

Hola, aca les dejo todas las clases:


Clase AppCliente:
Código
  1. import java.util.ArrayList;
  2.  
  3.  
  4.  
  5. import java.rmi.RemoteException;
  6. import java.util.ArrayList;
  7.  
  8. import javax.swing.JOptionPane;
  9. import Controlador.ControladorJuego;
  10. import Vista.ControlVista;
  11. import Vista.VistaConsola;
  12. import Vista.VistaGrafica;
  13. import ar.edu.unlu.rmimvc.RMIMVCException;
  14. import ar.edu.unlu.rmimvc.cliente.Cliente;
  15. import ar.edu.unlu.rmimvc.Util;
  16. import Modelo.IJuego;
  17. import Modelo.Juego;
  18.  
  19.  
  20.  
  21.  
  22. //import cliente.Cliente;
  23.  
  24. public class AppCliente {
  25.  
  26. public  IJuego miJuego;
  27.  
  28. public static void main(String[] args) throws RemoteException {
  29. ArrayList<String> ips = Util.getIpDisponibles();;
  30. String ip = (String) JOptionPane.showInputDialog(
  31. null,
  32. "Seleccione la IP en la que escuchará peticiones el cliente", "IP del cliente",
  33. JOptionPane.QUESTION_MESSAGE,
  34.  
  35.  
  36. null,
  37. ips.toArray(),
  38. null
  39. );
  40. String port = (String) JOptionPane.showInputDialog(
  41. null,
  42. "Seleccione el puerto en el que escuchará peticiones el cliente", "Puerto del cliente",
  43. JOptionPane.QUESTION_MESSAGE,
  44. null,
  45. null,
  46. 9999
  47. );
  48. String ipServidor = (String) JOptionPane.showInputDialog(
  49. null,
  50. "Seleccione la IP en la corre el servidor", "IP del servidor",
  51. JOptionPane.QUESTION_MESSAGE,
  52. null,
  53. null,
  54. null
  55. );
  56. String portServidor = (String) JOptionPane.showInputDialog(
  57. null,
  58. "Seleccione el puerto en el que corre el servidor", "Puerto del servidor",
  59. JOptionPane.QUESTION_MESSAGE,
  60. null,
  61. null,
  62. 8888
  63. );
  64.  
  65.  
  66. ControlVista vista = new VistaGrafica();
  67. ControladorJuego controlador = new ControladorJuego(vista);
  68. Cliente c = new Cliente(ip, Integer.parseInt(port), ipServidor, Integer.parseInt(portServidor));
  69. vista.setControlador(controlador);
  70. [color=red][b]vista.iniciar();[/b][/color]
  71. try {
  72.  
  73. c.iniciar(controlador);
  74. } catch (RemoteException e) {
  75. // TODO Auto-generated catch block
  76.  
  77. e.printStackTrace();
  78. } catch (RMIMVCException e) {
  79. // TODO Auto-generated catch block
  80. e.printStackTrace();
  81. }
  82. }
   }



Clase AppServidor
:

Código
  1. import java.rmi.RemoteException;
  2. import java.util.ArrayList;
  3.  
  4. import javax.swing.JOptionPane;
  5.  
  6. import Modelo.IJuego;
  7. import Modelo.Juego;
  8. import ar.edu.unlu.rmimvc.RMIMVCException;
  9. import ar.edu.unlu.rmimvc.Util;
  10. import ar.edu.unlu.rmimvc.servidor.Servidor;
  11.  
  12. public class AppServidor {
  13.  
  14.  
  15.  
  16. public static void main(String[] args) throws RemoteException {
  17. ArrayList<String> ips = Util.getIpDisponibles();
  18. String ip = (String) JOptionPane.showInputDialog(
  19. null,
  20. "Seleccione la IP en la que escuchará peticiones el servidor", "IP del servidor",
  21. JOptionPane.QUESTION_MESSAGE,
  22. null,
  23. ips.toArray(),
  24. null
  25. );
  26. String port = (String) JOptionPane.showInputDialog(
  27. null,
  28. "Seleccione el puerto en el que escuchará peticiones el servidor", "Puerto del servidor",
  29. JOptionPane.QUESTION_MESSAGE,
  30. null,
  31. null,
  32. 8888
  33. );
  34.  
  35.  
  36. Juego modelo = new Juego();
  37. System.out.println("Juego creado");
  38. Servidor servidor = new Servidor(ip, Integer.parseInt(port));
  39. try {
  40. servidor.iniciar(modelo);
  41. } catch (RemoteException e) {
  42. // TODO Auto-generated catch block
  43. e.printStackTrace();
  44. } catch (RMIMVCException e) {
  45. // TODO Auto-generated catch block
  46. e.printStackTrace();
  47. }
  48. }
  49. }



Clase jugador:
Código
  1. import java.io.Serializable;
  2. import java.util.ArrayList;
  3.  
  4. import Cartas.Cartas;
  5. import Cartas.Mazo;
  6. import Cartas.Palos;
  7. import ar.edu.unlu.rmimvc.observer.ObservableRemoto;
  8.  
  9. public class Jugador implements Serializable {
  10.  
  11. public String nombre;
  12.        public int puntos=0;
  13.  
  14.  
  15.  
  16.  
  17. public Jugador (String nombre){
  18. this.nombre=nombre;
  19. this.puntos=0;
  20.  
  21.  
  22. }


Clase Juego

Código
  1. public class Juego extends ObservableRemoto implements IJuego{
  2.  
  3.  
  4.  
  5.    public ArrayList<Jugador> jugadores = new ArrayList<>();
  6.  
  7.  
  8.  
  9.  
  10.     public Juego() {
  11.     jugadorActual = 0;
  12.     ronda = 1;
  13.     }
  14.  
  15.    public void iniciar() throws RemoteException {
  16.     estado=INICIANDO_JUEGO;
  17.     jugadorActual=0;
  18.     ronda=1;
  19.       notificarObservadores(1);
  20.     }
  21.  
  22.  
  23.  
  24.     public void agregarJugador (String nombre) throws RemoteException  {
  25.     jugadores.add(new Jugador(nombre));
  26.     notificarObservadores(2);
  27.  
  28.     }
  29.  
  30.    }


Interface IJuego:

Código
  1. import java.io.EOFException;
  2. import java.io.FileNotFoundException;
  3. import java.io.IOException;
  4. import java.io.StreamCorruptedException;
  5. import java.rmi.RemoteException;
  6. import java.util.ArrayList;
  7.  
  8. import Cartas.Cartas;
  9. import Cartas.Mazo;
  10. import ar.edu.unlu.rmimvc.observer.IObservableRemoto;
  11.  
  12. public interface IJuego extends IObservableRemoto {
  13.  
  14.  
  15. void iniciar() throws RemoteException;
  16.  
  17. void jugando() throws RemoteException;
  18.  
  19.    void agregarJugador(String nombre) throws RemoteException;
  20. }


Controlador:



Código
  1. public class ControladorJuego implements IControladorRemoto ;
  2.  
  3.  
  4.  
  5.     private   IJuego miJuego;
  6.     private  ControlVista miVista;
  7.  
  8.     public ControladorJuego(ControlVista miVista) throws RemoteException  {
  9.     this.miVista = miVista;
  10.     miVista.iniciar();
  11.  
  12.  
  13.     }
  14.  
  15.     public void agregarJugador(String nombre) throws RemoteException  {
  16.     [color=red][b]miJuego.agregarJugador(nombre);[/b][/color]
  17.     }
  18.  
  19.    public String mostrarJugadores() throws RemoteException  {
  20.     return miJuego.mostrarJugadores();
  21.     }
  22.  
  23.    public void actualizar(IObservableRemoto modelo, Object queCambio) throws RemoteException{
  24.     int cambio = (int) queCambio;
  25.     switch (cambio) {
  26.     case 1:
  27.     break;
  28.     case 2:
  29.     System.out.println("Jugador agregado con exito");
  30.  
  31.     break;
  32.    }
  33.  
  34. @Override
  35. public <T extends IObservableRemoto> void setModeloRemoto(T arg0) throws RemoteException {
  36. // TODO Auto-generated method stub
  37.  
  38. }
  39.  
  40.    }



Clase vistaConsola:

Código
  1. import java.rmi.RemoteException;
  2. import java.util.ArrayList;
  3. import java.util.Scanner;
  4.  
  5. import Vista.ControlVista;
  6. import Controlador.ControladorJuego;
  7. import Modelo.Juego;
  8. import Modelo.Jugador;
  9.  
  10. public class VistaConsola implements ControlVista {
  11.  
  12.  
  13. private static  ControladorJuego miControl;
  14.  
  15. public void menu() throws RemoteException {
  16. System.out.println("-------------------------");
  17. System.out.println("-     Configuración     -");
  18. System.out.println("-------------------------");
  19. System.out.println("-        M E N U        -");
  20. System.out.println("-------------------------");
  21. System.out.println("- 1 - Agregar Jugador   -");
  22. System.out.println("- 2 - Mostrar Jugadores -");
  23. System.out.println("- 3 - Iniciar Juego     -");
  24. System.out.println("-------------------------");
  25. System.out.println(" ");
  26.  
  27. Scanner a = new Scanner(System.in);
  28. int i = a.nextInt();
  29. switch (i) {
  30. case 1:
  31. String nom="";
  32. System.out.println("Ingrese nombre de jugador ");
  33. Scanner sc = new Scanner(System.in);
  34.  
  35. nom = sc.nextLine();
  36. [color=red][b]miControl.agregarJugador(nom);[/b][/color]
  37. break;
  38. }
  39.  
  40.  
  41. public void iniciar() throws RemoteException{
  42. [color=red][b]menu();[/b][/color]
  43. }
  44.  
  45. @Override
  46. public void setControlador(ControladorJuego controlador) throws RemoteException{
  47. miControl = controlador;
  48. }
  49.  
  50. }



Interface controlVista


Código
  1. import java.io.IOException;
  2. import java.rmi.RemoteException;
  3.  
  4. import Controlador.ControladorJuego;
  5.  
  6. public interface ControlVista {
  7. void menu() throws RemoteException;
  8. void setControlador(ControladorJuego controlador) throws RemoteException;
  9.        void iniciar() throws RemoteException ;
  10. }




El error es:

Exception in thread "main" java.lang.NullPointerException
   at Controlador.ControladorJuego.agregarJugador(ControladorJuego.java:)
   at Vista.VistaConsola.menu(VistaConsola.java:)
   at Vista.VistaConsola.iniciar(VistaConsola.java:)
   at AppCliente.main(AppCliente.java:)

Marque en rojo las lineas en las que me salta error
En línea

[JMS]

Desconectado Desconectado

Mensajes: 4



Ver Perfil WWW
Re: Ayudaaaaa. Exception in thread "main" java.lang.NullPointerException en java
« Respuesta #5 en: 22 Junio 2022, 23:17 pm »

Te falta la instanciacion en el constructor de ControladorJuego de la interface IJuego mi juego
Código
  1. public ControladorJuego(ControlVista miVista) throws RemoteException  {
  2.     this.miVista = miVista;
  3.     miVista.iniciar();
  4.                this.miJuego = new Juego();
  5.  
  6.     }

Con esto deberia funcionar. Si ves que sigue la excepción haría una depuración paso a paso. Para ver porque es null miControl
En línea

Ruusa

Desconectado Desconectado

Mensajes: 32


Ver Perfil
Re: Ayudaaaaa. Exception in thread "main" java.lang.NullPointerException en java
« Respuesta #6 en: 25 Junio 2022, 01:54 am »

hola gracias por tu respuesta. Si inicializo juego en el controlador se me crean dos juegos diferentes para cada cliente
En línea

rub'n


Desconectado Desconectado

Mensajes: 1.217


(e -> λ("live now")); tatuar -> λ("α");


Ver Perfil WWW
Re: Ayudaaaaa. Exception in thread "main" java.lang.NullPointerException en java
« Respuesta #7 en: 26 Junio 2022, 15:14 pm »

hola gracias por tu respuesta. Si inicializo juego en el controlador se me crean dos juegos diferentes para cada cliente

Tienes otra referencia en la linea "108" private   IJuego miJuego;
En línea



rubn0x52.com KNOWLEDGE  SHOULD BE FREE.
If you don't have time to read, you don't have the time (or the tools) to write, Simple as that. Stephen ki
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
[Solucionado] java.lang.NullPointerException por qué?
Java
Nakatsukasa Itero 0 5,077 Último mensaje 9 Mayo 2012, 21:32 pm
por Nakatsukasa Itero
Validar firma digita error java.lang.NullPointerException
Windows
mauro_ms 0 2,706 Último mensaje 13 Abril 2016, 22:36 pm
por mauro_ms
Ayuda por favor java.lang.NullPointerException
Java
TheZeck 1 2,327 Último mensaje 2 Mayo 2017, 00:55 am
por HardC0d3
Error java.lang.Byte cannot be cast to java.lang.Integer
Java
S4ms3pi0l__ 1 3,424 Último mensaje 24 Agosto 2019, 18:25 pm
por ivancea96
java.lang.NullPointerException en funcion para polimeros
Java
juligarc10 1 2,425 Último mensaje 25 Septiembre 2019, 21:13 pm
por rub'n
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines