Results 1 to 4 of 4
Like Tree2Likes
  • 1 Post By doWhile
  • 1 Post By Norm

Thread: GUI throwing exceptions

  1. #1
    pwnedu46 is offline Member
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Default GUI throwing exceptions

    I've been teaching myself from a textbook (Java Programming: Program Design Including Data Structures if anyone's interested) and got to Chapter 6 with no real problems. The compiler(JCreator Pro) is saying

    Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Container.java:1041)
    at java.awt.Container.add(Container.java:365)
    at RectangleProgram.<init>(RectangleProgram.java:32)
    at RectangleProgram.main(RectangleProgram.java:51)
    Unfortunately, it doesn't tell me a line number nor a decipherable error message.
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    
    public class RectangleProgram extends JFrame {
        private static final int WIDTH    = 400;
        private static final int HEIGHT   = 300;
    
        private JLabel lengthL, widthL, areaL, perimL;
        private JTextField lengthTF, widthTF, areaTF, perimTF;
    
        public RectangleProgram()
        {
        	setTitle("Area and Perimeter of a Rectangle");
    
        	lengthL = new JLabel("Enter the length:", SwingConstants.CENTER);
        	widthL 	= new JLabel("Enter the width:", SwingConstants.CENTER);
        	areaL	= new JLabel("Area:", SwingConstants.CENTER);
        	perimL 	= new JLabel("Perimeter:", SwingConstants.CENTER);
    
        	Container pane = getContentPane();
    		pane.setLayout (new GridLayout(4,2));
    		pane.add(lengthL);
    		pane.add(lengthTF);
    		pane.add(widthL);
    		pane.add(widthTF);
    		pane.add(areaL);
    		pane.add(areaTF);
    		pane.add(perimL);
    		pane.add(perimTF);
    
    		lengthTF = new JTextField(10);
    		widthTF  = new JTextField(10);
    		areaTF	 = new JTextField(10);
    		perimTF  = new JTextField(10);
    
    		setSize(WIDTH, HEIGHT);
    		setVisible(true);
    		setDefaultCloseOperation (EXIT_ON_CLOSE);
        }
    
        public static void main(String[] args) {
    		RectangleProgram rectProg = new RectangleProgram();
        }
    }
    Can anyone help?
    Thanks

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    Unfortunately, it doesn't tell me a line number nor a decipherable error message
    Really? Looks like line number 32 in RectangleProgram class while using the method add(). To interpret, the JLabel's and JTextField's which you are trying to add to your JPanel are never instantiated
    pwnedu46 likes this.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default

    Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Container.java:1041)
    at java.awt.Container.add(Container.java:365)
    at RectangleProgram.<init>(RectangleProgram.java:32)
    at RectangleProgram.main(RectangleProgram.java:51)
    How to read the above
    This is a trace of the call stack for when your code was executing.
    Start at the bottom and go up.
    At line 51 of RectangleProgram in the main method, there is a new statement that calls a constructor (<init>)
    At line 32 in the constructor(<init>) there is an add() statement.
    The next two lines are in the Java code, but in same format as those for your code
    The top line says what the exception is: NullPointerException
    pwnedu46 likes this.

  4. #4
    pwnedu46 is offline Member
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Default

    Thanks for the help. I had to move lines 32-35 up above line 21.

Similar Threads

  1. Throwing exceptions
    By senorbum in forum New To Java
    Replies: 7
    Last Post: 07-22-2010, 01:57 AM
  2. Exception throwing
    By andy16 in forum New To Java
    Replies: 9
    Last Post: 06-07-2010, 05:57 PM
  3. throwing exceptions in Iterator subclasses
    By hirop35 in forum New To Java
    Replies: 4
    Last Post: 10-15-2009, 09:19 PM
  4. Replies: 4
    Last Post: 08-30-2009, 11:22 AM
  5. throwing bug value
    By Dangi in forum New To Java
    Replies: 3
    Last Post: 10-25-2008, 06:14 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •