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

 

 


Tema destacado: Security Series.XSS. [Cross Site Scripting]


  Mostrar Mensajes
Páginas: [1] 2 3 4
1  Programación / Scripting / Ayuda para crear inputs dinámicos! en: 30 Julio 2022, 22:13 pm
Hola! Soy principiante en javascript. Quiero crear un código que al apretar un boton me cree clones de un select y un botón para eliminar el clon que elija y nose como hacerlo! Si alguien me puede guiar se lo agradecería mucho
2  Programación / Java / Re: Ayudaaaaa. Exception in thread "main" java.lang.NullPointerException en java 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
3  Programación / Java / Re: Ayudaaaaa. Exception in thread "main" java.lang.NullPointerException en java 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
4  Programación / Java / 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!



5  Programación / Java / Error con Spring boot! Ayuda en: 17 Febrero 2022, 20:12 pm
Hola! Buenas tardes. Soy nueva con Spring y me aparecio este error:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-02-17 16:02:00.613 ERROR 21000 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   :

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of method inicio in mx.com.gm.ControladorInicio required a bean of type 'org.springframework.ui.Model' that could not be found.


Action:

Consider defining a bean of type 'org.springframework.ui.Model' in your configuration.


Alguien sabe a que se debe? Les dejo mi codigo

Código
  1. package mx.com.gm;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.context.annotation.Configuration;
  6.  
  7. @SpringBootApplication
  8. public class HolaSpringApplication {
  9.  
  10. public static void main(String[] args) {
  11. SpringApplication.run(HolaSpringApplication.class, args);
  12. }
  13.  
  14. }
  15.  
  16.  
  17. package mx.com.gm;
  18.  
  19.  
  20. import java.util.ArrayList;
  21. import java.util.Arrays;
  22.  
  23. import org.springframework.beans.factory.annotation.Autowired;
  24.  
  25. import org.springframework.beans.factory.annotation.Value;
  26. import org.springframework.context.annotation.Bean;
  27. import org.springframework.stereotype.Controller;
  28. import org.springframework.ui.Model;
  29. import org.springframework.web.bind.annotation.GetMapping;
  30. import org.springframework.web.bind.annotation.ModelAttribute;
  31.  
  32. import lombok.extern.java.Log;
  33. import lombok.extern.slf4j.Slf4j;
  34. import mx.com.gm.dominio.Persona;
  35.  
  36.  
  37.  
  38. @Controller
  39.  
  40.  
  41. public class ControladorInicio {
  42.  
  43.  
  44.  
  45.   private PersonaDao personadao;
  46.  
  47. @GetMapping("/")
  48. @Autowired
  49.  
  50.  
  51. public String inicio (Model model) {
  52. String saludar = "Adios mundo con thymeleaf";
  53.  
  54. var personas = personadao.findAll();
  55.  
  56. model.addAttribute("personas", personas);
  57.  
  58.  
  59.  
  60.  
  61.  
  62. return "index";
  63.  
  64. }
  65. }
  66.  
  67.  
  68. package mx.com.gm.dominio;
  69.  
  70. import java.io.Serializable;
  71.  
  72. import javax.persistence.Entity;
  73. import javax.persistence.GeneratedValue;
  74. import javax.persistence.GenerationType;
  75. import javax.persistence.Id;
  76.  
  77. //import lombok.Data;
  78.  
  79. //@Data
  80.  
  81.  
  82. public class Persona implements Serializable {
  83.  
  84. private static final long SerialVersionUID= 1L;
  85.  
  86. @Id
  87. @GeneratedValue(strategy = GenerationType.IDENTITY)
  88.    private Integer ID;
  89. private String nombre;
  90. private String apellido;
  91. private String email;
  92.  
  93. public String getNombre() {
  94. return nombre;
  95. }
  96. public void setNombre(String nombre) {
  97. this.nombre = nombre;
  98. }
  99. public String getApellido() {
  100. return apellido;
  101. }
  102. public void setApellido(String apellido) {
  103. this.apellido = apellido;
  104. }
  105. public String getEmail() {
  106. return email;
  107. }
  108. public void setEmail(String email) {
  109. this.email = email;
  110. }
  111.  
  112.  
  113.  
  114.  
  115. }
  116.  
  117. package mx.com.gm;
  118.  
  119. import org.springframework.data.repository.CrudRepository;
  120.  
  121. import mx.com.gm.dominio.Persona;
  122.  
  123. public interface PersonaDao extends CrudRepository<Persona, Integer>{
  124.  
  125.  
  126. }
  127.  
  128.  
  129. mi pom.xml
  130. <?xml version="1.0" encoding="UTF-8"?>
  131. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  132.         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  133.    <modelVersion>4.0.0</modelVersion>
  134.    <parent>
  135.        <groupId>org.springframework.boot</groupId>
  136.        <artifactId>spring-boot-starter-parent</artifactId>
  137.        <version>2.2.4.RELEASE</version>
  138.        <relativePath/> <!-- lookup parent from repository -->
  139.    </parent>
  140.    <groupId>mx.com.gm</groupId>
  141.    <artifactId>HolaMundoSpringData</artifactId>
  142.    <version>1.0</version>
  143.    <name>HolaMundoSpringData</name>
  144.    <description>HolaMundo con Spring</description>
  145.  
  146.    <properties>
  147.        <java.version>13</java.version>
  148.    </properties>
  149.  
  150.    <dependencies>
  151.        <dependency>
  152.            <groupId>org.springframework.boot</groupId>
  153.            <artifactId>spring-boot-starter-thymeleaf</artifactId>
  154.        </dependency>
  155.        <dependency>
  156.            <groupId>org.springframework.boot</groupId>
  157.            <artifactId>spring-boot-starter-web</artifactId>
  158.        </dependency>
  159.        <dependency>
  160.            <groupId>org.springframework.boot</groupId>
  161.            <artifactId>spring-boot-devtools</artifactId>
  162.            <scope>runtime</scope>
  163.            <optional>true</optional>
  164.        </dependency>
  165.        <dependency>
  166.            <groupId>org.projectlombok</groupId>
  167.            <artifactId>lombok</artifactId>
  168.            <optional>true</optional>
  169.        </dependency>
  170.        <dependency>
  171.            <groupId>mysql</groupId>
  172.            <artifactId>mysql-connector-java</artifactId>
  173.            <scope>runtime</scope>
  174.        </dependency>
  175.        <dependency>
  176.            <groupId>org.springframework.boot</groupId>
  177.            <artifactId>spring-boot-starter-data-jpa</artifactId>
  178.        </dependency>
  179.        <dependency>
  180.            <groupId>org.springframework.boot</groupId>
  181.            <artifactId>spring-boot-starter-test</artifactId>
  182.            <scope>test</scope>
  183.            <exclusions>
  184.                <exclusion>
  185.                    <groupId>org.junit.vintage</groupId>
  186.                    <artifactId>junit-vintage-engine</artifactId>
  187.                </exclusion>
  188.            </exclusions>
  189.        </dependency>
  190.    </dependencies>
  191.  
  192.    <build>
  193.        <plugins>
  194.            <plugin>
  195.                <groupId>org.springframework.boot</groupId>
  196.                <artifactId>spring-boot-maven-plugin</artifactId>
  197.            </plugin>
  198.        </plugins>
  199.    </build>
  200.  
  201. </project>
  202.  
  203.  
  204. application.properti:
  205.  
  206. spring.datasource.url=jdbc:mysql://localhost:3306/spring?zeroDateTimeBehavior=convertToNull&useSSL=false&useTimezone=true&serverTimezone=UTC&allowPublicKeyRetrieval=true
  207. spring.datasource.username=root
  208. spring.datasource.password=123777
  209. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  210. spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
  211. spring.jpa.properties.hibernate.format_sql=true
  212. logging.level.org.hibernate.SQL=DEBUG
  213. logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
  214.  
  215.  


