Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners show null point Exception """ while trying 2 load a JComboBox"" data is fetch from database ms-access
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Saurabh
show null point Exception """ while trying 2 load a JComboBox"" data is fetch from database ms-access
1 Answer(s)      2 years and a month ago
Posted in : Java Beginners


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(); } }


Advertisement
View Answers

September 18, 2012 at 11:48 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:
Advertisements
 

 

 

DMCA.com