Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Search data....

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Search data....

    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)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Search data....

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javaapplication4.JavaApplication4.retrieveRecords( JavaApplication4.java:88)
    There is a variable with a null value at line 84. Look at that line and determine what variable has a null value and then backtrack in the code to find out why that variable does not have a valid value.

    If you can not tell what variable is null, add a println just before line 88 and print the value of all variables on line 88.

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Search data....

    Quote Originally Posted by Norm View Post
    There is a variable with a null value at line 84. Look at that line and determine what variable has a null value and then backtrack in the code to find out why that variable does not have a valid value.

    If you can not tell what variable is null, add a println just before line 88 and print the value of all variables on line 88.

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting

    If I m not intialised my value as null. at line 84
    then got the error to intialised a value when i put that value
    in the if condition..

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Search data....

    What variable has the null value? Where do you give that variable a value?

    You can not use a variable with a null value to reference methods in a class. THe variable must have a valid non-null value.

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: April 9th, 2012, 05:13 PM
  2. Graph Search Theory with extend of BFS, UFS and A* Search in grid
    By keat84 in forum Java Theory & Questions
    Replies: 1
    Last Post: February 7th, 2012, 09:46 AM
  3. Replies: 2
    Last Post: June 15th, 2011, 03:49 PM
  4. [SOLVED] i cant make a java jar file which can search data from mysql
    By talha07 in forum JDBC & Databases
    Replies: 6
    Last Post: January 20th, 2011, 06:09 AM
  5. Data Structures(Binary Search Tree to AVL Tree)ASAP
    By jfAdik in forum Algorithms & Recursion
    Replies: 2
    Last Post: April 5th, 2010, 03:58 AM