473,569 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to solve NullPointerExce ption in java swing

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

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


public RegionFrame()
{
Container cont=getContent Pane();

cont.setLayout( null);

lblRegionForm=n ew JLabel("Region" );

lblDistName=new JLabel("Distric t 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(lblDis tName);
cont.add(txtDis tName);
lblDistName.set Bounds(50,50,12 0,25);
txtDistName.set Bounds(180,50,1 50,25);

cont.add(lblReg Name);
cont.add(txtReg Name);
lblRegName.setB ounds(50,150,12 0,25);
txtRegName.setB ounds(180,150,1 50,25);


cont.add(lblSta tName);
cont.add(cmbSta );
lblStatName.set Bounds(50,150,1 20,25);
cmbSta.setBound s(180,150,150,2 5);

cont.add(butAdd );
cont.add(butUpd ate);
butAdd.setBound s(100,125,80,25 );
butUpdate.setBo unds(200,125,80 ,25);

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







Exception in thread "main" java.lang.NullP ointerException
at java.awt.Contai ner.addImpl(Con tainer.java:621 )
at java.awt.Contai ner.add(Contain er.java:307)
at RegionFrame.<in it>(RegionFrame .java:44)
at RegionFrame.mai n(RegionFrame.j ava:60)
Jan 30 '10 #1
1 6420
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.<in it>(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.NullP ointerException " - 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.prin tln()). 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
3070
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 sun.awt.RepaintArea.paint(RepaintArea.java:177) at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:260) at...
17
1773
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 at konta_bankowe.Interfejs.readFile(Interfejs.java) at konta_bankowe.Interfejs.wyswietlHistorieRachunku(Interfejs.java) at...
1
1887
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 javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) package Zad21;
3
1655
oll3i
by: oll3i | last post by:
double dystanse = null; Integer czyNajblizszySasiad = null; Integer najblizszySasiad=null; System.out.println("Przed 1 petla for"); for(int j=0;j<=10;j++){ System.out.println("W 1 petli for"); if(j!=aktualnyWiersz){
21
4022
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 java.util.Hashtable.put(Unknown Source) at javax.swing.JEditorPane.registerEditorKitForContentType(Unknown Source) at...
1
1777
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;
1
1177
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 upon anything in the report package though, there is a NullPointerException for the 'report' variable (which identifies the location of the report. The...
2
3947
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 "Main" Java.lang NullPointerException at Project1.sortingByZipCode<Project1.java:80> at...
2
6871
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 of the examples I've seen online - in fact, it is almost identical. The only things I've changed (I think) are variable names. The main one I've been...
1
6741
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
7618
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...
0
7926
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8138
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7679
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
6287
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5223
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2117
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
1228
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
946
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.