NullPointerException.... in java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ketand1
    New Member
    • Nov 2007
    • 5

    #1

    NullPointerException.... in java

    [CODE=java]import java.awt.*;
    import java.awt.event. *;
    import java.io.*;
    import java.util.*;
    import java.sql.*;
    import java.lang.*;

    class DbAwt extends Frame implements ActionListener
    {
    private TextField t1, t2, t3;
    private Button b1, b2, b3;
    private Label l1, l2, l3;
    private Connection con = null;
    private ResultSet rs;
    private Statement st;
    private String name = null;
    private PreparedStateme nt pst = null;
    private int r = 0, cr = 0;

    public DbAwt()
    {

    t1 = new TextField();
    t2 = new TextField();
    t3 = new TextField();
    b1 = new Button("Insert" );
    //b2 = new Button("Update" );
    b3 = new Button("Delete" );
    l1 = new Label("Roll Number");
    l2 = new Label("Name");
    l3 = new Label("Cet Score");

    setBackground(C olor.gray);
    setSize(400, 400);

    setLayout(new GridLayout(6, 6, 50, 40));
    add(l1);
    add(t1);
    add(l2);
    add(t2);
    add(l3);
    add(t3);
    add(b1);
    add(b3);
    add(b2);
    b1.addActionLis tener(this);
    b3.addActionLis tener(this);
    addWindowListen er(new winevent());
    setVisible(true );
    }
    public void actionPerformed (ActionEvent ae)
    {
    try
    {
    if (ae.getSource() == b1)
    {
    Class.forName(" sun.jdbc.odbc.J dbcOdbcDriver") ;
    con = DriverManager.g etConnection("J dbc:Odbc:ketan" );
    r = Integer.parseIn t(t1.getText()) ;
    name = t2.getText();
    cr = Integer.parseIn t(t3.getText()) ;
    pst = con.prepareStat ement("insert into Stud (Roll,Name,cets cr) values(?,?,?)") ;
    pst.setInt(1, r);
    pst.setString(2 , name);
    pst.setInt(3, cr);
    int n = 0;
    n=pst.executeUp date();

    if (n > 0)
    {
    //t1.setText("");
    //t2.setText("");
    //t3.setText("");
    t1.setText("Dat a inserted...");
    }
    else
    {
    //t1.setText("");
    //t2.setText("");
    //t3.setText("");
    t1.setText("Dat a inserted...");
    }
    con.close();
    }
    else
    {
    t1.setText("");
    t2.setText("Goo d bye");
    }
    }
    catch (Exception e)
    {
    System.out.prin tln("Exception is : " + e);
    }
    }
    public static void main(String arg[])throws NullPointerExce ption
    {
    try
    {
    new DbAwt();
    }
    catch (NullPointerExc eption ne)
    {
    System.out.prin tln("Exception is : " + ne);
    }
    }
    }
    class winevent extends WindowAdapter
    {
    public void windowClosing(W indowEvent we)
    {
    System.exit(0);
    }
    }[/CODE]


    When i run this programm at run time i get the following error please explain me hou to overcome from this error

    Code:
     Exception in thread "main" java.lang.NullPointerException
            at java.awt.Container.addImpl(Container.java:1027)
            at java.awt.Container.add(Container.java:352)
            at DbAwt1.<init>(DbAwt1.java:43)
            at DbAwt1.main(DbAwt1.java:49)
    Last edited by r035198x; Mar 13 '08, 06:17 PM. Reason: added code tags
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    1.) Use code tags when posting code
    2.) Use meaningful variable names in your code. Names like b1,b2 e.t.c create confusion for everyone including yourself.
    3.) Nullpointers are a sitting duck if you know how to use System.out.prin tln to print out variable values.
    4.) Whay did you not initialize b2 if you wanted to add it to your container?

    Comment

    Working...