show null point Exception """ while trying 2 load a JComboBox"" data is fetch from database ms-access

show null point Exception """ while trying 2 load a JComboBox"" data is fetch from database ms-access

import javax.swing.*; import java.awt.*; import java.sql.*; class Comboo extends JFrame { JComboBox jcb; Connection con; Statement stat; ResultSet res; public Comboo() { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:trrl"); JOptionPane.showMessageDialog(null,"connected to database");

// JOptionPane.showMessageDialog(null,"iam here"); stat=con.createStatement(); String ss="select name from sorab"; res=stat.executeQuery(ss); while(res.next()) { jcb.addItem(res.getString("name")); } } catch(Exception f) { System.out.println(f); } setSize(400,500); setLayout(null); setVisible(true); Color d=Color.white; getContentPane().setBackground(d); setDefaultCloseOperation(JFrame.EXITONCLOSE); jcb=new JComboBox(); jcb.setBounds(200,200,150,30); add(jcb);

} public static void main(String[] args) { new Comboo(); } }

View Answers

September 18, 2012 at 11:47 AM

You didn't create an instance of jcombobox class therefore error occurs. Anyways, we have modified your code.

import javax.swing.*; 
import java.awt.*;
import java.sql.*; 
class Comboo extends JFrame {
    JComboBox jcb; 
    Connection con;
    Statement stat;
    ResultSet res;
    public Comboo() {
        jcb=new JComboBox();
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
            con=DriverManager.getConnection("jdbc:odbc:trrl"); 
            JOptionPane.showMessageDialog(null,"connected to database");

stat=con.createStatement(); 
String ss="select name from data";
res=stat.executeQuery(ss);
while(res.next()) { 
    jcb.addItem(res.getString("name"));
    }
    } 
    catch(Exception f) {
        System.out.println(f); 
        } 
        setSize(400,500);
        setLayout(null); 
        setVisible(true); 
        Color d=Color.white; 
        getContentPane().setBackground(d); 
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        jcb.setBounds(200,200,150,30);
        add(jcb);

}
public static void main(String[] args) {
    new Comboo();
    } 
    }









Related Tutorials/Questions & Answers:

Ads