473,387 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

NullPointerException.... in java

Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.io.*;
  4. import java.util.*;
  5. import java.sql.*;
  6. import java.lang.*;
  7.  
  8. class DbAwt extends Frame implements ActionListener
  9. {
  10.     private TextField t1, t2, t3;
  11.     private Button b1, b2, b3;
  12.     private Label l1, l2, l3;
  13.     private Connection con = null;
  14.     private ResultSet rs;
  15.     private Statement st;
  16.     private String name = null;
  17.     private PreparedStatement pst = null;
  18.     private int r = 0, cr = 0;
  19.  
  20.     public DbAwt()
  21.     {
  22.  
  23.         t1 = new TextField();
  24.         t2 = new TextField();
  25.         t3 = new TextField();
  26.         b1 = new Button("Insert");
  27.         //b2 = new Button("Update");
  28.         b3 = new Button("Delete");
  29.         l1 = new Label("Roll Number");
  30.         l2 = new Label("Name");
  31.         l3 = new Label("Cet Score");
  32.  
  33.         setBackground(Color.gray);
  34.         setSize(400, 400);
  35.  
  36.         setLayout(new GridLayout(6, 6, 50, 40));
  37.         add(l1);
  38.         add(t1);
  39.         add(l2);
  40.         add(t2);
  41.         add(l3);
  42.         add(t3);
  43.         add(b1);
  44.         add(b3);
  45.         add(b2);
  46.         b1.addActionListener(this);
  47.         b3.addActionListener(this);
  48.         addWindowListener(new winevent());
  49.         setVisible(true);
  50.     }
  51.     public void actionPerformed(ActionEvent ae)
  52.     {
  53.         try
  54.         {
  55.             if (ae.getSource() == b1)
  56.             {
  57.                 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  58.                 con = DriverManager.getConnection("Jdbc:Odbc:ketan");
  59.                 r = Integer.parseInt(t1.getText());
  60.                 name = t2.getText();
  61.                 cr = Integer.parseInt(t3.getText());
  62.                 pst = con.prepareStatement("insert into Stud (Roll,Name,cetscr) values(?,?,?)");
  63.                 pst.setInt(1, r);
  64.                 pst.setString(2, name);
  65.                 pst.setInt(3, cr);
  66.                 int n = 0;
  67.                 n=pst.executeUpdate();
  68.  
  69.                 if (n > 0)
  70.                 {
  71.                     //t1.setText("");
  72.                     //t2.setText("");
  73.                     //t3.setText("");
  74.                     t1.setText("Data inserted...");
  75.                 }
  76.                 else
  77.                 {
  78.                     //t1.setText("");
  79.                     //t2.setText("");
  80.                     //t3.setText("");
  81.                     t1.setText("Data inserted...");
  82.                 }
  83.                 con.close();
  84.             }
  85.             else
  86.             {
  87.                 t1.setText("");
  88.                 t2.setText("Good bye");
  89.             }
  90.         }
  91.         catch (Exception e)
  92.         {
  93.             System.out.println("Exception is : " + e);
  94.         }
  95.     }
  96.     public static void main(String arg[])throws NullPointerException
  97.     {
  98.         try
  99.         {
  100.             new DbAwt();
  101.         }
  102.         catch (NullPointerException ne)
  103.         {
  104.             System.out.println("Exception is : " + ne);
  105.         }
  106.     }
  107. }
  108. class winevent extends WindowAdapter
  109. {
  110.     public void windowClosing(WindowEvent we)
  111.     {
  112.         System.exit(0);
  113.     }
  114. }

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

Expand|Select|Wrap|Line Numbers
  1.  Exception in thread "main" java.lang.NullPointerException
  2.         at java.awt.Container.addImpl(Container.java:1027)
  3.         at java.awt.Container.add(Container.java:352)
  4.         at DbAwt1.<init>(DbAwt1.java:43)
  5.         at DbAwt1.main(DbAwt1.java:49)
Mar 13 '08 #1
1 2393
r035198x
13,262 8TB
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.println to print out variable values.
4.) Whay did you not initialize b2 if you wanted to add it to your container?
Mar 13 '08 #2

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

Similar topics

2
by: Smith | last post by:
The program compiled successfully, but it gives the following error on runtime.... java.lang.NullPointerException at FrogManiaApp.paint(FrogManiaApp.java:102) at...
3
by: Alan Krueger | last post by:
Greetings, I've been able to cache Transformer objects in a Tomcat-based servlet application to avoid unnecessary Transformer rebuilding, except for certain ones on certain machines. I'm...
3
oll3i
by: oll3i | last post by:
double dystanse = null; Integer czyNajblizszySasiad = null; Integer najblizszySasiad=null; System.out.println("Przed 1 petla for"); ...
1
by: Lost Prophet | last post by:
Hi Im having a problem with some code. Originally, the application was set into three packages (script, gui and control). I have changed is so there is a fourth called 'report'. Whenever I call...
2
by: sokeefe | last post by:
I am trying to edit the GUI of a project in Netbeans. In particular, I am trying to add new JButtons. I get a NullPointerException when I try to add an Event to any given JButton (even ones that...
1
by: sokeefe | last post by:
I am trying to edit the GUI of a project in Netbeans. In particular, I am trying to add new JButtons. I get a NullPointerException when I try to add an Event to any given JButton (even ones that...
2
by: dragonridingsorceress | last post by:
I am trying to learn how to use the JFileChooser. I'm working in BlueJ. I keep getting a NullPointerException. Full text of the error message is at the bottom of the post. My code is based on some...
1
by: r035198x | last post by:
This exception occurs often enough in practice to warrant its own article. It is a very silly exception to get because it's one of the easiest exceptions to avoid in programming. Yet we've all got it...
3
by: chris123456789 | last post by:
Hi, when I run my code I get a NullPointerException:null. Here is the part of the code where the error occurs: import java.util.*; import java.io.*; public class Decrypt { ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.