Results 1 to 3 of 3
  1. #1
    mcashe is offline Member
    Join Date
    Aug 2009
    Posts
    8
    Rep Power
    0

    Default NullPointerException: I can't get rid of it.

    Guys/Gals,
    I am desperately trying to get rid of this nullpointerexception but for the life of me i can't figure out where it's coming from... It happens upon entry into the createShowGUI method.

    Java Code:
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.JTextField;
    
    public class TripCalculatorFrame
    {
        private static JFrame frame;
        
        private static JPanel panelOfComponents;
        private static JPanel panelCalculationType;
        private static JPanel panelOfInputs;
        private static JPanel panelOfButtons;
        
        private static JLabel lblTripDistance;
        private static JLabel lblTripSpeed;
        private static JLabel lblTripDuration;
        
        private static JTextField txtTripDistance;
        private static JTextField txtTripSpeed;
        private static JTextField txtTripDuration;
        
        private static JRadioButton rbtnCalcTripSpeed;
        private static JRadioButton rbtnCalcTripDuration;
    
        private static JButton btnCalcTripSpeed;
        private static JButton btnCalcTripDuration;    
        
        private static GridBagLayout      gridbagLayout;
        private static GridBagConstraints gridbagConstraints;
        
        public TripCalculatorFrame()
        {
            frame = new JFrame("Trip Calculator");
            
            panelOfComponents    = new JPanel();
            panelCalculationType = new JPanel();
            panelOfInputs        = new JPanel();
            panelOfButtons       = new JPanel();
            
            rbtnCalcTripSpeed    = new JRadioButton("Calculate Trip Speed");
            rbtnCalcTripDuration = new JRadioButton("Calculate Trip Duration");
            
            lblTripDistance = new JLabel("Trip Distance (in miles)");
            lblTripDuration = new JLabel("Trip Duration (in minutes)");
            lblTripSpeed    = new JLabel("Trip Speed (in mph)");
            
            txtTripDistance = new JTextField();
            txtTripDuration = new JTextField();
            txtTripSpeed    = new JTextField();
            
            btnCalcTripSpeed    = new JButton("Calculate Trip Speed");
            btnCalcTripDuration = new JButton("Calculate Trip Duration");
            
            gridbagLayout      = new GridBagLayout();
            gridbagConstraints = new GridBagConstraints();
        }
    
        private static void addComponentsToFrame(Container container)
        {
            panelOfComponents.setLayout(gridbagLayout);
            
            // PanelCalculationType
            gridbagConstraints.gridx = 0;
            gridbagConstraints.gridy = 0;
            gridbagConstraints.gridwidth = 2;
            gridbagConstraints.gridheight = 1;
            panelOfComponents.add(addPanelCalculationTypeToContainer());
            
            // PanelOfInputs
            gridbagConstraints.gridx = 0;
            gridbagConstraints.gridy = 2;
            gridbagConstraints.gridwidth = 3;
            gridbagConstraints.gridheight = 3;        
            panelOfComponents.add(addPanelOfInputsToContainer());
            
            // PanelOfButtons
            gridbagConstraints.gridx = 0;
            gridbagConstraints.gridy = 6;
            gridbagConstraints.gridwidth = 3;
            gridbagConstraints.gridheight = 1;     
            panelOfComponents.add(addPanelOfButtonsToContainer());
            
            container.add(panelOfComponents);
        }    
        
        private static JPanel addPanelCalculationTypeToContainer()
        {
            gridbagLayout.setConstraints(panelCalculationType, gridbagConstraints);
            
            panelCalculationType.add(rbtnCalcTripSpeed);
            panelCalculationType.add(rbtnCalcTripDuration);
            
            return panelCalculationType;
        }
        
        private static JPanel addPanelOfInputsToContainer()
        {
            gridbagLayout.setConstraints(panelOfInputs, gridbagConstraints);
            
            panelOfInputs.add(lblTripDistance);
            panelOfInputs.add(txtTripDistance);
            panelOfInputs.add(lblTripDuration);
            panelOfInputs.add(txtTripDuration);
            panelOfInputs.add(lblTripSpeed);
            panelOfInputs.add(txtTripSpeed);
            
            return panelOfInputs;
        }
        
        private static JPanel addPanelOfButtonsToContainer()
        {
            gridbagLayout.setConstraints(panelOfButtons, gridbagConstraints);
            
            panelOfButtons.add(btnCalcTripDuration);
            panelOfButtons.add(btnCalcTripSpeed);
            
            return panelOfButtons;
        }
        
        public static void createAndShowGUI()
        {
            Dimension screenDimension = frame.getToolkit().getScreenSize();
            addComponentsToFrame(frame.getContentPane());
    
            frame.setLocation(screenDimension.width/2 - 50, 
                              screenDimension.height/2 - 50);
            frame.pack();
            frame.setVisible(true);
            WindowListener windowCloser = new WindowAdapter()
            {
                public void windowClosing(WindowEvent e)
                {
                    System.exit(0);
                }
            };
            frame.addWindowListener(windowCloser);            
        }
    }

  2. #2
    weddy69 is offline Member
    Join Date
    Aug 2009
    Posts
    5
    Rep Power
    0

    Default

    where did you ever set the bounds or screen size of your program? your getting a dimension that was never initialized.

    second personally i would not use static variables unless I had to, all your class variables are static???

  3. #3
    Spiller is offline Member
    Join Date
    Aug 2009
    Posts
    4
    Rep Power
    0

    Default

    I do wonder why you've done everything like this (static). Absolutely none of that needs to be static.

    The frame variable will be null until you've called the TripCalculatorFrame constructor, at which point I assume you discard the object, since its only members are static. You are instantiating the frame first before calling createAndShowGUI(), right? Sorry if I'm asking the obvious, but there's no other reason why that should throw a null pointer exception.

Similar Threads

  1. NullPointerException
    By tommyyyy in forum New To Java
    Replies: 9
    Last Post: 03-26-2009, 11:51 PM
  2. NullPointerException
    By Aika in forum New To Java
    Replies: 8
    Last Post: 11-19-2008, 12:34 AM
  3. NullPointerException
    By mensa in forum Java 2D
    Replies: 5
    Last Post: 05-04-2008, 12:19 AM
  4. NullPointerException
    By ravian in forum New To Java
    Replies: 2
    Last Post: 12-07-2007, 05:20 PM
  5. NullPointerException
    By Feng in forum New To Java
    Replies: 5
    Last Post: 11-24-2007, 08:51 PM

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
  •