Exception in thread "main" java.lang.NullPointerException

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pradeep84
    New Member
    • Sep 2007
    • 37

    Exception in thread "main" java.lang.NullPointerException

    hi all,

    i'm using the following code and i'm getting
    exception in thread "main" java.lang.nullp ointerexception at the line which is in bold. help me to solve this problem

    import java.applet.*;
    import java.awt.*;
    import java.io.*;
    import javax.swing.*;
    import java.sql.*;
    import java.awt.event. *;
    import sun.jdbc.odbc.* ;
    public class Customerdetails extends Frame implements ActionListener
    {
    TextField ccod,cname,add, phno,conp,email ,fax;
    Label l1,l2,l3,l4,l5, l6,l7;
    Button save,exit;
    Connection con;
    ResultSet rs;
    Statement stmt;

    String s1,s2,s3,s4,s5, s6,s7;

    public Customerdetails ()
    {
    super("Customer Details");
    setSize(800,800 );
    setLayout(new GridLayout(7,2, 45,45));
    l1=new Label("Customer code");
    l2=new Label("CompanyN ame");
    l3=new Label("Address" );
    l4=new Label("PhoneNum ber");
    l5=new Label("Contact person");
    l6=new Label("EmailAdd ress");
    l7=new Label("FaxNumbe r");

    add(l1);
    add(ccod);
    add(l2);
    add(cname);
    add(l3);
    add(add);
    add(l4);
    add(phno);
    add(l5);
    add(conp);
    add(l6);
    add(email);
    add(l7);
    add(fax);

    save.addActionL istener(this);
    exit.addActionL istener(this);
    setVisible(true );



    addWindowListen er(new WindowAdapter() {
    public void WindowClosing(W indowEvent w) {System.exit(0) ;}});
    }
    public void actionPerformed (ActionEvent ae)
    {

    if(ae.getSource ()==save)
    {
    s1=ccod.getText ();
    s2=cname.getTex t();
    s3=add.getText( );
    s4=phno.getText ();
    s5=conp.getText ();
    s6=email.getTex t();
    s7=fax.getText( );

    try
    {
    String query = "insert into Customerdetails (Customercode,C ompanyName,Addr ess,PhoneNumber ,Contact person,EmailAdd ress,FaxNumber) "+
    "values('"+s1+" ','"+s2+"','"+s 3+"','"+s4+"',' "+s5+"','"+s6+" ','"+s7+"')";
    Class.forName(" sun.jdbc.odbc.J dbcOdbcDriver") ;
    con=DriverManag er.getConnectio n("jdbc:odbc:pr ism");
    stmt=con.create Statement();
    rs=stmt.execute Query(query);
    stmt.close();
    }
    catch(ClassNotF oundException e)
    {
    System.out.prin tln(e);
    }
    catch(SQLExcept ion e)
    {
    System.out.prin tln(e);
    }
    }
    if(ae.getSource ()==exit)
    {
    System.exit(0);
    }
    }
    public static void main(String args[])
    {

    Customerdetails cd=new Customerdetails ();
    }
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by pradeep84
    hi all,

    i'm using the following code and i'm getting
    exception in thread "main" java.lang.nullp ointerexception at the line which is in bold. help me to solve this problem

    import java.applet.*;
    import java.awt.*;
    import java.io.*;
    import javax.swing.*;
    import java.sql.*;
    import java.awt.event. *;
    import sun.jdbc.odbc.* ;
    public class Customerdetails extends Frame implements ActionListener
    {
    TextField ccod,cname,add, phno,conp,email ,fax;
    Label l1,l2,l3,l4,l5, l6,l7;
    Button save,exit;
    Connection con;
    ResultSet rs;
    Statement stmt;

    String s1,s2,s3,s4,s5, s6,s7;

    public Customerdetails ()
    {
    super("Customer Details");
    setSize(800,800 );
    setLayout(new GridLayout(7,2, 45,45));
    l1=new Label("Customer code");
    l2=new Label("CompanyN ame");
    l3=new Label("Address" );
    l4=new Label("PhoneNum ber");
    l5=new Label("Contact person");
    l6=new Label("EmailAdd ress");
    l7=new Label("FaxNumbe r");

    add(l1);
    add(ccod);
    add(l2);
    add(cname);
    add(l3);
    add(add);
    add(l4);
    add(phno);
    add(l5);
    add(conp);
    add(l6);
    add(email);
    add(l7);
    add(fax);

    save.addActionL istener(this);
    exit.addActionL istener(this);
    setVisible(true );



    addWindowListen er(new WindowAdapter() {
    public void WindowClosing(W indowEvent w) {System.exit(0) ;}});
    }
    public void actionPerformed (ActionEvent ae)
    {

    if(ae.getSource ()==save)
    {
    s1=ccod.getText ();
    s2=cname.getTex t();
    s3=add.getText( );
    s4=phno.getText ();
    s5=conp.getText ();
    s6=email.getTex t();
    s7=fax.getText( );

    try
    {
    String query = "insert into Customerdetails (Customercode,C ompanyName,Addr ess,PhoneNumber ,Contact person,EmailAdd ress,FaxNumber) "+
    "values('"+s1+" ','"+s2+"','"+s 3+"','"+s4+"',' "+s5+"','"+s6+" ','"+s7+"')";
    Class.forName(" sun.jdbc.odbc.J dbcOdbcDriver") ;
    con=DriverManag er.getConnectio n("jdbc:odbc:pr ism");
    stmt=con.create Statement();
    rs=stmt.execute Query(query);
    stmt.close();
    }
    catch(ClassNotF oundException e)
    {
    System.out.prin tln(e);
    }
    catch(SQLExcept ion e)
    {
    System.out.prin tln(e);
    }
    }
    if(ae.getSource ()==exit)
    {
    System.exit(0);
    }
    }
    public static void main(String args[])
    {

    Customerdetails cd=new Customerdetails ();
    }
    }
    1.) Use code tags when posting code.
    2.) Your constructor dereferences variables that are not initialized. See the buttons for example.

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Hi pradeep84,
      Welcome to TSDN!

      Do you know why does this happen?
      Have a look at this example.
      [code=java]
      class A
      {
      void test(){}
      }
      class Main
      {
      A a;
      a.test(); //Here it causes the Error, NullPointerExce ption.
      //Because here a is not initialized so a is null.
      }
      [/code]
      I think you understand my point.

      Kind regards,
      Dmjpro.

      Comment

      • pradeep84
        New Member
        • Sep 2007
        • 37

        #4
        Thank you sir..,

        Even though i declared.. it shows the same error.. plz say me exactly where i hav to declare....... and in wat type...

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by pradeep84
          Thank you sir..,

          Even though i declared.. it shows the same error.. plz say me exactly where i hav to declare....... and in wat type...
          *initialize* using the new keyword.

          Comment

          • madhoriya22
            Contributor
            • Jul 2007
            • 251

            #6
            Originally posted by pradeep84
            Thank you sir..,

            Even though i declared.. it shows the same error.. plz say me exactly where i hav to declare....... and in wat type...
            Hi,
            Can you show us how you have declared .. I think you have not done it correctly ..... I vil suggest you to check the previous posts again .. try to understand what those people are saying.
            Last edited by madhoriya22; Sep 10 '07, 09:01 AM. Reason: To add my signature :)

            Comment

            • pradeep84
              New Member
              • Sep 2007
              • 37

              #7
              i don't know wheather it is right... or wrong..

              [public class Customerdetails extends Frame implements ActionListener
              {
              TextField ccod,cname,add, phno,conp,email ,fax;
              Label l1,l2,l3,l4,l5, l6,l7;
              Button save,exit;
              Connection con;
              ResultSet rs;
              Statement stmt;
              Customerdetails cud;
              String s1,s2,s3,s4,s5, s6,s7;

              or i hav to declare some where else..
              say me...,

              Comment

              • madhoriya22
                Contributor
                • Jul 2007
                • 251

                #8
                Originally posted by pradeep84
                i don't know wheather it is right... or wrong..

                [public class Customerdetails extends Frame implements ActionListener
                {
                TextField ccod,cname,add, phno,conp,email ,fax;
                Label l1,l2,l3,l4,l5, l6,l7;
                Button save,exit;
                Connection con;
                ResultSet rs;
                Statement stmt;
                Customerdetails cud;
                String s1,s2,s3,s4,s5, s6,s7;

                or i hav to declare some where else..
                say me...,
                Hi,
                You have not understood the previous posts. Here you are creating only instances not intializing them. So initialize ur all these instances in ur constructor and ur problem will be solved like ..
                Code:
                cud = new Customerdetails();
                Last edited by madhoriya22; Sep 10 '07, 09:29 AM. Reason: To add my signature :)

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  [CODE=java]Button save;[/CODE]

                  save is null at that point.

                  [CODE=java]Button save = new Button("Save");[/CODE]
                  Here, save holds a reference to an instance of the Button class which is not null.

                  Comment

                  • Nepomuk
                    Recognized Expert Specialist
                    • Aug 2007
                    • 3111

                    #10
                    Originally posted by pradeep84
                    i don't know wheather it is right... or wrong..

                    [public class Customerdetails extends Frame implements ActionListener
                    {
                    TextField ccod,cname,add, phno,conp,email ,fax;
                    Label l1,l2,l3,l4,l5, l6,l7;
                    Button save,exit;
                    Connection con;
                    ResultSet rs;
                    Statement stmt;
                    Customerdetails cud;
                    String s1,s2,s3,s4,s5, s6,s7;

                    or i hav to declare some where else..
                    say me...,
                    One (the?) Problem is somewhere else:
                    [CODE=java]
                    super("SPlantin g ");
                    setSize(800,800 );
                    setLayout(new GridLayout(7,2, 45,45));
                    l1=new Label("Date");
                    l2=new Label("Noofmixe s");
                    l3=new Label("TotalBen tomikused");
                    l4=new Label("TotalLus formused");
                    l5=new Label("starttim e");
                    l6=new Label("Offtime" );
                    l7=new Label("PowerCon sumed");
                    l8=new Label("Opertorn ame");


                    add(l1);
                    add(date);
                    add(l2);
                    add(nom);
                    add(l3);
                    add(tb);
                    add(l4);
                    add(tl);
                    add(l5);
                    add(st);
                    add(l6);
                    add(ft);
                    add(l7);
                    add(pc);
                    add(l8);
                    add(on);
                    [/CODE]You never initialized "date".

                    Greetings,
                    Nepomuk

                    Comment

                    • madhoriya22
                      Contributor
                      • Jul 2007
                      • 251

                      #11
                      Originally posted by r035198x
                      [CODE=java]Button save;[/CODE]

                      save is null at that point.

                      [CODE=java]Button save = new Button("Save");[/CODE]
                      Here, save holds a reference to an instance of the Button class which is not null.
                      Hi,
                      Isn't it the duplicate post. I think there are two posts running parallely and both have same code and problem.

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by nepomuk
                        One (the?) Problem is somewhere else:
                        [CODE=java]
                        super("SPlantin g ");
                        setSize(800,800 );
                        setLayout(new GridLayout(7,2, 45,45));
                        l1=new Label("Date");
                        l2=new Label("Noofmixe s");
                        l3=new Label("TotalBen tomikused");
                        l4=new Label("TotalLus formused");
                        l5=new Label("starttim e");
                        l6=new Label("Offtime" );
                        l7=new Label("PowerCon sumed");
                        l8=new Label("Opertorn ame");


                        add(l1);
                        add(date);
                        add(l2);
                        add(nom);
                        add(l3);
                        add(tb);
                        add(l4);
                        add(tl);
                        add(l5);
                        add(st);
                        add(l6);
                        add(ft);
                        add(l7);
                        add(pc);
                        add(l8);
                        add(on);
                        [/CODE]You never initialized "date".

                        Greetings,
                        Nepomuk
                        Much worse is
                        [CODE=java] save.addActionL istener(this);
                        exit.addActionL istener(this);[/CODE]
                        Both save and exit are null at that point.

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #13
                          Originally posted by madhoriya22
                          Hi,
                          Isn't it the duplicate post. I think there are two posts running parallely and both have same code and problem.
                          It is. I've closed the second one.

                          Comment

                          • Nepomuk
                            Recognized Expert Specialist
                            • Aug 2007
                            • 3111

                            #14
                            Originally posted by r035198x
                            Much worse is
                            [CODE=java] save.addActionL istener(this);
                            exit.addActionL istener(this);[/CODE]
                            Both save and exit are null at that point.
                            Uff, I never even got that far. Pradeep84, have you worked through a few Java tutorials? Those errors make me suspect, that you haven't understood, how to create Objects in Java. Have a look at the Articles list.

                            Greetings,
                            Nepomuk

                            Comment

                            • pradeep84
                              New Member
                              • Sep 2007
                              • 37

                              #15
                              Hi...
                              i hav encoded these codes..in the program.. but it shows error in the main class of the program
                              Code:
                               
                                              
                                               Button save= new Button("Save");
                                               Button exit= new Button("Exit");
                              
                              public static void main(String args[]) 
                              {
                                 [B]Customerdetails cud= new Customerdetails();[/B]}
                              Thanks
                              regards
                              pradeep

                              Comment

                              Working...