how to solve NullPointerException in java swing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • senthilaim
    New Member
    • Jan 2010
    • 1

    how to solve NullPointerException in java swing

    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)
  • pbrockway2
    Recognized Expert New Member
    • Nov 2007
    • 151

    #2
    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?

    Comment

    Working...