I have a small problem with a program that I am developing. In one of the windows to which I arrive, I must choose a level, and it depends on the level I choose, it thunders, showing me an error of index = 0; and size = 0
I do not know what may be failing. Help me.
At the time I click on level 2 or 3, this fails, returning this error:
However, when it is level 1, it works correctly, accessing the following frame:
The part of the code is the following: This is where the levels are chosen
public ventanaNivelesEjercicio() {
initComponents();
this.setLocationRelativeTo(null);
this.setTitle("Nivel de los ejercicios");
int idBotonComparaPreguntas = 1;
}
private void btnAccederNivel1ActionPerformed(java.awt.event.ActionEvent evt) {
ventanaQuiz vQuiz = new ventanaQuiz( 1);
vQuiz.setVisible(true);
this.dispose();
}
private void btnAccederNivel2ActionPerformed(java.awt.event.ActionEvent evt) {
ventanaQuiz vQuiz = new ventanaQuiz(2);
vQuiz.setVisible(true);
this.dispose();
}
private void btnAccederNivel3ActionPerformed(java.awt.event.ActionEvent evt) {
ventanaQuiz vQuiz = new ventanaQuiz(3);
vQuiz.setVisible(true);
this.dispose();
}
private void btnRegresarOpcionesAlumnoActionPerformed(java.awt.event.ActionEvent evt) {
ventanaOpcionesAlumno vOAlumno = new ventanaOpcionesAlumno();
vOAlumno.setVisible(true);
this.dispose();
}
Class where the questions are loaded and displayed in the Frame:
public List<Pregunta> preguntas;
Pregunta pregunta;
int posicion = 0;
DatosQuiz datosQuiz;
public ventanaQuiz(int nivel) {
datosQuiz = new DatosQuiz();
this.posicion = 0;
pregunta = null;
this.preguntas = datosQuiz.getPreguntas(nivel);
initComponents();
this.setLocationRelativeTo(null);
this.setTitle("Preguntas Quiz");
btnAvanzar.setVisible(true);
btnRegresar.setVisible(true);
cargarPregunta();
}
private void cargarPregunta() {
btnGrupoRespuesta.clearSelection();
pregunta = preguntas.get(posicion);
lblPreguntaMostrada.setText(pregunta.getText());
rbtnOpcionRespuesta1.setText(pregunta.respuestas.get(0).getText());
rbtnOpcionRespuesta1.setSelected(pregunta.respuestas.get(0).Selected);
rbtnOpcionRespuesta2.setText(pregunta.respuestas.get(1).getText());
rbtnOpcionRespuesta2.setSelected(pregunta.respuestas.get(1).Selected);
rbtnOpcionRespuesta3.setText(pregunta.respuestas.get(2).getText());
rbtnOpcionRespuesta3.setSelected(pregunta.respuestas.get(2).Selected);
rbtnOpcionRespuesta4.setText(pregunta.respuestas.get(3).getText());
rbtnOpcionRespuesta4.setSelected(pregunta.respuestas.get(3).Selected);
}
Here the questions are sent to the DB and the whole roll is made:
public List<Pregunta> getPreguntas(int Nivel) {
//TODO: Obtener las 10 primeras preguntas
Conexion conexion = new Conexion();
Connection connection = conexion.obtenerConexion();
List<Pregunta> listaPreguntas = new ArrayList<Pregunta>();
try {
PreparedStatement preparedstatement = connection.prepareStatement(" SELECT * FROM ejercicios WHERE nivel = ? ");
preparedstatement.setInt(1, Nivel);
ResultSet rs = preparedstatement.executeQuery();
while (rs.next()) {
Pregunta pregunta = new Pregunta();
pregunta.setId(rs.getInt("numEjercicio"));
pregunta.setLevel(rs.getInt("nivel"));
pregunta.setText(rs.getString("descripcion"));
PreparedStatement preparedstatement2 = connection.prepareStatement(" SELECT * FROM respuestas WHERE numEjercicio = ? ");
preparedstatement2.setInt(1, pregunta.getId());
ResultSet rs2 = preparedstatement2.executeQuery();
ArrayList<Respuesta> respuestas = new ArrayList<>();
while (rs2.next()) {
Respuesta respuesta = new Respuesta();
respuesta.isCorrect = rs2.getBoolean("isCorrecta");
respuesta.Text = rs2.getString("valor");
respuesta.Selected = false;
respuestas.add(respuesta);
}
pregunta.respuestas = respuestas;
listaPreguntas.add(pregunta);
}
} catch (Exception e) {
System.out.println("Algo paso y yo no se que fue" + e.getMessage());
}
return listaPreguntas;
}