473,406 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

false NULLPOINTEREXCEPTION?

153 100+
Hi,

I am accessing my jtable via

Expand|Select|Wrap|Line Numbers
  1. sf1.setText((String)table.getValueAt(selectedRow, 1));
  2.        sf2.setText((String)table.getValueAt(selectedRow, 2));
  3.        sf3.setText((String)table.getValueAt(selectedRow, 3));
  4.        sf4.setText((String)table.getValueAt(selectedRow, 4));
  5.        sf5.setText((String)table.getValueAt(selectedRow, 5));
  6.        sf6.setText((String)table.getValueAt(selectedRow, 6));
  7.        sf7.setText((String)table.getValueAt(selectedRow, 7));
  8.        sf8.setText((String)table.getValueAt(selectedRow, 8));
  9.        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.
Jun 21 '09 #1
5 2520
JosAH
11,448 Expert 8TB
@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.
Jun 21 '09 #2
brendanmcdonagh
153 100+
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

Expand|Select|Wrap|Line Numbers
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package javaapplication1;
  7. import java.awt.*;
  8. import java.sql.*;
  9. import java.util.*;
  10. import javax.swing.JOptionPane.*;
  11. import javax.swing.*;
  12. import java.awt.event.*;
  13. import javax.swing.event.*;
  14. import javax.swing.table.TableColumn;
  15.  
  16. /**
  17.  *
  18.  * @author Admin
  19.  */
  20. public class GUI extends JFrame {
  21.  
  22.     JTable table;
  23. JMenuItem jmi;
  24.  
  25.         static JTextField sf1;
  26.         static JTextField sf2;
  27.         static JTextField sf3;
  28.         static JTextField sf4;
  29.         static JTextField sf5;
  30.         static JTextField sf6;
  31.         static JTextField sf7;
  32.         static JTextField sf8;
  33.         static JTextField sf9;
  34.     Boolean data;
  35.     static JLabel tf1;
  36.     static JLabel tf2;
  37.     static JLabel tf3;
  38.     static JLabel tf4;
  39.     static JLabel tf5;
  40.     static JLabel tf6;
  41.     static JLabel tf7;
  42.     static JLabel tf8;
  43.     static JLabel tf9;
  44. Statement s;
  45. Connection con;
  46.     JPanel panel1 = new JPanel();
  47.     JLabel search;
  48.     JLabel from;
  49. JTable table2;
  50.     JPanel panel2;
  51.     JPanel panel3;
  52.     JPanel searchPanel;
  53.     JButton addButton;
  54.     JButton repairButton;
  55.     JButton newButton;
  56.     JButton deleteButton;
  57.     JButton editButton;
  58.     JButton refreshButton;
  59.     JButton searchButton;
  60.     Vector columnNames;
  61. int selectedRow;
  62. JTextField searchField;
  63. JComboBox combo;
  64.  
  65.  
  66.  
  67.     public GUI(JTable aTable, Connection con2, Vector vector)
  68.     {
  69.         searchPanel = new JPanel();
  70.         searchPanel.setLayout(new FlowLayout());
  71.         columnNames = vector;
  72.         //setLayout(new GridLayout(1,1));
  73.         panel1 = new  JPanel();
  74.         panel2 = new  JPanel();
  75.         panel3 = new  JPanel();
  76.         //panel1.setBorder(BorderFactory.createLineBorder(Color.black));
  77. //panel2.setBorder(BorderFactory.createLineBorder(Color.black));
  78. //panel3.setBorder(BorderFactory.createLineBorder(Color.black));
  79. //searchPanel.setBorder(BorderFactory.createLineBorder(Color.black));
  80.         searchField = new JTextField(20);
  81.         search = new JLabel("Enter your Search criteria: ");
  82.         from = new JLabel("From Column: ");
  83.         searchPanel.add(search);
  84.         searchPanel.add(searchField);
  85.         searchPanel.add(from);
  86.         //combo = new JComboBox("Select Column");
  87.  
  88.         JPanel panel4 = new JPanel();
  89.         panel1.setLayout(new GridLayout(1, 1));
  90.  
  91.         panel2.setPreferredSize(new Dimension(10, 10));
  92.      panel2.setLayout(new GridLayout(10,2));
  93.       panel3.setLayout(new FlowLayout());
  94.  
  95.         JMenuBar mb = new JMenuBar();
  96.         JMenu m = new JMenu("File");
  97.  
  98.                  jmi = new JMenuItem("Exit");
  99.                 jmi.addActionListener(new ButtonWatcher());
  100.                 m.add(jmi);
  101.         JMenu m2 = new JMenu("Edit");
  102.         m2.add(new JMenuItem("Edit"));
  103.  
  104.         JMenu m3 = new JMenu("View");
  105.         m3.add(new JMenuItem("View"));
  106.  
  107.         JMenu m4 = new JMenu("Help");
  108.         m4.add(new JMenuItem("Help"));
  109.  
  110.         mb.add(m);
  111.         mb.add(m2);
  112.         mb.add(m3);
  113.         mb.add(m4);
  114.         setJMenuBar(mb);
  115.        Dimension screenSize = getToolkit().getScreenSize();
  116. setSize(screenSize.width, screenSize.height);
  117. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  118.         setTitle("PC Warehouse Customer Database");
  119.         table = aTable;
  120.  
  121.  
  122.         table.getTableHeader().getColumnModel().getColumn(0).setMaxWidth(0);
  123.     table.getTableHeader().getColumnModel().getColumn(0).setMinWidth(0);
  124.     table.getTableHeader().getColumnModel().getColumn(0).setPreferredWidth(0);
  125.     table.getTableHeader().getColumnModel().getColumn(0).setResizable(false);
  126. panel1.add(table);
  127.  
  128.  
  129.        // table.setCellSelectionEnabled(true);
  130.     ListSelectionModel cellSelectionModel = table.getSelectionModel();
  131.     cellSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  132. table.setRowSelectionInterval(0, 0);
  133.     cellSelectionModel.addListSelectionListener(new ListSelectionListener() {
  134.       public void valueChanged(ListSelectionEvent e) {
  135.         //String selectedData = null;
  136.  
  137.         selectedRow = table.getSelectedRow();
  138.         try
  139.         {
  140.        tf1.setText((String)table.getValueAt(selectedRow, 1));
  141.        tf2.setText((String)table.getValueAt(selectedRow, 2));
  142.        tf3.setText((String)table.getValueAt(selectedRow, 3));
  143.        tf4.setText((String)table.getValueAt(selectedRow, 4));
  144.        tf5.setText((String)table.getValueAt(selectedRow, 5));
  145.        tf6.setText((String)table.getValueAt(selectedRow, 6));
  146.        tf7.setText((String)table.getValueAt(selectedRow, 7));
  147.        tf8.setText((String)table.getValueAt(selectedRow, 8));
  148.        tf9.setText((String)table.getValueAt(selectedRow, 9));
  149.        System.out.println((String)table.getValueAt(selectedRow, 8));
  150.        System.out.println("Selected: " + selectedRow);
  151.       }
  152.         catch(NullPointerException op)
  153.         {
  154.             System.out.println("empty");
  155.         }
  156.       }
  157.     });
  158.  
  159.  
  160.         JScrollPane sp = new JScrollPane(table);
  161.       con = con2;
  162.       try
  163.       {
  164.       s = con.createStatement();
  165.       }
  166.       catch(Exception e)
  167.       {
  168.           System.out.println(e.getMessage());
  169.       }
  170.         //s.execute("select * from customers");
  171.          //table.scrollRectToVisible(table.getCellRect(0,2, true));
  172.         addButton = new JButton("Save Customer");
  173.         newButton = new JButton("New Order");
  174.         repairButton = new JButton("New Repair");
  175.         editButton = new JButton("Edit Selected Customer");
  176.         deleteButton = new JButton("Delete Selected Customer");
  177.         refreshButton = new JButton("Refresh");
  178.          searchButton = new JButton("Search");
  179.         JLabel labelFirstName = new JLabel("Customer First Name:");
  180.         JLabel labelLastName = new JLabel("Customer Last Name:");
  181.         //labelName.setBorder(BorderFactory.createLineBorder(Color.black));
  182.  
  183.    // new JLabel("Customer Name");
  184.         tf1 = new JLabel();
  185.         JLabel labelAddress1 = new JLabel("Customer Address:");
  186.          tf2 = new JLabel();
  187.          JLabel labelEmail = new JLabel("Customer Email:");
  188.          tf3 = new JLabel();
  189.         JLabel labelPhone = new JLabel("Customer Phone No:");
  190.          tf4 = new JLabel();
  191.         JLabel labelPurchase = new JLabel("Customer Purchase:");
  192.          tf5 = new JLabel();
  193.          JLabel labelPurchasePrice = new JLabel("Customer Purchase Price:");
  194.          tf6 = new JLabel();
  195.          JLabel labelPurchaseDate = new JLabel("Customer Purchase Date:");
  196.          tf7 = new JLabel();
  197.          JLabel labelPurchaseOrRepair = new JLabel("Purchase or Repair:");
  198.          tf8 = new JLabel();
  199.          //JScrollPane purchaseScroll = new JScrollPane(tf5);
  200.         //panel = new JPanel();
  201.         //panel.setLayout(new GridLayout(2, 2));
  202.         //panel1.add(sp);
  203.  
  204.  
  205.  
  206.         //panel2.setLayout(new GridLayout(10,2));
  207.         panel2.add(labelFirstName);
  208.         panel2.add(tf1);
  209.         panel2.add(labelLastName);
  210.         panel2.add(tf2);
  211.  
  212.          panel2.add(labelAddress1);
  213.         panel2.add(tf3);
  214.         panel2.add(labelEmail);
  215.         panel2.add(tf4);
  216.         panel2.add(labelPhone);
  217.         panel2.add(tf5);
  218.         panel2.add(labelPurchase);
  219.         panel2.add(tf6);
  220.         panel2.add(labelPurchasePrice);
  221.         panel2.add(tf7);
  222.         panel2.add(labelPurchaseDate);
  223.         panel2.add(tf8);
  224.         panel2.add(labelPurchaseOrRepair);
  225.         //panel2.add(purchaseScroll);
  226.  
  227.         //panel.add(panel2);
  228.  
  229.         //cp.add(panel);
  230.         repairButton.addActionListener(new ButtonWatcher());
  231.         addButton.addActionListener(new ButtonWatcher());
  232.         newButton.addActionListener(new ButtonWatcher());
  233.         editButton.addActionListener(new ButtonWatcher());
  234.         deleteButton.addActionListener(new ButtonWatcher());
  235.         refreshButton.addActionListener(new ButtonWatcher());
  236.         searchButton.addActionListener(new ButtonWatcher());
  237.         System.out.println("HERE");
  238.         //panel2.add(addButton);
  239.         panel3.add(newButton);
  240.         panel3.add(repairButton);
  241.         panel3.add(editButton);
  242.         panel3.add(deleteButton);
  243.         panel3.add(refreshButton);
  244.         Container cp = getContentPane();
  245.         cp.setLayout(new GridLayout(4,1));
  246.         panel1.add(sp);
  247.        cp.add(panel1);
  248.  
  249.        combo = new JComboBox(columnNames);
  250.        combo.removeItemAt(0);
  251.  
  252. searchPanel.add(combo);
  253. searchPanel.add(searchButton);
  254.         cp.add(panel2);
  255.         cp.add(searchPanel);
  256.         cp.add(panel3);
  257.  
  258.         setVisible(true);
  259.  
  260.     }
  261.     public JTable createTable()
  262.     {
  263.         String dataSourceName = "pWarehouseDatabase";
  264.         String dbURL = "jdbc:odbc:" + dataSourceName;
  265.         try {
  266.         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  267.         Connection con = DriverManager.getConnection(dbURL, "","");
  268.         Statement s = con.createStatement();
  269.         s.execute("select * from customers ORDER BY customerLastName"); // create a table * from customers
  270.         //s.execute("insert into TEST12345 values(1)"); // insert some data into the table
  271.         //s.execute("select column_name from TEST12345"); // select the data from the table
  272.         ResultSet resultSet = s.getResultSet(); // get any ResultSet that came from our query
  273.         ResultSetMetaData metaData = resultSet.getMetaData();
  274.                int numberOfColumns =  metaData.getColumnCount();
  275.                Vector columnNames = new Vector();
  276.                for(int column = 0; column < numberOfColumns; column++)
  277.                {
  278.                      columnNames.addElement(metaData.getColumnLabel(column+1));
  279.                }
  280.                Vector rows = new Vector();
  281.                while (resultSet.next())
  282.                {
  283.                     Vector newRow = new Vector();
  284.                     for (int i = 1; i <= metaData.getColumnCount(); i++)
  285.                     {
  286.                          newRow.addElement(resultSet.getObject(i));
  287.                     }
  288.                     rows.addElement(newRow);
  289.                }
  290.                 table = new JTable(rows, columnNames);
  291.  
  292.  
  293.                //dispose();
  294.  
  295.  
  296.         //s.close(); // close the Statement to let the database know we're done with it
  297.         //con.close(); // close the Connection to let the database know we're done with it
  298.             //GUI gui2 = new GUI(table, con);
  299.     }
  300.  
  301.  
  302.         catch (Exception o){
  303.       o.printStackTrace();
  304.     }
  305.    return table;
  306. }
  307.     /*private class listSelectionListener implements TableModelListener
  308.     {
  309.         public void tableChanged(TableModelEvent e)
  310.         {
  311.             System.out.println("changed");
  312.             int rowNumber = table.getSelectedRow();
  313.             System.out.println(rowNumber);
  314.         }
  315.  
  316.     }
  317. */
  318.     private class ButtonWatcher implements ActionListener
  319.     {
  320.  
  321.          public void itemStateChanged(ItemEvent e) {
  322.              Object o = e.getSource();
  323.              if(o.equals(jmi))
  324.              {
  325.         System.exit(1);
  326.     }
  327.  
  328.          }
  329.  
  330.         public void actionPerformed(ActionEvent e)
  331.         {
  332.             Object obj = e.getSource();
  333.             if(obj.equals(jmi))
  334.              {
  335.         System.exit(1);
  336.     }
  337.             if(obj.equals(searchButton))
  338.              {
  339.                 data = false;
  340.              String keyword = searchField.getText();
  341.              String columnSearch = (String)combo.getSelectedItem();
  342.              try
  343.                 {
  344.                     //s.close();
  345.                     //con.close();
  346.                     String dbURL = "jdbc:odbc:" + "pWarehouseDatabase";
  347.                     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  348.         Connection con = DriverManager.getConnection(dbURL, "","");
  349.         Statement s = con.createStatement();
  350.                // s.execute("INSERT INTO customers//(name, address, email, phone, purchase)");
  351. //System.out.println(columnSearch + keyword);
  352.             //s.execute("INSERT INTO customers values(?,'" +name + "','" + address + "','" + email + "','" + phone + "','" + purchase + "')");
  353.            //String query = "INSERT INTO customers(id, customerName, customerAddress, customerEmail, customerNumber, customerPurchase) values(?, '" +name + "','" + address + "','" + email + "','" + phone + "','" + purchase + "')";
  354. //s.execute("SELECT * FROM customers WHERE '" + columnSearch + "' = '" + keyword + "'");
  355. String query = "SELECT * FROM customers WHERE ("+columnSearch+" = '"+keyword+"')";
  356. s.execute(query);
  357.         //s.execute("SELECT * from customers");
  358. //s.execute("SELECT * FROM customers WHERE 'customerFirstName' = 'hope'");
  359. ResultSet resultSet = s.getResultSet(); // get any ResultSet that came from our query
  360.  
  361.         ResultSetMetaData metaData = resultSet.getMetaData();
  362.                int numberOfColumns =  metaData.getColumnCount();
  363.                Vector columnNames = new Vector();
  364.                for(int column = 0; column < numberOfColumns; column++)
  365.                {
  366.                      columnNames.addElement(metaData.getColumnLabel(column+1));
  367.                      //System.out.print(test);
  368.                }
  369.                Vector rows = new Vector();
  370.                for (int i = 1; i <= metaData.getColumnCount(); i++)
  371.                     {
  372.                          //newRow.addElement(resultSet.getObject(i));
  373.  
  374.                     }
  375.                while (resultSet.next())
  376.                {
  377.                    Vector newRow = new Vector();
  378.                    data = true;
  379.  
  380.  
  381.                     for (int i = 1; i <= metaData.getColumnCount(); i++)
  382.                     {
  383.                          newRow.addElement(resultSet.getObject(i));
  384.  
  385.                     }
  386.                     rows.addElement(newRow);
  387.  
  388.                }
  389.  
  390.                table2 = new JTable(rows, columnNames);
  391.                //System.out.println("Not nulls - rows" + rows.toString());
  392.  
  393.  
  394.  
  395.      //System.out.println("WORK " + table);
  396.                //GUI gui = new GUI(table,con, columnNames);
  397.         s.close(); // close the Statement to let the database know we're done with it
  398.         con.close();
  399.         if(data)
  400.         {
  401.         dispose();
  402.  
  403.         SearchResults results = new SearchResults(table2);
  404.         }
  405.         else
  406.         {
  407.             JFrame frame = new JFrame();
  408.             JOptionPane.showMessageDialog(frame, "Your search for " + keyword + " returned no matches", "Search Result", JOptionPane.PLAIN_MESSAGE);
  409.         }
  410.         //results.setVisible(true);
  411.  
  412.              }
  413.              catch(Exception p)
  414.              {
  415.                 System.out.println(p.getMessage());
  416.              }
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.     }
  424.             if(obj.equals(addButton))
  425.             {
  426.                 String name = tf1.getText();
  427.                 String address = tf2.getText();
  428.                 String email = tf3.getText();
  429.                 String phone = tf4.getText();
  430.                 String purchase = tf5.getText();
  431.                 String id = "NULL";
  432.                 try
  433.                 {
  434.                     s.close();
  435.                     con.close();
  436.                     String dbURL = "jdbc:odbc:" + "pWarehouseDatabase";
  437.                     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  438.         Connection con = DriverManager.getConnection(dbURL, "","");
  439.         Statement s = con.createStatement();
  440.                // s.execute("INSERT INTO customers//(name, address, email, phone, purchase)");
  441.  
  442.             //s.execute("INSERT INTO customers values(?,'" +name + "','" + address + "','" + email + "','" + phone + "','" + purchase + "')");
  443.            //String query = "INSERT INTO customers(id, customerName, customerAddress, customerEmail, customerNumber, customerPurchase) values(?, '" +name + "','" + address + "','" + email + "','" + phone + "','" + purchase + "')";
  444. s.execute("INSERT INTO customers(customerName, customerAddress, customerEmail, customerNumber, customerPurchase) values('" +name + "','" + address + "','" + email + "','" + phone + "','" + purchase + "')");
  445.  
  446.  
  447.             //s.execute("UPDATE customers  SET customerName = '" +name + "' WHERE customerName = '" +name + "'");
  448.             //Statement s = connection.createStatement();
  449.             //s.executeUpdate(sql);
  450.             //id++;
  451.             s.close();
  452.  
  453.  
  454.  
  455.  
  456.                 tf1.setText("");
  457.                 tf2.setText("");
  458.                 tf3.setText("");
  459.                 tf4.setText("");
  460.                 tf5.setText("");
  461.                 }
  462.                 catch(Exception a)
  463.       {
  464.           System.out.println(a.getMessage());
  465.       }
  466.             }
  467.             if(obj.equals(newButton))
  468.             {
  469.                 //JPanel panel2 = new JPanel(panel2);
  470.                 try
  471.                 {
  472.                     String dbURL = "jdbc:odbc:" + "pWarehouseDatabase";
  473.                     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  474.         Connection con = DriverManager.getConnection(dbURL, "","");
  475.         Statement s = con.createStatement();
  476.         NewFrame newFrame = new NewFrame(con);
  477.        // newFrame.setUndecorated(true);
  478.  
  479.                 newFrame.setVisible(true);
  480.  
  481.                 s.close();
  482.                 dispose();
  483.                 //setVisible(false);
  484.                 }
  485.                 catch(Exception t)
  486.                 {
  487.  
  488.                 }
  489.  
  490.             }
  491.  
  492.             if(obj.equals(deleteButton))
  493.             {
  494.  
  495.                 try
  496.                 {
  497.                     String dbURL = "jdbc:odbc:" + "pWarehouseDatabase";
  498.                     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  499.         Connection con = DriverManager.getConnection(dbURL, "","");
  500.         Statement s = con.createStatement();
  501.  
  502.         Object key = table.getValueAt(selectedRow, 0);
  503.         String test = key.toString();
  504.         int number = Integer.parseInt(test);
  505.                 //int test = (int)key;
  506.         int response;
  507.  
  508.    response = JOptionPane.showConfirmDialog(null, "Are you sure you want to delete this contact?");
  509. if (response == JOptionPane.YES_OPTION)
  510.     {
  511.  
  512.         System.out.println(number);
  513.  
  514.  
  515.         String strSQL = "DELETE FROM customers WHERE id =" + number;
  516.  
  517.  
  518.             //dispose();
  519.             s.executeUpdate(strSQL);
  520.             System.out.println(" rows effected");
  521.     }
  522.  
  523.        // s.execute("DELETE FROM customers(customerName, customerAddress, customerEmail, customerNumber, customerPurchase) WHERE(id = 'number')";
  524.  
  525.  
  526.  
  527. /*
  528.             s.close();
  529.                     con.close();
  530.                     String dbURL2 = "jdbc:odbc:" + "pWarehouseDatabase";
  531.                     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  532.         Connection con2 = DriverManager.getConnection(dbURL2, "","");
  533.         Statement s2 = con2.createStatement();
  534.         ResultSet resultSet2 = s2.getResultSet(); // get any ResultSet that came from our query
  535.         ResultSetMetaData metaData2 = resultSet2.getMetaData();
  536.                int numberOfColumns2 =  metaData2.getColumnCount();
  537.                Vector columnNames2 = new Vector();
  538.                for(int column2 = 0; column2 < numberOfColumns2; column2++)
  539.                {
  540.                      columnNames2.addElement(metaData2.getColumnLabel(column2+1));
  541.                }
  542.                Vector rows2 = new Vector();
  543.                while (resultSet2.next())
  544.                {
  545.                     Vector newRow2 = new Vector();
  546.                     for (int i = 1; i <= metaData2.getColumnCount(); i++)
  547.                     {
  548.                          newRow2.addElement(resultSet2.getObject(i));
  549.                     }
  550.                     rows2.addElement(newRow2);
  551.                }
  552.                 table = new JTable(rows2, columnNames2);*/
  553.  
  554.             table = createTable();
  555.        GUI gui = new GUI(table,con, columnNames);
  556.  
  557. //GUI gui = new GUI( table, con);
  558. //gui.setVisible(true);
  559.  
  560.         dispose();
  561.      System.out.println("dispose");
  562.  
  563.  
  564.                 }
  565.              /* catch(SQLException s){
  566.         System.out.println("SQL statement is not executed!");
  567.       }
  568. */
  569.     catch (Exception o){
  570.       o.printStackTrace();
  571.     }
  572.  
  573.  
  574.  
  575.             }
  576.             if(obj.equals(repairButton))
  577.             {
  578.  
  579.                 NewRepair repair = new NewRepair(con);
  580.                 repair.setVisible(true);
  581.                 dispose();
  582.             }
  583.             if(obj.equals(editButton))
  584.             {
  585.                 dispose();
  586.                  Object key = table.getValueAt(selectedRow, 0);
  587.         String test = key.toString();
  588.         int number = Integer.parseInt(test);
  589.         System.out.println(number);
  590.         //panel2.remove(newButton);
  591.        // panel2.remove(editButton);
  592.         //panel2.remove(deleteButton);
  593.   //panel2.remove(refreshButton);
  594.         //dispose();
  595. JPanel innerPanel;
  596. //innerPanel.setLayout(new GridLayout(15,2));
  597. sf1 = new JTextField();
  598. sf2 = new JTextField();
  599. sf3 = new JTextField();
  600. sf4 = new JTextField();
  601. sf5 = new JTextField();
  602. sf6 = new JTextField();
  603.  sf7 = new JTextField();
  604. sf8 = new JTextField();
  605. sf9 = new JTextField();
  606. System.out.println("EDITING");
  607. try
  608. {
  609.         sf1.setText((String)table.getValueAt(selectedRow, 1));
  610.        sf2.setText((String)table.getValueAt(selectedRow, 2));
  611.        sf3.setText((String)table.getValueAt(selectedRow, 3));
  612.        sf4.setText((String)table.getValueAt(selectedRow, 4));
  613.        sf5.setText((String)table.getValueAt(selectedRow, 5));
  614.        sf6.setText((String)table.getValueAt(selectedRow, 6));
  615.        sf7.setText((String)table.getValueAt(selectedRow, 7));
  616.        sf8.setText((String)table.getValueAt(selectedRow, 8));
  617.        sf9.setText((String)table.getValueAt(selectedRow, 9));
  618. }
  619. catch(Exception er)
  620. {
  621.     System.out.println("This one is empty too");
  622. }
  623.        //System.out.println(table.getValueAt(selectedRow, 8));
  624.         innerPanel = new JPanel();
  625.         innerPanel.setLayout(new GridLayout(15,2));
  626.         innerPanel.add(new JLabel("Customer First Name"));
  627.         //innerPanel.add(addButton);
  628.         //innerPanel.add(newButton);
  629.         innerPanel.add(sf1);
  630.         innerPanel.add(new JLabel("Customer Last Name"));
  631.         innerPanel.add(sf2);
  632.  
  633.          innerPanel.add(new JLabel("Customer Address"));
  634.         innerPanel.add(sf3);
  635.         innerPanel.add(new JLabel("Customer Email"));
  636.         innerPanel.add(sf4);
  637.         innerPanel.add(new JLabel("Customer Phone"));
  638.         innerPanel.add(sf5);
  639.         innerPanel.add(new JLabel("Customer Purchase"));
  640.         innerPanel.add(sf6);
  641.         innerPanel.add(new JLabel("Customer Purchase Price"));
  642.         innerPanel.add(sf7);
  643.         innerPanel.add(new JLabel("Customer Purchase Date"));
  644.         innerPanel.add(sf8);
  645.         innerPanel.add(new JLabel("Purchase or Repair"));
  646.         innerPanel.add(sf9);
  647.         //innerPanel.add(purchaseScroll);
  648.         //innerPanel.add(addButton);
  649.         //innerPanel.add(cancelButton);
  650.         //add(innerPanel);
  651.         EditFrame editFrame = new EditFrame(number, innerPanel, sf1, sf2, sf3, sf4, sf5, sf6, sf7, sf8, sf9);
  652.             }
  653.  
  654.             if(obj.equals(refreshButton))
  655.         {
  656.             try
  657.             {
  658.  
  659.                 //s.close();
  660.  
  661.                     //con.close();
  662.                      System.out.println("here i AM");
  663.                     String dbURL = "jdbc:odbc:" + "pWarehouseDatabase";
  664.                     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  665.          con = DriverManager.getConnection(dbURL, "","");
  666.          s = con.createStatement();
  667.  
  668.          s.execute("select * from customers ORDER BY customerLastName"); // create a table * from customers
  669.         //s.execute("insert into TEST12345 values(1)"); // insert some data into the table
  670.         //s.execute("select column_name from TEST12345"); // select the data from the table
  671.         ResultSet resultSet = s.getResultSet(); // get any ResultSet that came from our query
  672.         ResultSetMetaData metaData = resultSet.getMetaData();
  673.                int numberOfColumns =  metaData.getColumnCount();
  674.                Vector columnNames = new Vector();
  675.                for(int column = 0; column < numberOfColumns; column++)
  676.                {
  677.                      columnNames.addElement(metaData.getColumnLabel(column+1));
  678.                }
  679.                Vector rows = new Vector();
  680.                while (resultSet.next())
  681.                {
  682.                     Vector newRow = new Vector();
  683.                     for (int i = 1; i <= metaData.getColumnCount(); i++)
  684.                     {
  685.                          newRow.addElement(resultSet.getObject(i));
  686.                     }
  687.                     rows.addElement(newRow);
  688.                }
  689.                JTable table = new JTable(rows, columnNames);
  690.                dispose();
  691. GUI gui = new GUI( table, con, columnNames);
  692. gui.setVisible(true);
  693.         }
  694.             catch(Exception i)
  695.             {
  696.  
  697.             }
  698.     }
  699.  
  700.         }
  701.     }
  702. }
  703.  
Jun 21 '09 #3
brendanmcdonagh
153 100+
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
Jun 21 '09 #4
JosAH
11,448 Expert 8TB
@brendanmcdonagh
I just skimmed through it, no need to say sorry; here's a little tip: don't just do this:

Expand|Select|Wrap|Line Numbers
  1. try {
  2.    ...
  3. catch (Exception e) {
  4.    System.out.println("A short message");
  5. }
  6.  
... but do this instead:

Expand|Select|Wrap|Line Numbers
  1. try {
  2.    ...
  3. catch (Exception e) {
  4.    e.printStackTrace();
  5. }
  6.  
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
Jun 21 '09 #5
brendanmcdonagh
153 100+
cheers for that Jos, I'll be using your recommendation from now on.

Regards
Brendan
Jun 21 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
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...
4
by: gabryh | last post by:
Hi, The following code throws me NullPointerException. ..... public static boolean isEmpty(String value) { return ((value == null) || (value.trim().equals(""))); }
0
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...
3
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...
1
oll3i
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...
13
oll3i
by: oll3i | last post by:
private List<Klient> klienci; m_add_client.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { ...
1
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...
2
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...
1
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...
3
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 { ...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
tracyyun
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...
0
agi2029
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,...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.