Código
  1. <!DOCTYPE html>
  2. <html xmlns:th="http://www.thymeleaf.org">
  3.    <meta charset="UTF-8">
  4.    <meta http-equiv="X-UA-Compatible" content="IE=edge">
  5.    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.    <title>Bienvenido</title>
  7. </head>
  8.    <h1>inicio</h1>
  9.  
  10.  
  11.   <table border= "1">
  12.    <tr>
  13.        <th> Nombre: </th>
  14.        <th> Apellido: </th>
  15.        <th> Email: </th>
  16.    </tr>
  17.  
  18.    <tr th:each="persona : ${personas}">
  19.  
  20.     <td th:text="${persona.nombre}">Nombre</td>
  21.     <td th:text="${persona.apellido}">Apellido</td>
  22.    <td th:text="${persona.email}">Email</td>
  23.  
  24.  
  25.    </tr>
  26.    </table>
  27.  
  28.  
  29. </body>
  30. </html>
  31.  
6  Programación / Java / Ayuda para leer archivo de objetos! en: 13 Noviembre 2021, 17:30 pm
Hola a todos! Tengo problemas para leer un archivo de objetos, si alguien me puede ayudar se lo agradeceria!

Asi guardo los objetos:
Código
  1. public void guardar (Jugador j) throws IOException {
  2. ganadores.add(j);
  3. File f= new File("jugador7.dat");
  4. if( f.exists()){
  5. MiObjectOutputStream salida= new MiObjectOutputStream(new FileOutputStream(f));
  6. salida.writeObject(ganadores);
  7. salida.close();
  8. } else {
  9. ObjectOutputStream salida= new ObjectOutputStream(new FileOutputStream("jugador9.dat"));
  10. salida.writeObject(ganadores);
  11. salida.close();
  12.  
  13. }
  14.  
  15.  
  16.  
  17. y asi los leo:
  18.  
  19. String ganadoress="";
  20. //ObjectInputStream entrada=null;
  21. System.out.println("antes");
  22. int i=0;
  23.  
  24. try {
  25. ObjectInputStream entrada = new ObjectInputStream(new FileInputStream("jugador9.dat"));
  26.  
  27. System.out.println("Entro");
  28.     ganadores = (ArrayList<Jugador>) entrada.readObject();
  29.     entrada.close();
  30.   try {
  31.     while (true) {
  32.     System.out.println(ganadores.get(i).getNombre());
  33.     i++;
  34.     ganadores = (ArrayList<Jugador>) entrada.readObject();
  35.  
  36.     }
  37.  
  38.  
  39. return ganadoress;
  40.  
  41. }
  42.  
  43.  
  44. Clase miObjectOutputStream:
  45.  
  46. public class MiObjectOutputStream extends ObjectOutputStream{
  47.  
  48. @Override
  49. protected void writeStreamHeader() throws IOException  {
  50. //nada
  51. }
  52.  
  53. public MiObjectOutputStream() throws IOException {
  54. super();
  55. }
  56.  
  57. public MiObjectOutputStream (OutputStream fileOutputStream) throws IOException  {
  58. // TODO Auto-generated constructor stub
  59. super(fileOutputStream);
  60. }
  61.  
7  Programación / Java / Ayuda con ObjectOutputStream! en: 30 Agosto 2021, 18:21 pm
Hola. Buenas tardes, estoy haciendo un ejercicio con persistencia. Y tengo problemas para grabar los objetos en un archivo. Voy agregando los objetos de a uno, ya que tengo un menu. Cuando agrego el primer objeto se lee bien, pero cuando agrego el segundo ya no se lee ninguno. Nose que estoy haciendo mal

Código
  1. public class MiObjectOutputStream extends ObjectOutputStream{ //clase q hereda de objectoutputstream para no sobreescribir la cabecera
  2.  
  3. public void writeStreamHeader() {
  4. //nada
  5. }
  6.  
  7. public MiObjectOutputStream() throws IOException {
  8. super();
  9. }
  10.  
  11. public MiObjectOutputStream (FileOutputStream fileOutputStream) throws IOException {
  12. // TODO Auto-generated constructor stub
  13. super(fileOutputStream);
  14. }
  15.  
  16. }
  17.  
  18.  
  19. //Los metodos guardar y recuperar estan en otra clase
  20.  
  21. public void guardar (Jugador j) throws IOException {
  22. File f= new File("jugador.objeto");
  23. if( f.exists()){
  24. MiObjectOutputStream salida= new MiObjectOutputStream(new FileOutputStream(f));
  25. salida.writeObject(j);
  26. salida.close();
  27. } else {
  28. salida.writeObject(j);
  29. salida.close();
  30. }
  31.  
  32.  
  33. }
  34.  
  35. ObjectInputStream entrada=null;
  36. try{ entrada = new ObjectInputStream(new FileInputStream("jugador.objeto"));
  37.  
  38. while (true) {
  39. Jugador j = ( Jugador) entrada.readObject();
  40. System.out.println(j.getNombre());
  41.  
  42.  
  43.  
  44. }
  45. } catch(IOException io){
  46. } finally {
  47. try {
  48. entrada.close();
  49. } catch (Exception exp) {
  50. }
  51.  
  52.  
  53. }

MOD: Etiqueta GeSHi
8  Programación / Java / Ayuda con persistencia! en: 18 Agosto 2021, 15:04 pm
Hola, buen dia!
Tengo un problema al implementar persistencia. Lo que quiero hacer es guardar en un archivo los ganadores de un juego y ni se llega a crear el archivo. No entiendo que es lo que estoy haciendo mal.

En la clase Juego tengo este metodo:

private Persistencia p;

public Jugador mostrarGanador()throws RemoteException { //devuelve el ganador
      int mayor=0;
      Jugador j = null;
      for (int i=0; i<jugadores.size();i++) {
         if (jugadores.get(i).getPuntos() > mayor) {
            mayor=jugadores.get(i).getPuntos();
               j=jugadores.get(i);
         }
   
      
   }
      
      try {
         p.guardar(j);  //Este es el metodo guardar de la clase Persistencia, aqui
      } catch (IOException e) {     quiero guardar el ganador
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      return j;
   }


Clase persistencia:

public class Persistencia {
   
   public void guardar (Jugador j) throws IOException {
      ObjectOutputStream salida= new ObjectOutputStream(new FileOutputStream("jugador.objeto"));
      salida.writeObject(j);
      salida.close();
   
   }
   
   
   public Jugador recuperar() throws FileNotFoundException, IOException, ClassNotFoundException {
      ObjectInputStream entrada = new ObjectInputStream(new FileInputStream("jugador.objeto"));
      Jugador j = (Jugador ) entrada.readObject();
        entrada.close();
        return j;

   }

   
}
9  Programación / Java / Re: remove no funciona en: 19 Marzo 2021, 23:16 pm
Supongo que <cartasEnMano> es un ArrayList<> de algún tipo de objeto que has creado tú, no?

Cuando se utiliza ArrayList.remove(Object o), se elimina el objeto i del array que cumpla Objects.equals(o, array.get(i)). En resumen, necesitas sobreescribir el método equals() para determinar cuándo dos objetos son iguales ya que si no lo haces sólo serán iguales cuando sean el mismo objeto (misma dirección de memoria).

Otra solución más sencilla si ya tienes el índice es usar ArrayList.remove(int index).
Código
  1. j.cartasEnMano.remove(op)
Además de más sencillo, es más seguro pues te aseguras de tirar exactamente la misma y no otra que sea igual a esa.

PD: Utiliza las etiquetas de Código GeSHi para el código ya sea abriendo el desplegable que se llama "Código GeSHi" y eligiendo Java o escribiendo directamente:
[code=java]
Tu código Java aquí
[/code]



Hola muchas gracias. Intente eso y sigue sin funcionar. Nose que esta pasando. no me anda ningun metodo del arraylist cuando lo llamo dentro de ese metodo. Es muy raro
10  Programación / Java / remove no funciona en: 17 Marzo 2021, 00:33 am
Hola buenas noches. Tengo un problema para eliminar elementos de un arraylist.
Cuando llamo al metodo remove no funciona. No borra el elemento deseado.

Código
  1. boton.addActionListener(new ActionListener() {
  2. public void actionPerformed(ActionEvent e) {
  3. try {
  4. if (indice < miControl.jugador(nombreJ).cartasEnMano.size()) {
  5. r=indice;
  6. ImageIcon imagen = new
  7. ImageIcon(miControl.jugador(nombreJ).cartasEnMano.get(indice)+".jpg");
  8. cartaNum.setText("Carta numero: "+ indice);
  9. btn.setIcon(new
  10. ImageIcon(imagen.getImage().getScaledInstance(btn.getWidth(),btn.getHeight(),Image.SCALE_SMOOTH)));
  11. indice++;
  12.  
  13. }else {
  14. indice=0;
  15. r=0;
  16.  
  17. }
  18.  
  19. }
  20.  
  21.  
  22. catch (RemoteException e1) {
  23. // TODO Auto-generated catch block
  24. e1.printStackTrace();
  25. }
  26.  
  27. }
  28. });
  29.  
  30. boton3.addActionListener(new ActionListener() {
  31. public void actionPerformed(ActionEvent e) {
  32. try {
  33.  
  34. miControl.tirarCarta(r,miControl.jugador(nombreJ));
  35. indice=0;
  36. r=0;
  37. }
  38.  
  39. else {
  40.  
  41. }
  42. }
  43.  
  44.  
  45.  
  46. public void tirarCarta( int op, Jugador j) throws RemoteException {
  47. //op va a ser la carta a tirar y jugador va a ser el jugador que la tira
  48. mesa.add(j.cartasEnMano.get(op));
  49. j.cartasEnMano.remove(j.cartasEnMano.get(op));
  50.  
  51.  
  52. }


Si alguien me puede ayudar se lo agradeceria

MOD: Etiqueta GeSHi
Páginas: [1] 2 3 4
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines