Ver Mensaje Individual
  #1 (permalink)  
Antiguo 27/03/2016, 20:32
JOHARFLO
 
Fecha de Ingreso: diciembre-2008
Mensajes: 21
Antigüedad: 15 años, 5 meses
Puntos: 0
Problema en llenado de un JTable con datos de una tabla de una Base de Datos

Saludos a todos;

estoy intentando llenar un JTable, que esta en un formulario, con datos que tengo en una tabla de una base de datos pero me sale una Exception "java.lang.nullpointerexception"

aqui el codigo;

Código Java:
Ver original
  1. package Formularios;
  2.  
  3. import java.awt.Color;
  4.  
  5. import java.awt.Font;
  6. import java.awt.Insets;
  7.  
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.sql.Connection;
  11. import java.sql.PreparedStatement;
  12. import java.sql.ResultSet;
  13. import java.sql.ResultSetMetaData;
  14. import java.sql.Statement;
  15. import java.util.*;
  16.  
  17. import javax.swing.*;
  18. import javax.swing.border.Border;
  19. import javax.swing.table.DefaultTableModel;
  20.  
  21. import CAD.ModuloConexion; // clase donse esta la conexion a la bD
  22.  
  23.  
  24.  
  25. public class F_Clientes extends JInternalFrame implements ActionListener {
  26.    
  27.     //VAriables---------------------------
  28.    
  29.     private JTable tbBuscador;
  30.     private JScrollPane scrollTabla;
  31.     private DefaultTableModel modelo;
  32.    
  33.     private Object[][] data;
  34.    
  35.     Connection conectador=null;
  36.     PreparedStatement pst = null;
  37.     ResultSet rs=null; 
  38.  
  39.     //----------------------------------------------------------------------------     
  40.  
  41.    
  42.     // constructor
  43.     public  F_Clientes(){
  44.        
  45.         initComponents();
  46.         conectador= ModuloConexion.conexionDB();// conector a la base de datos
  47.        
  48.     }
  49.        
  50.        
  51.     //metodo para cargar la matriz----------------
  52.     public Object[][] Carga_Matriz() {
  53.         int registros=0;
  54.         String sql = "Select cliente,direccion,telefono,email FROM tbclientes ";
  55.         String sql2 = "SELECT COUNT(*) AS total FROM tbclientes";
  56.         //obtenemos la cantidad de registros existentes en la tabla
  57.         try{
  58.             pst = conectador.prepareStatement(sql2);
  59.             rs = pst.executeQuery();
  60.             rs.next();
  61.             registros = rs.getInt("total");
  62.             rs.close();
  63.         } catch (Exception e) {
  64.             JOptionPane.showMessageDialog(null, e);
  65.         }
  66.         //se crea una matriz con tantas filas y columnas que necesite
  67.         data = new Object[registros][4];
  68.         //realizamos la consulta sql y llenamos los datos en la matriz "Object"
  69.         try{
  70.             pst = conectador.prepareStatement(sql);
  71.             rs = pst.executeQuery();
  72.             int i = 0;
  73.             while(rs.next()){
  74.                 data[i][0] = rs.getString( "cliente" );
  75.                 data[i][1] = rs.getString( "direccion" );
  76.                 data[i][2] = rs.getString( "telefono" );
  77.                 data[i][3] = rs.getString( "email" );
  78.                 i++;
  79.             }
  80.             rs.close();
  81.         } catch (Exception e) {
  82.             JOptionPane.showMessageDialog(null, e);
  83.         }
  84.         return data;
  85.     }
  86.  
  87.        
  88.    
  89.     private void initComponents() {
  90.        
  91.                this.setBounds(0,25,600,500);
  92.         this.setResizable(false);
  93.         this.setTitle("Clientes-System");
  94.         this.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
  95.         this.setClosable(true);
  96.         this.setResizable(false);
  97.         this.setLayout(null);
  98.         Font fuente=new Font("Tahoma",Font.BOLD,11);
  99.        
  100.        
  101.        
  102.        
  103.  
  104.         //tabla buscador-----------------------JTable---------------------------
  105.  
  106.  
  107.         modelo = new DefaultTableModel();
  108.         tbBuscador = new JTable();
  109.         scrollTabla = new JScrollPane();
  110.  
  111.         String titulos[] = { "Cliente", "Direccion", "Telefono", "Email"};
  112.         Object informacion[][] =Carga_Matriz(); //llamada del metodo para carga de datos
  113.  
  114.         tbBuscador = new JTable(informacion, titulos);
  115.         tbBuscador.setEnabled(false);
  116.         //tbBuscador.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
  117.         scrollTabla.setBounds(50,60,500,100);
  118.         scrollTabla.setViewportView(tbBuscador);
  119.         this.add(scrollTabla);
  120.  
  121.         //----------------------------------------------------------------------------------------
  122.        
  123. }


Gracias de ante mano