Results 1 to 8 of 8
  1. #1
    anishka is offline Member
    Join Date
    Jun 2013
    Posts
    5
    Rep Power
    0

    Default getting Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException again

    hi,
    m new to this site... dnt konow where to post question.... i have made program on JFrame using swing with connectivity to jdbc but getting the above specified error....
    Please help me fixing this


    program

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.sql.*;


    class recapgui extends JFrame implements ActionListener
    {


    public void actionPerformed(ActionEvent e)
    {
    Connection con;
    String name="";
    name=t1.getText();
    String age="";
    age=t2.getText();

    String sexcode;
    if(r1.isSelected()==true)
    sexcode="male";
    else
    sexcode="female";
    String nation=(String)jc.getSelectedItem();
    /*String pass=t3.getText();*/
    try
    {
    DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
    con=DriverManager.getConnection("jdbc:oracle:thin: @localhost:1521:XE","system","qurika");
    JOptionPane.showMessageDialog(null,"connected");


    PreparedStatement st=con.prepareStatement("insert into cust values(?,?,?,?)");
    st.setString(1,name);
    st.setString(2,age);
    st.setString(3,sexcode);
    st.setString(4,nation);
    /*st.setString(5,pass);*/
    st.executeUpdate();

    JOptionPane.showMessageDialog(null,"inserted");
    con.close();
    }
    catch(Exception ex)
    {
    JOptionPane.showMessageDialog(null,"sorry");
    }


    }


    public static void main(String args[])
    {
    recapgui ob =new recapgui();
    ob.setSize(600,600);
    ob.setVisible(true);
    }


    JLabel l1,l2,l3,l4,l5;
    JButton b1,b2;
    JTextField t1,t2;
    JPasswordField t3;
    JRadioButton r1,r2;
    JComboBox jc;

    recapgui()
    {
    JLabel l1=new JLabel("Name");
    JLabel l2=new JLabel("Age");
    JLabel l3=new JLabel("sexcode");
    JLabel l4=new JLabel("nation");
    JLabel l5=new JLabel("Password");

    JTextField t1= new JTextField();
    JTextField t2=new JTextField();
    JPasswordField t3=new JPasswordField();
    JRadioButton r1=new JRadioButton("male");
    JRadioButton r2=new JRadioButton("female");
    ButtonGroup bg=new ButtonGroup();
    bg.add(r1);
    bg.add(r2);

    JComboBox jc =new JComboBox();
    jc.addItem("india");
    jc.addItem("usa");
    jc.setEditable(true);
    JButton b1= new JButton("save");
    JButton b2=new JButton("cancel");

    JPanel p=new JPanel();
    p.add(r1);
    p.add(r2);

    GridLayout g1=new GridLayout(6,2);
    add(l1);
    add(t1);
    add(l2);
    add(t2);
    add(l3);
    add(p);
    add(l4);
    add(jc);
    add(l5);
    add(t3);
    add(b1);
    add(b2);
    setLayout(g1);
    b1.addActionListener(this);

    }





    }




    error:


    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at recapgui.actionPerformed(recapgui.java:14)
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Sour
    ce)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Sour
    ce)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Sour
    ce)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Sour
    ce)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at recapgui.actionPerformed(recapgui.java:14)
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Sour
    ce)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Sour
    ce)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Sour
    ce)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Sour
    ce)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: getting Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException aga

    Moved from Forum Guides.

    Please go through the Guide For New Members and BB Code List - Java Programming Forum - Learn Java Programming and edit your post accordingly.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    anishka is offline Member
    Join Date
    Jun 2013
    Posts
    5
    Rep Power
    0

    Default Re: getting Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException aga

    error is generated after clicking on save button... also i have nt added any actionListener to button cancel

  4. #4
    anishka is offline Member
    Join Date
    Jun 2013
    Posts
    5
    Rep Power
    0

    Default Re: getting Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException aga

    thank u :)

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,947
    Rep Power
    4

    Default Re: getting Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException aga

    First, please put code tags [code][/code]around your code to properly format it.

    The problem is that that one of your variables referenced in the actionPerformed method is null. Probably because the event was fired before it was properly initialized. Use some print statements to help you figure out which one. Although it should be at line 14.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    748
    Rep Power
    2

    Default Re: getting Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException aga

    You have declared t1 as field, but also as local variable in the cosntructor. This is legal Java, but the side effect is that the field is still NULL. Remove the declaration from the constructor. Also, formatting your code properly will GREATLY help you in not making a mess. I took the liberty to reformat the code for you so you can see what it kinda should look like. I deliberately did not fix your error, since I am not the one doing your homework :)

    Java Code:
    import sun.jdbc.odbc.JdbcOdbcDriver;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    
    public class recapgui extends JFrame implements ActionListener {
    
        JLabel l1, l2, l3, l4, l5;
        JButton b1, b2;
        JTextField t1, t2;
        JPasswordField t3;
        JRadioButton r1, r2;
        JComboBox jc;
    
        recapgui() {
            JLabel l1 = new JLabel("Name");
            JLabel l2 = new JLabel("Age");
            JLabel l3 = new JLabel("sexcode");
            JLabel l4 = new JLabel("nation");
            JLabel l5 = new JLabel("Password");
    
            JTextField t1 = new JTextField();
            JTextField t2 = new JTextField();
            JPasswordField t3 = new JPasswordField();
            JRadioButton r1 = new JRadioButton("male");
            JRadioButton r2 = new JRadioButton("female");
            ButtonGroup bg = new ButtonGroup();
            bg.add(r1);
            bg.add(r2);
    
            JComboBox jc = new JComboBox();
            jc.addItem("india");
            jc.addItem("usa");
            jc.setEditable(true);
            JButton b1 = new JButton("save");
            JButton b2 = new JButton("cancel");
    
            JPanel p = new JPanel();
            p.add(r1);
            p.add(r2);
    
            GridLayout g1 = new GridLayout(6, 2);
            add(l1);
            add(t1);
            add(l2);
            add(t2);
            add(l3);
            add(p);
            add(l4);
            add(jc);
            add(l5);
            add(t3);
            add(b1);
            add(b2);
            setLayout(g1);
            b1.addActionListener(this);
        }
    
        public static void main(String args[]) {
            recapgui ob = new recapgui();
            ob.setSize(600, 600);
            ob.setVisible(true);
        }
    
        public void actionPerformed(ActionEvent e) {
            Connection con;
            String name = "";
            name = t1.getText();
            String age = "";
            age = t2.getText();
    
            String sexcode;
            if (r1.isSelected() == true) {
                sexcode = "male";
            }
            else {
                sexcode = "female";
            }
            String nation = (String) jc.getSelectedItem();
    /*String pass=t3.getText();*/
            try {
                DriverManager.registerDriver(new JdbcOdbcDriver());
                con = DriverManager.getConnection("jdbc:oracle:thin: @localhost:1521:XE", "system", "qurika");
                JOptionPane.showMessageDialog(null, "connected");
    
    
                PreparedStatement st = con.prepareStatement("insert into cust values(?,?,?,?)");
                st.setString(1, name);
                st.setString(2, age);
                st.setString(3, sexcode);
                st.setString(4, nation);
    /*st.setString(5,pass);*/
                st.executeUpdate();
    
                JOptionPane.showMessageDialog(null, "inserted");
                con.close();
            }
            catch (Exception ex) {
                JOptionPane.showMessageDialog(null, "sorry");
            }
        }
    }

  7. #7
    anishka is offline Member
    Join Date
    Jun 2013
    Posts
    5
    Rep Power
    0

    Default Re: getting Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException aga

    thanks alot... :)

  8. #8
    anishka is offline Member
    Join Date
    Jun 2013
    Posts
    5
    Rep Power
    0

    Default Re: getting Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException aga

    i got the output..,.. n thank u for doing little bit of my homework :P

Similar Threads

  1. Replies: 11
    Last Post: 09-24-2012, 03:37 AM
  2. Replies: 1
    Last Post: 12-13-2010, 03:19 PM
  3. Replies: 0
    Last Post: 05-28-2010, 02:47 PM
  4. Replies: 3
    Last Post: 04-30-2010, 07:44 PM
  5. Replies: 5
    Last Post: 08-26-2009, 11:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •