473,507 Members | 4,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to solve NullPointerException in java swing

1 New Member
import javax.swing.*;
import java.awt.*;

public class RegionFrame extends JFrame
{
JLabel lblRegName=null;
JLabel lblDistName=null;
JLabel lblRegionForm=null;
JLabel lblStatName=null;
JTextField txtRegName=null;
JTextField txtDistName=null;
JButton butAdd=null;
JButton butUpdate=null;
JComboBox cmbSta=null;


public RegionFrame()
{
Container cont=getContentPane();

cont.setLayout(null);

lblRegionForm=new JLabel("Region");

lblDistName=new JLabel("District Name");
lblRegName=new JLabel("Region Name");
txtDistName=new JTextField(25);
txtRegName=new JTextField(25);
butAdd=new JButton("Add");
butUpdate=new JButton("Update");
cmbSta=new JComboBox();

cont.add(lblDistName);
cont.add(txtDistName);
lblDistName.setBounds(50,50,120,25);
txtDistName.setBounds(180,50,150,25);

cont.add(lblRegName);
cont.add(txtRegName);
lblRegName.setBounds(50,150,120,25);
txtRegName.setBounds(180,150,150,25);


cont.add(lblStatName);
cont.add(cmbSta);
lblStatName.setBounds(50,150,120,25);
cmbSta.setBounds(180,150,150,25);

cont.add(butAdd);
cont.add(butUpdate);
butAdd.setBounds(100,125,80,25);
butUpdate.setBounds(200,125,80,25);

setVisible(true);
pack();
setSize(600,400);
}
public static void main(String ar[])
{
new RegionFrame();
}
}







Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:621)
at java.awt.Container.add(Container.java:307)
at RegionFrame.<init>(RegionFrame.java:44)
at RegionFrame.main(RegionFrame.java:60)
Jan 30 '10 #1
1 6405
pbrockway2
151 Recognized Expert New Member
You read these stack traces from the top down and once you get to a line that mentions your code you have a place to start looking for the bug.

"at RegionFrame.<init>(RegionFrame.java:44)" means that the error originated on the 44th line of the code you posted. Find this line.

The exception itself - "Exception in thread "main" java.lang.NullPointerException" - means that you have tried to dereference some variable whose value was null. This can come about for several reasons: you use the dereference (dot) operator on a variable whose value is null, or you make an array access on an array variable whose value is nulll, or you passa variable whose value is null to some other method that only accepts non null arguments.

So go to line 44 and check the two variables you find. Are either of them null? (You can check this with System.out.println()). Once you have found the null variable ask yourself: where did I mean to give that variable a nonnull value? And why didn't that happen?
Jan 30 '10 #2

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

Similar topics

2
3066
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...
17
1770
oll3i
by: oll3i | last post by:
nevermind the previous prob i posted i m getting further and i get a new exception in readFile method pleae help me with that one thank u a lot in advance java.lang.NullPointerException...
1
1869
oll3i
by: oll3i | last post by:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Zad21.Cennik.getCenna(Cennik.java) at Zad21.KwiaciarniaView$1.actionPerformed(KwiaciarniaView.java) at...
3
1647
oll3i
by: oll3i | last post by:
double dystanse = null; Integer czyNajblizszySasiad = null; Integer najblizszySasiad=null; System.out.println("Przed 1 petla for"); ...
21
4012
by: teenIce | last post by:
Hi all, Thanks in advance. I am new at Java. Currently I'm having two java files one is applet. when i try to preview the applet i get an error like this : java.lang.NullPointerException at...
1
1758
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...
1
1172
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
3939
by: lilyumestar | last post by:
This project is due by Tuesday and I haven't even gotten half of it done. Can anyone please help me with this Exception error? I've been trying to figure it out for several hours Error Message ...
2
6855
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
6733
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...
0
7220
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
7105
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...
0
7308
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7371
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...
0
7479
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...
1
5037
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
410
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...

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.