0

When I'm trying to populate my jtable named ctab with data from the database, the program throws a nullpointer exception.here is the program code:

package client;

import net.proteanit.sql.DbUtils;
import java.sql.*;
import java.util.*;
import java.util.Date;
import java.awt.EventQueue;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Cursor;
import javax.swing.JLabel;
import javax.swing.table.DefaultTableModel;
import client.AddCon;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JList;
import javax.swing.table.TableColumnModel;

public class MAINMENU {

private JFrame frmKusinaNiKambal;

private JTextField searchbar;
    private Connection conn;
    private JTable ctab;
    private ResultSet rs;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                MAINMENU window = new MAINMENU();
                window.frmKusinaNiKambal.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public MAINMENU() {
         try{
//Load database driver and connect

    Class.forName("com.mysql.jdbc.Driver");
    Connection conn = DriverManager.getConnection(
    "jdbc:mysql://localhost:3306/KusinaNiKambal","root","1234");
    JOptionPane.showMessageDialog(null, "Database Synchronization Successful.\nQuerying may now commence");
         }
            catch (Exception e){
JOptionPane.showMessageDialog(null, e);
}
    initialize();
}

    private void poptab(){
    try{
        String sql = "select * from kusinanikambal.cis";
        PreparedStatement pst = conn.prepareStatement(sql);
        rs = pst.executeQuery(sql);
        ctab.setModel(DbUtils.resultSetToTableModel(rs));
    }
    catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }
    }
/**
 * Initialize the contents of the frame.
 */
private void initialize() {

    frmKusinaNiKambal = new JFrame();
    frmKusinaNiKambal.setTitle("Kusina ni Kambal - Client Information System");
    frmKusinaNiKambal.setResizable(false);
    frmKusinaNiKambal.getContentPane().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    frmKusinaNiKambal.setBounds(100, 100, 800, 500);
    frmKusinaNiKambal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frmKusinaNiKambal.getContentPane().setLayout(null);
    poptab();
            try{
//Load database driver and connect
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/KusinaNiKambal","root","1234");}
            catch (Exception e){
JOptionPane.showMessageDialog(null, e);
}
    JButton AddCon = new JButton("Add");
    AddCon.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            client.AddCon.main(null);
        }
    });
    AddCon.setFont(new Font("Arial", Font.BOLD, 17));
    AddCon.setBounds(629, 97, 155, 40);
    frmKusinaNiKambal.getContentPane().add(AddCon);

    JButton ViewCon = new JButton("View");
    ViewCon.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

                    }
    });
    ViewCon.setFont(new Font("Arial", Font.BOLD, 17));
    ViewCon.setBounds(629, 199, 155, 40);
    frmKusinaNiKambal.getContentPane().add(ViewCon);

    JLabel lblWelcomeToThe = new JLabel("Welcome to the Kusina ni Kambal Client     Information System!");
    lblWelcomeToThe.setFont(new Font("Arial", Font.PLAIN, 17));
    lblWelcomeToThe.setBounds(133, 11, 443, 14);
    frmKusinaNiKambal.getContentPane().add(lblWelcomeToThe);

    JButton EditCon = new JButton("Edit");
            EditCon.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
                            client.EditCon.main(null);
                    }
            });
            EditCon.setFont(new Font("Arial", Font.BOLD, 17));
    EditCon.setBounds(629, 148, 155, 40);
    frmKusinaNiKambal.getContentPane().add(EditCon);


    JButton btnNewButton_1 = new JButton("Archive");
    btnNewButton_1.setFont(new Font("Arial", Font.BOLD, 17));
    btnNewButton_1.setBounds(629, 250, 155, 40);
    frmKusinaNiKambal.getContentPane().add(btnNewButton_1);

            String [] columnNames = {"Contract #","Engager","Contact #",
                "Client Address","Event Date","Time","Event Address",
                "Contract FilePath","Referred By"};
            DefaultTableModel model = new DefaultTableModel();
            model.setColumnIdentifiers(columnNames);

    ctab = new JTable();
    ctab.setBounds(26, 97, 580, 347);
    frmKusinaNiKambal.getContentPane().add(ctab);
            ctab.setModel(model);

    searchbar = new JTextField();
    searchbar.setBounds(26, 54, 361, 20);
    frmKusinaNiKambal.getContentPane().add(searchbar);
    searchbar.setColumns(10);

    JComboBox filter = new JComboBox();
    filter.setBounds(397, 54, 115, 20);
    frmKusinaNiKambal.getContentPane().add(filter);

    JButton btnSearch = new JButton("Search");
    btnSearch.setBounds(522, 53, 89, 23);
    frmKusinaNiKambal.getContentPane().add(btnSearch);
}
}

and here is the sql script for the table named cis under the schema kusinanikambal

create database kusinanikambal;
create table cis(
contno varchar(12) not null,
engager varchar(30) not null,
contactno varchar(12) not null,
claddr varchar(100) not null,
evdate date default 0000-00-00,
evtime time,
contract varchar(200),
referrer varchar(30)
);

here is the staktrace:

java.lang.NullPointerException
at client.MAINMENU.poptab(MAINMENU.java:79)
at client.MAINMENU.initialize(MAINMENU.java:102)
at client.MAINMENU.<init>(MAINMENU.java:70)
at client.MAINMENU$1.run(MAINMENU.java:47)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at             java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
2
  • When debugging, make sure to run e.printStackTrace() to see the whole stack trace. Commented Jan 20, 2014 at 18:53
  • Please indent your code properly before posting it, this is a mess to try to look through. Commented Jan 20, 2014 at 18:54

1 Answer 1

1

You'll probably want to change

Connection conn = DriverManager.getConnection(...

in your constructor, to

this.conn = DriverManager.getConnection(...

Currently you are shadowing the instance variable with a local variable of the same name. Your instance variable therefore remains null.

I haven't gone through all your code, but it seems like you are doing this in many places. Correct those as well.

Sign up to request clarification or add additional context in comments.

3 Comments

I've done the revision of the code, but the exception comes when i try to populate the jtable in the method poptab.
@user3209957 In your poptab() method, you are trying to use ctab before you've initialized it.
@nachokk It seems to be initialized in poptab(). But regardless, somewhere, something is null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.