Hi,
I am accessing my jtable via - sf1.setText((String)table.getValueAt(selectedRow, 1));
-
sf2.setText((String)table.getValueAt(selectedRow, 2));
-
sf3.setText((String)table.getValueAt(selectedRow, 3));
-
sf4.setText((String)table.getValueAt(selectedRow, 4));
-
sf5.setText((String)table.getValueAt(selectedRow, 5));
-
sf6.setText((String)table.getValueAt(selectedRow, 6));
-
sf7.setText((String)table.getValueAt(selectedRow, 7));
-
sf8.setText((String)table.getValueAt(selectedRow, 8));
-
sf9.setText((String)table.getValueAt(selectedRow, 9));
However the last element always returns null. I thought it something to do with sf8 when that was my last column but now it's doing it for my new last column sf9. When i look in my table there is data in the last columns and the data from sf8 is now showing after adding another column. Very confused.
Any one see what I don't?
p.s I am purposely not accessing the firstcolumn in the jtable.
5 2520 @brendanmcdonagh
Are sf1 ... sf9 JTextFields or similar? Is the 'table' variable your JTable? How many columns does its model have? Are you sure the data doesn't contain a null value for some reason?
kind regards,
Jos
ps. The piece of code you showed isn't enough to find the error.
Hi Jos,
didn't want to paste whole class as it's quite big but if your willing to scan through i would appreciate it - /*
-
* To change this template, choose Tools | Templates
-
* and open the template in the editor.
-
*/
-
-
package javaapplication1;
-
import java.awt.*;
-
import java.sql.*;
-
import java.util.*;
-
import javax.swing.JOptionPane.*;
-
import javax.swing.*;
-
import java.awt.event.*;
-
import javax.swing.event.*;
-
import javax.swing.table.TableColumn;
-
-
/**
-
*
-
* @author Admin
-
*/
-
public class GUI extends JFrame {
-
-
JTable table;
-
JMenuItem jmi;
-
-
static JTextField sf1;
-
static JTextField sf2;
-
static JTextField sf3;
-
static JTextField sf4;
-
static JTextField sf5;
-
static JTextField sf6;
-
static JTextField sf7;
-
static JTextField sf8;
-
static JTextField sf9;
-
Boolean data;
-
static JLabel tf1;
-
static JLabel tf2;
-
static JLabel tf3;
-
static JLabel tf4;
-
static JLabel tf5;
-
static JLabel tf6;
-
static JLabel tf7;
-
static JLabel tf8;
-
static JLabel tf9;
-
Statement s;
-
Connection con;
-
JPanel panel1 = new JPanel();
-
JLabel search;
-
JLabel from;
-
JTable table2;
-
JPanel panel2;
-
JPanel panel3;
-
JPanel searchPanel;
-
JButton addButton;
-
JButton repairButton;
-
JButton newButton;
-
JButton deleteButton;
-
JButton editButton;
-
JButton refreshButton;
-
JButton searchButton;
-
Vector columnNames;
-
int selectedRow;
-
JTextField searchField;
-
JComboBox combo;
-
-
-
-
public GUI(JTable aTable, Connection con2, Vector vector)
-
{
-
searchPanel = new JPanel();
-
searchPanel.setLayout(new FlowLayout());
-
columnNames = vector;
-
//setLayout(new GridLayout(1,1));
-
panel1 = new JPanel();
-
panel2 = new JPanel();
-
panel3 = new JPanel();
-
//panel1.setBorder(BorderFactory.createLineBorder(Color.black));
-
//panel2.setBorder(BorderFactory.createLineBorder(Color.black));
-
//panel3.setBorder(BorderFactory.createLineBorder(Color.black));
-
//searchPanel.setBorder(BorderFactory.createLineBorder(Color.black));
-
searchField = new JTextField(20);
-
search = new JLabel("Enter your Search criteria: ");
-
from = new JLabel("From Column: ");
-
searchPanel.add(search);
-
searchPanel.add(searchField);
-
searchPanel.add(from);
-
//combo = new JComboBox("Select Column");
-
-
JPanel panel4 = new JPanel();
-
panel1.setLayout(new GridLayout(1, 1));
-
-
panel2.setPreferredSize(new Dimension(10, 10));
-
panel2.setLayout(new GridLayout(10,2));
-
panel3.setLayout(new FlowLayout());
-
-
JMenuBar mb = new JMenuBar();
-
JMenu m = new JMenu("File");
-
-
jmi = new JMenuItem("Exit");
-
jmi.addActionListener(new ButtonWatcher());
-
m.add(jmi);
-
JMenu m2 = new JMenu("Edit");
-
m2.add(new JMenuItem("Edit"));
-
-
JMenu m3 = new JMenu("View");
-
m3.add(new JMenuItem("View"));
-
-
JMenu m4 = new JMenu("Help");
-
m4.add(new JMenuItem("Help"));
-
-
mb.add(m);
-
mb.add(m2);
-
mb.add(m3);
-
mb.add(m4);
-
setJMenuBar(mb);
-
Dimension screenSize = getToolkit().getScreenSize();
-
setSize(screenSize.width, screenSize.height);
-
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
setTitle("PC Warehouse Customer Database");
-
table = aTable;
-
-
-
table.getTableHeader().getColumnModel().getColumn(0).setMaxWidth(0);
-
table.getTableHeader().getColumnModel().getColumn(0).setMinWidth(0);
-
table.getTableHeader().getColumnModel().getColumn(0).setPreferredWidth(0);
-
table.getTableHeader().getColumnModel().getColumn(0).setResizable(false);
-
panel1.add(table);
-
-
-
// table.setCellSelectionEnabled(true);
-
ListSelectionModel cellSelectionModel = table.getSelectionModel();
-
cellSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
-
table.setRowSelectionInterval(0, 0);
-
cellSelectionModel.addListSelectionListener(new ListSelectionListener() {
-
public void valueChanged(ListSelectionEvent e) {
-
//String selectedData = null;
-
-
selectedRow = table.getSelectedRow();
-
try
-
{
-
tf1.setText((String)table.getValueAt(selectedRow, 1));
-
tf2.setText((String)table.getValueAt(selectedRow, 2));
-
tf3.setText((String)table.getValueAt(selectedRow, 3));
-
tf4.setText((String)table.getValueAt(selectedRow, 4));
-
tf5.setText((String)table.getValueAt(selectedRow, 5));
-
tf6.setText((String)table.getValueAt(selectedRow, 6));
-
tf7.setText((String)table.getValueAt(selectedRow, 7));
-
tf8.setText((String)table.getValueAt(selectedRow, 8));
-
tf9.setText((String)table.getValueAt(selectedRow, 9));
-
System.out.println((String)table.getValueAt(selectedRow, 8));
-
System.out.println("Selected: " + selectedRow);
-
}
-
catch(NullPointerException op)
-
{
-
System.out.println("empty");
-
}
-
}
-
});
-
-
-
JScrollPane sp = new JScrollPane(table);
-
con = con2;
-
try
-
{
-
s = con.createStatement();
-
}
-
catch(Exception e)
-
{
-
System.out.println(e.getMessage());
-
}
-
//s.execute("select * from customers");
-
//table.scrollRectToVisible(table.getCellRect(0,2, true));
-
addButton = new JButton("Save Customer");
-
newButton = new JButton("New Order");
-
repairButton = new JButton("New Repair");
-
editButton = new JButton("Edit Selected Customer");
-
deleteButton = new JButton("Delete Selected Customer");
-
refreshButton = new JButton("Refresh");
-
searchButton = new JButton("Search");
-
JLabel labelFirstName = new JLabel("Customer First Name:");
-
JLabel labelLastName = new JLabel("Customer Last Name:");
-
//labelName.setBorder(BorderFactory.createLineBorder(Color.black));
-
-
// new JLabel("Customer Name");
-
tf1 = new JLabel();
-
JLabel labelAddress1 = new JLabel("Customer Address:");
-
tf2 = new JLabel();
-
JLabel labelEmail = new JLabel("Customer Email:");
-
tf3 = new JLabel();
-
JLabel labelPhone = new JLabel("Customer Phone No:");
-
tf4 = new JLabel();
-
JLabel labelPurchase = new JLabel("Customer Purchase:");
-
tf5 = new JLabel();
-
JLabel labelPurchasePrice = new JLabel("Customer Purchase Price:");
-
tf6 = new JLabel();
-
JLabel labelPurchaseDate = new JLabel("Customer Purchase Date:");
-
tf7 = new JLabel();
-
JLabel labelPurchaseOrRepair = new JLabel("Purchase or Repair:");
-
tf8 = new JLabel();
-
//JScrollPane purchaseScroll = new JScrollPane(tf5);
-
//panel = new JPanel();
-
//panel.setLayout(new GridLayout(2, 2));
-
//panel1.add(sp);
-
-
-
-
//panel2.setLayout(new GridLayout(10,2));
-
panel2.add(labelFirstName);
-
panel2.add(tf1);
-
panel2.add(labelLastName);
-
panel2.add(tf2);
-
-
panel2.add(labelAddress1);
-
panel2.add(tf3);
-
panel2.add(labelEmail);
-
panel2.add(tf4);
-
panel2.add(labelPhone);
-
panel2.add(tf5);
-
panel2.add(labelPurchase);
-
panel2.add(tf6);
-
panel2.add(labelPurchasePrice);
-
panel2.add(tf7);
-
panel2.add(labelPurchaseDate);
-
panel2.add(tf8);
-
panel2.add(labelPurchaseOrRepair);
-
//panel2.add(purchaseScroll);
-
-
//panel.add(panel2);
-
-
//cp.add(panel);
-
repairButton.addActionListener(new ButtonWatcher());
-
addButton.addActionListener(new ButtonWatcher());
-
newButton.addActionListener(new ButtonWatcher());
-
editButton.addActionListener(new ButtonWatcher());
-
deleteButton.addActionListener(new ButtonWatcher());
-
refreshButton.addActionListener(new ButtonWatcher());
-
searchButton.addActionListener(new ButtonWatcher());
-
System.out.println("HERE");
-
//panel2.add(addButton);
-
panel3.add(newButton);
-
panel3.add(repairButton);
-
panel3.add(editButton);
-
panel3.add(deleteButton);
-
panel3.add(refreshButton);
-
Container cp = getContentPane();
-
cp.setLayout(new GridLayout(4,1));
-
panel1.add(sp);
-
cp.add(panel1);
-
-
combo = new JComboBox(columnNames);
-
combo.removeItemAt(0);
-
-
searchPanel.add(combo);
-
searchPanel.add(searchButton);
-
cp.add(panel2);
-
cp.add(searchPanel);
-
cp.add(panel3);
-
-
setVisible(true);
-
-
}
-
public JTable createTable()
-
{
-
String dataSourceName = "pWarehouseDatabase";
-
String dbURL = "jdbc:odbc:" + dataSourceName;
-
try {
-
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
-
Connection con = DriverManager.getConnection(dbURL, "","");
-
Statement s = con.createStatement();
-
s.execute("select * from customers ORDER BY customerLastName"); // create a table * from customers
-
//s.execute("insert into TEST12345 values(1)"); // insert some data into the table
-
//s.execute("select column_name from TEST12345"); // select the data from the table
-
ResultSet resultSet = s.getResultSet(); // get any ResultSet that came from our query
-
ResultSetMetaData metaData = resultSet.getMetaData();
-
int numberOfColumns = metaData.getColumnCount();
-
Vector columnNames = new Vector();
-
for(int column = 0; column < numberOfColumns; column++)
-
{
-
columnNames.addElement(metaData.getColumnLabel(column+1));
-
}
-
Vector rows = new Vector();
-
while (resultSet.next())
-
{
-
Vector newRow = new Vector();
-
for (int i = 1; i <= metaData.getColumnCount(); i++)
-
{
-
newRow.addElement(resultSet.getObject(i));
-
}
-
rows.addElement(newRow);
-
}
-
table = new JTable(rows, columnNames);
-
-
-
//dispose();
-
-
-
//s.close(); // close the Statement to let the database know we're done with it
-
//con.close(); // close the Connection to let the database know we're done with it
-
//GUI gui2 = new GUI(table, con);
-
}
-
-
-
catch (Exception o){
-
o.printStackTrace();
-
}
-
return table;
-
}
-
/*private class listSelectionListener implements TableModelListener
-
{
-
public void tableChanged(TableModelEvent e)
-
{
-
System.out.println("changed");
-
int rowNumber = table.getSelectedRow();
-
System.out.println(rowNumber);
-
}
-
-
}
-
*/
-
private class ButtonWatcher implements ActionListener
-
{
-
-
public void itemStateChanged(ItemEvent e) {
-
Object o = e.getSource();
-
if(o.equals(jmi))
-
{
-
System.exit(1);
-
}
-
-
}
-
-
public void actionPerformed(ActionEvent e)
-
{
-
Object obj = e.getSource();
-
if(obj.equals(jmi))
-
{
-
System.exit(1);
-
}
-
if(obj.equals(searchButton))
-
{
-
data = false;
-
String keyword = searchField.getText();
-
String columnSearch = (String)combo.getSelectedItem();
-
try
-
{
-
//s.close();
-
//con.close();
-
String dbURL = "jdbc:odbc:" + "pWarehouseDatabase";
-
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
-
Connection con = DriverManager.getConnection(dbURL, "","");
-
Statement s = con.createStatement();
-
// s.execute("INSERT INTO customers//(name, address, email, phone, purchase)");
-
//System.out.println(columnSearch + keyword);
-
//s.execute("INSERT INTO customers values(?,'" +name + "','" + address + "','" + email + "','" + phone + "','" + purchase + "')");
-
//String query = "INSERT INTO customers(id, customerName, customerAddress, customerEmail, customerNumber, customerPurchase) values(?, '" +name + "','" + address + "','" + email + "','" + phone + "','" + purchase + "')";
-
//s.execute("SELECT * FROM customers WHERE '" + columnSearch + "' = '" + keyword + "'");
-
String query = "SELECT * FROM customers WHERE ("+columnSearch+" = '"+keyword+"')";
-
s.execute(query);
-
//s.execute("SELECT * from customers");
-
//s.execute("SELECT * FROM customers WHERE 'customerFirstName' = 'hope'");
-
ResultSet resultSet = s.getResultSet(); // get any ResultSet that came from our query
-
-
ResultSetMetaData metaData = resultSet.getMetaData();
-
int numberOfColumns = metaData.getColumnCount();
-
Vector columnNames = new Vector();
-
for(int column = 0; column < numberOfColumns; column++)
-
{
-
columnNames.addElement(metaData.getColumnLabel(column+1));
-
//System.out.print(test);
-
}
-
Vector rows = new Vector();
-
for (int i = 1; i <= metaData.getColumnCount(); i++)
-
{
-
//newRow.addElement(resultSet.getObject(i));
-
-
}
-
while (resultSet.next())
-
{
-
Vector newRow = new Vector();
-
data = true;
-
-
-
for (int i = 1; i <= metaData.getColumnCount(); i++)
-
{
-
newRow.addElement(resultSet.getObject(i));
-
-
}
-
rows.addElement(newRow);
-
-
}
-
-
table2 = new JTable(rows, columnNames);
-
//System.out.println("Not nulls - rows" + rows.toString());
-
-
-
-
//System.out.println("WORK " + table);
-
//GUI gui = new GUI(table,con, columnNames);
-
s.close(); // close the Statement to let the database know we're done with it
-
con.close();
-
if(data)
-
{
-
dispose();
-
-
SearchResults results = new SearchResults(table2);
-
}
-
else
-
{
-
JFrame frame = new JFrame();
-
JOptionPane.showMessageDialog(frame, "Your search for " + keyword + " returned no matches", "Search Result", JOptionPane.PLAIN_MESSAGE);
-
}
-
//results.setVisible(true);
-
-
}
-
catch(Exception p)
-
{
-
System.out.println(p.getMessage());
-
}
-
-
-
-
-
-
-
}
-
if(obj.equals(addButton))
-
{
-
String name = tf1.getText();
-
String address = tf2.getText();
-
String email = tf3.getText();
-
String phone = tf4.getText();
-
String purchase = tf5.getText();
-
String id = "NULL";
-
try
-
{
-
s.close();
-
con.close();
-
String dbURL = "jdbc:odbc:" + "pWarehouseDatabase";
-
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
-
Connection con = DriverManager.getConnection(dbURL, "","");
-
Statement s = con.createStatement();
-
// s.execute("INSERT INTO customers//(name, address, email, phone, purchase)");
-
-
//s.execute("INSERT INTO customers values(?,'" +name + "','" + address + "','" + email + "','" + phone + "','" + purchase + "')");
-
//String query = "INSERT INTO customers(id, customerName, customerAddress, customerEmail, customerNumber, customerPurchase) values(?, '" +name + "','" + address + "','" + email + "','" + phone + "','" + purchase + "')";
-
s.execute("INSERT INTO customers(customerName, customerAddress, customerEmail, customerNumber, customerPurchase) values('" +name + "','" + address + "','" + email + "','" + phone + "','" + purchase + "')");
-
-
-
//s.execute("UPDATE customers SET customerName = '" +name + "' WHERE customerName = '" +name + "'");
-
//Statement s = connection.createStatement();
-
//s.executeUpdate(sql);
-
//id++;
-
s.close();
-
-
-
-
-
tf1.setText("");
-
tf2.setText("");
-
tf3.setText("");
-
tf4.setText("");
-
tf5.setText("");
-
}
-
catch(Exception a)
-
{
-
System.out.println(a.getMessage());
-
}
-
}
-
if(obj.equals(newButton))
-
{
-
//JPanel panel2 = new JPanel(panel2);
-
try
-
{
-
String dbURL = "jdbc:odbc:" + "pWarehouseDatabase";
-
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
-
Connection con = DriverManager.getConnection(dbURL, "","");
-
Statement s = con.createStatement();
-
NewFrame newFrame = new NewFrame(con);
-
// newFrame.setUndecorated(true);
-
-
newFrame.setVisible(true);
-
-
s.close();
-
dispose();
-
//setVisible(false);
-
}
-
catch(Exception t)
-
{
-
-
}
-
-
}
-
-
if(obj.equals(deleteButton))
-
{
-
-
try
-
{
-
String dbURL = "jdbc:odbc:" + "pWarehouseDatabase";
-
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
-
Connection con = DriverManager.getConnection(dbURL, "","");
-
Statement s = con.createStatement();
-
-
Object key = table.getValueAt(selectedRow, 0);
-
String test = key.toString();
-
int number = Integer.parseInt(test);
-
//int test = (int)key;
-
int response;
-
-
response = JOptionPane.showConfirmDialog(null, "Are you sure you want to delete this contact?");
-
if (response == JOptionPane.YES_OPTION)
-
{
-
-
System.out.println(number);
-
-
-
String strSQL = "DELETE FROM customers WHERE id =" + number;
-
-
-
//dispose();
-
s.executeUpdate(strSQL);
-
System.out.println(" rows effected");
-
}
-
-
// s.execute("DELETE FROM customers(customerName, customerAddress, customerEmail, customerNumber, customerPurchase) WHERE(id = 'number')";
-
-
-
-
/*
-
s.close();
-
con.close();
-
String dbURL2 = "jdbc:odbc:" + "pWarehouseDatabase";
-
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
-
Connection con2 = DriverManager.getConnection(dbURL2, "","");
-
Statement s2 = con2.createStatement();
-
ResultSet resultSet2 = s2.getResultSet(); // get any ResultSet that came from our query
-
ResultSetMetaData metaData2 = resultSet2.getMetaData();
-
int numberOfColumns2 = metaData2.getColumnCount();
-
Vector columnNames2 = new Vector();
-
for(int column2 = 0; column2 < numberOfColumns2; column2++)
-
{
-
columnNames2.addElement(metaData2.getColumnLabel(column2+1));
-
}
-
Vector rows2 = new Vector();
-
while (resultSet2.next())
-
{
-
Vector newRow2 = new Vector();
-
for (int i = 1; i <= metaData2.getColumnCount(); i++)
-
{
-
newRow2.addElement(resultSet2.getObject(i));
-
}
-
rows2.addElement(newRow2);
-
}
-
table = new JTable(rows2, columnNames2);*/
-
-
table = createTable();
-
GUI gui = new GUI(table,con, columnNames);
-
-
//GUI gui = new GUI( table, con);
-
//gui.setVisible(true);
-
-
dispose();
-
System.out.println("dispose");
-
-
-
}
-
/* catch(SQLException s){
-
System.out.println("SQL statement is not executed!");
-
}
-
*/
-
catch (Exception o){
-
o.printStackTrace();
-
}
-
-
-
-
}
-
if(obj.equals(repairButton))
-
{
-
-
NewRepair repair = new NewRepair(con);
-
repair.setVisible(true);
-
dispose();
-
}
-
if(obj.equals(editButton))
-
{
-
dispose();
-
Object key = table.getValueAt(selectedRow, 0);
-
String test = key.toString();
-
int number = Integer.parseInt(test);
-
System.out.println(number);
-
//panel2.remove(newButton);
-
// panel2.remove(editButton);
-
//panel2.remove(deleteButton);
-
//panel2.remove(refreshButton);
-
//dispose();
-
JPanel innerPanel;
-
//innerPanel.setLayout(new GridLayout(15,2));
-
sf1 = new JTextField();
-
sf2 = new JTextField();
-
sf3 = new JTextField();
-
sf4 = new JTextField();
-
sf5 = new JTextField();
-
sf6 = new JTextField();
-
sf7 = new JTextField();
-
sf8 = new JTextField();
-
sf9 = new JTextField();
-
System.out.println("EDITING");
-
try
-
{
-
sf1.setText((String)table.getValueAt(selectedRow, 1));
-
sf2.setText((String)table.getValueAt(selectedRow, 2));
-
sf3.setText((String)table.getValueAt(selectedRow, 3));
-
sf4.setText((String)table.getValueAt(selectedRow, 4));
-
sf5.setText((String)table.getValueAt(selectedRow, 5));
-
sf6.setText((String)table.getValueAt(selectedRow, 6));
-
sf7.setText((String)table.getValueAt(selectedRow, 7));
-
sf8.setText((String)table.getValueAt(selectedRow, 8));
-
sf9.setText((String)table.getValueAt(selectedRow, 9));
-
}
-
catch(Exception er)
-
{
-
System.out.println("This one is empty too");
-
}
-
//System.out.println(table.getValueAt(selectedRow, 8));
-
innerPanel = new JPanel();
-
innerPanel.setLayout(new GridLayout(15,2));
-
innerPanel.add(new JLabel("Customer First Name"));
-
//innerPanel.add(addButton);
-
//innerPanel.add(newButton);
-
innerPanel.add(sf1);
-
innerPanel.add(new JLabel("Customer Last Name"));
-
innerPanel.add(sf2);
-
-
innerPanel.add(new JLabel("Customer Address"));
-
innerPanel.add(sf3);
-
innerPanel.add(new JLabel("Customer Email"));
-
innerPanel.add(sf4);
-
innerPanel.add(new JLabel("Customer Phone"));
-
innerPanel.add(sf5);
-
innerPanel.add(new JLabel("Customer Purchase"));
-
innerPanel.add(sf6);
-
innerPanel.add(new JLabel("Customer Purchase Price"));
-
innerPanel.add(sf7);
-
innerPanel.add(new JLabel("Customer Purchase Date"));
-
innerPanel.add(sf8);
-
innerPanel.add(new JLabel("Purchase or Repair"));
-
innerPanel.add(sf9);
-
//innerPanel.add(purchaseScroll);
-
//innerPanel.add(addButton);
-
//innerPanel.add(cancelButton);
-
//add(innerPanel);
-
EditFrame editFrame = new EditFrame(number, innerPanel, sf1, sf2, sf3, sf4, sf5, sf6, sf7, sf8, sf9);
-
}
-
-
if(obj.equals(refreshButton))
-
{
-
try
-
{
-
-
//s.close();
-
-
//con.close();
-
System.out.println("here i AM");
-
String dbURL = "jdbc:odbc:" + "pWarehouseDatabase";
-
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
-
con = DriverManager.getConnection(dbURL, "","");
-
s = con.createStatement();
-
-
s.execute("select * from customers ORDER BY customerLastName"); // create a table * from customers
-
//s.execute("insert into TEST12345 values(1)"); // insert some data into the table
-
//s.execute("select column_name from TEST12345"); // select the data from the table
-
ResultSet resultSet = s.getResultSet(); // get any ResultSet that came from our query
-
ResultSetMetaData metaData = resultSet.getMetaData();
-
int numberOfColumns = metaData.getColumnCount();
-
Vector columnNames = new Vector();
-
for(int column = 0; column < numberOfColumns; column++)
-
{
-
columnNames.addElement(metaData.getColumnLabel(column+1));
-
}
-
Vector rows = new Vector();
-
while (resultSet.next())
-
{
-
Vector newRow = new Vector();
-
for (int i = 1; i <= metaData.getColumnCount(); i++)
-
{
-
newRow.addElement(resultSet.getObject(i));
-
}
-
rows.addElement(newRow);
-
}
-
JTable table = new JTable(rows, columnNames);
-
dispose();
-
GUI gui = new GUI( table, con, columnNames);
-
gui.setVisible(true);
-
}
-
catch(Exception i)
-
{
-
-
}
-
}
-
-
}
-
}
-
}
-
I really hope you haven't gone through all that - I just scanned through it noticed the label to show the last column wasn't initialized - doh!
Sorry to waste your time Jos
@brendanmcdonagh
I just skimmed through it, no need to say sorry; here's a little tip: don't just do this: -
try {
-
...
-
catch (Exception e) {
-
System.out.println("A short message");
-
}
-
... but do this instead: -
try {
-
...
-
catch (Exception e) {
-
e.printStackTrace();
-
}
-
You're still debugging and those exceptions are not to be thrown; if they are you want to know all about ithem, including the cause of them. The stack trace shows you exactly what happened and where it happened.
kind regards,
Jos
cheers for that Jos, I'll be using your recommendation from now on.
Regards
Brendan
Sign in to post your reply or Sign up for a free account.
Similar topics
by: K S Aldebaraan |
last post by:
I'm trying to submit a form with an action of a servlet, and a view
equal to the same jsp page. I'm not sure what I'm doing wrong, but
keep getting a NullPointerException on the second line of...
|
by: gabryh |
last post by:
Hi,
The following code throws me NullPointerException.
.....
public static boolean isEmpty(String value) {
return ((value == null) || (value.trim().equals("")));
}
|
by: Old-timer |
last post by:
Not sure where else to post this.
I'm sure I'm doing something wrong, but I wouldn't think a simple app
would give me so much trouble. I've got a small test java class that
I'm trying to have...
|
by: Alan Krueger |
last post by:
Greetings,
I've been able to cache Transformer objects in a Tomcat-based servlet
application to avoid unnecessary Transformer rebuilding, except for
certain ones on certain machines. I'm...
|
by: oll3i |
last post by:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Zad21.Cennik.getCenna(Cennik.java)
at Zad21.KwiaciarniaView$1.actionPerformed(KwiaciarniaView.java)
at...
|
by: oll3i |
last post by:
private List<Klient> klienci;
m_add_client.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
...
|
by: ketand1 |
last post by:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import java.lang.*;
class DbAwt extends Frame implements ActionListener
{
private...
|
by: sokeefe |
last post by:
I am trying to edit the GUI of a project in Netbeans. In particular, I am trying to add new JButtons. I get a NullPointerException when I try to add an Event to any given JButton (even ones that...
|
by: sokeefe |
last post by:
I am trying to edit the GUI of a project in Netbeans. In particular, I am trying to add new JButtons. I get a NullPointerException when I try to add an Event to any given JButton (even ones that...
|
by: chris123456789 |
last post by:
Hi, when I run my code I get a NullPointerException:null. Here is the part of the code where the error occurs:
import java.util.*;
import java.io.*;
public class Decrypt
{
...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |