473,544 Members | 439 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Exception in thread "main" java.lang.NullP ointerException

37 New Member
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 ();
}
}
Sep 10 '07 #1
18 2558
r035198x
13,262 MVP
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.
Sep 10 '07 #2
dmjpro
2,476 Top Contributor
Hi pradeep84,
Welcome to TSDN!

Do you know why does this happen?
Have a look at this example.
Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3. void test(){}
  4. }
  5. class Main
  6. {
  7. A a;
  8. a.test(); //Here it causes the Error, NullPointerException.
  9. //Because here a is not initialized so a is null.
  10. }
  11.  
I think you understand my point.

Kind regards,
Dmjpro.
Sep 10 '07 #3
pradeep84
37 New Member
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...
Sep 10 '07 #4
r035198x
13,262 MVP
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.
Sep 10 '07 #5
madhoriya22
252 Contributor
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.
Sep 10 '07 #6
pradeep84
37 New Member
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...,
Sep 10 '07 #7
madhoriya22
252 Contributor
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 ..
Expand|Select|Wrap|Line Numbers
  1. cud = new Customerdetails();
  2.  
Sep 10 '07 #8
r035198x
13,262 MVP
Expand|Select|Wrap|Line Numbers
  1. Button save;
save is null at that point.

Expand|Select|Wrap|Line Numbers
  1. Button save = new Button("Save");
Here, save holds a reference to an instance of the Button class which is not null.
Sep 10 '07 #9
Nepomuk
3,112 Recognized Expert Specialist
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:
Expand|Select|Wrap|Line Numbers
  1. super("SPlanting ");
  2.         setSize(800,800);
  3.         setLayout(new GridLayout(7,2,45,45));
  4.         l1=new Label("Date");
  5.         l2=new Label("Noofmixes");
  6.         l3=new Label("TotalBentomikused");
  7.         l4=new Label("TotalLusformused");
  8.         l5=new Label("starttime");
  9.         l6=new Label("Offtime");
  10.         l7=new Label("PowerConsumed");
  11.         l8=new Label("Opertorname");
  12.  
  13.  
  14.         add(l1);
  15.         add(date);
  16.         add(l2);
  17.         add(nom);
  18.         add(l3);
  19.         add(tb);
  20.         add(l4);
  21.         add(tl);
  22.         add(l5);
  23.         add(st);
  24.         add(l6);
  25.         add(ft);
  26.         add(l7);
  27.         add(pc);
  28.         add(l8);
  29.         add(on);
  30.  
You never initialized "date".

Greetings,
Nepomuk
Sep 10 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

0
2984
by: Phillip Montgomery | last post by:
Hello all; I'm trying to debug an issue with a java script called, SelectSockets. It appears to be a fairly common one found on the web. I downloaded the SGI Java v1.4.1 installation from SGI's webpage and installed it using SGI's swmgr application. The installation was very straight forward and there were no errors when I installed the...
1
9100
by: Andy Howells | last post by:
Can anybody help me on this? I am getting the below error but have not got a clue why. The file in my classpath eing used has the class that it says is not defined. Any ideas? I am running java version 1.4.0 and WMQ Series version 5.3 with CSD04. Exception in thread "main" java.lang.NoClassDefFoundError: com/ibm/mq/server/MQSESSION at...
9
3104
by: jith87 | last post by:
What is ths error.????? I m nw loading the JDBC driver for connection with MySql. Exception in thread "main" java.lang.NoClassDefFoundError: MySQLJDBCDriverTest
1
1767
by: GHKASHYAP | last post by:
Hi this is the exception i am getting when i am trying to run this application: Exception in thread "main" java.lang.NullPointerException thanks in advance.. package com.inventive.StockMarketTrading; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.ActionEvent;
3
6429
by: Ananthu | last post by:
Hi This is my codings in order to access mysql database from java. Codings: import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement;
9
3272
by: tiyaramunna | last post by:
I am trying to configure my system with Java program just to practice on the coding....when i compile a test.java program i am able to see the class file but i cant run the program ... I am getting the following error Exception in thread "main" java.lang.NoClassDefFoundError: test Caused by: java.lang.ClassNotFoundException: test at...
4
14704
by: jmitch89 | last post by:
I don't why I get this error: Exception in thread "main" java.lang.NoClassDefFoundError The statement below works just fine: java -cp "appframework-1.0.3.jar;swing-worker-1.1.jar";CurrentStrobe.jar com.visionpro.currentstrobe.CurrentStrobeApp However, the statement below produces the error: java -cp...
1
13714
by: jimgym1989 | last post by:
I dont get it..why is the error: Exception in thread "main" java.util.InputMismatchException this is my code /** * @(#)textFileRead.java * * * @author * @version 1.00 2008/10/17 */
3
7635
by: ohadr | last post by:
hi, i get Exception in thread "main" java.lang.NullPointerException when i run my application. the exact error is: "Exception in thread "main" java.lang.NullPointerException at sortmergejoin.MergeJoin.Field(MergeJoin.java:204) at sortmergejoin.MergeJoin.SMJoin(MergeJoin.java:84) at...
1
6738
by: onlinegear | last post by:
HI i am writing this for college i know i have loads of combo boxes with nothing in the i havent got that far yet. but every time i run this is comes up with this erro run: Exception in thread "main" java.lang.NullPointerException at java.awt.Container.addImpl(Container.java:1041) at java.awt.Container.add(Container.java:365) ...
0
7439
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7376
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7395
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7722
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3433
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3425
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1851
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
997
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
679
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.