Hi. Everyboy..
This Is simple swing application for search data from the database using textbox..
this is my code but getting some error.. any body help me....
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
public class JavaApplication4 extends JFrame implements ActionListener{
static JTextField FName,LName;
static JButton btnSearch;
static JPanel areapanel,bpanel,ipanel;
private static Statement st = null;
private static Connection con = null;
private static ResultSet rs = null;
JavaApplication4(){
super("Student Information System");
areapanel = new JPanel();
areapanel.setLayout(new GridLayout(6,2,5,5));
areapanel.add(new JLabel("First Name"));
areapanel.add(FName = new JTextField(10));
areapanel.add(new JLabel(""));
areapanel.add(btnSearch = new JButton("Search"));
areapanel.add(new JLabel("Last Name:"));
areapanel.add(LName = new JTextField(10));
ipanel = new JPanel();
ipanel.setLayout(new GridLayout(3,2,10,10));
ipanel.add(new JLabel(""));
bpanel = new JPanel();
bpanel.setLayout(new BorderLayout());
bpanel.add(areapanel,BorderLayout.NORTH);
bpanel.add(ipanel,BorderLayout.SOUTH);
add(bpanel);
btnSearch.addActionListener(this);
}
public static void main(String[] args) {
JavaApplication4 mainapp = new JavaApplication4();
mainapp.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
mainapp.setResizable(false);
mainapp.setSize(575, 250);
mainapp.setVisible(true);
}
public void retrieveRecords(){
String fname = null,lname = null;
try{
con = getConnection();
String query = "Select * from tblStudents where ID like '" + FName.getSelectedText() + "'";
st = con.createStatement();
ResultSet rs = st.executeQuery(query);
while(rs.next()){
// fname = rs.getString("FirstName");
lname = rs.getString("LastName");
}
st.close();
con.close();
}
catch(SQLException ex){
System.err.println(ex.getMessage());
}
// FName.setText(fname);
LName.setText(lname);
if(FName.getText().equals("") && LName.getText().equals("")){
JOptionPane.showMessageDialog(null,"Record Not Found!");
FName.setText("");
LName.setText("");
}
else
FName.setText(fname);
LName.setText(lname);
}
public static Connection getConnection()
{
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException ex) {
Logger.getLogger(JavaApplication4.class.getName()) .log(Level.SEVERE, null, ex);
}
return con;
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == btnSearch)
{
if(FName.getText().equals("")){
JOptionPane.showMessageDialog(null,"no record to search!");
}else{
retrieveRecords();
}
}
}
}
-----------------------------------------------------------------------------------
Getting this error....:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javaapplication4.JavaApplication4.retrieveRecords( JavaApplication4.java:88)
at javaapplication4.JavaApplication4.actionPerformed( JavaApplication4.java:136)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener$Actions .actionPerformed(BasicButtonListener.java:287)
at javax.swing.SwingUtilities.notifyAction(SwingUtili ties.java:1636)
at javax.swing.JComponent.processKeyBinding(JComponen t.java:2849)
at javax.swing.JComponent.processKeyBindings(JCompone nt.java:2884)
at javax.swing.JComponent.processKeyEvent(JComponent. java:2812)
at java.awt.Component.processEvent(Component.java:599 3)
at java.awt.Container.processEvent(Container.java:204 1)
at java.awt.Component.dispatchEventImpl(Component.jav a:4583)
at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
at java.awt.Component.dispatchEvent(Component.java:44 13)
at java.awt.KeyboardFocusManager.redispatchEvent(Keyb oardFocusManager.java:1848)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEv ent(DefaultKeyboardFocusManager.java:704)
at java.awt.DefaultKeyboardFocusManager.preDispatchKe yEvent(DefaultKeyboardFocusManager.java:969)
at java.awt.DefaultKeyboardFocusManager.typeAheadAsse rtions(DefaultKeyboardFocusManager.java:841)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent (DefaultKeyboardFocusManager.java:668)
at java.awt.Component.dispatchEventImpl(Component.jav a:4455)
at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
at java.awt.Window.dispatchEventImpl(Window.java:2475 )
at java.awt.Component.dispatchEvent(Component.java:44 13)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)