Ver Mensaje Individual
  #1 (permalink)  
Antiguo 04/08/2014, 17:52
Avatar de lufe
lufe
 
Fecha de Ingreso: mayo-2009
Mensajes: 294
Antigüedad: 15 años
Puntos: 15
Mostrar registros de una BD, ejercicio estoy trancada

Hola chicos, estoy tratando de aprender y siguiendo el tutorial de http://www.homeandlearn.co.uk/java/d...g_buttons.html logré finalmente hacer andar una base de datos y mostrar un registro!

El tema es que al hacer clic en el botón Next me tira un error del tipo:
Cita:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Employees.Workers.btnNextActionPerformed(Workers.j ava:218)
que según entiendo se debe a que la variable rs es nula Null cuando el programa entra al try

Sin embargo, esa variable debería estar bien porque el primer registro SI lo muestra, o sea que tenía un valor adentro. No me doy cuenta como solucionarlo y el tutorial no dice más nada.

Les pego el código que tengo de las partes fundamentales, no me deja pegar TODO el código:

Donde se define la variable rs
Código:
/*
package Employees;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;

/**
 *
 * @author lufe
 */
public class Workers extends javax.swing.JFrame {

    /**
     * Creates new form Workers
     */
    
    Connection con;
    Statement stmt;
    ResultSet rs;
    
    public Workers() {
        initComponents();
        DoConnect();
    }
y la acción que ejecuta al hacer clic en NEXT

Código:
private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        System.out.println("Por entrar al Try");
        System.out.println(rs);
        try {
            if ( rs.next( ) ) {
                int id_col = rs.getInt("ID");
                String id = Integer.toString(id_col);
                String first_name = rs.getString("First_Name");
                String last_name = rs.getString("Last_Name");
                String job = rs.getString("Job_Title");
                
                textID.setText(id);
                textFirstName.setText(first_name);
                textLastName.setText(last_name);
                textJobTitle.setText(job);
            }
        else {
            rs.previous( );
            JOptionPane.showMessageDialog(Workers.this, "End of File");
        }
            }
        catch (SQLException err) {
            JOptionPane.showMessageDialog(Workers.this, err.getMessage());
        }
    }
Las líneas

System.out.println("Por entrar al Try");
System.out.println(rs);

las agregué yo misma para verificar que rs entra valiendo NULL

No me doy cuenta como solucionarlo