Results 1 to 5 of 5
  1. #1
    joshmo72 is offline Member
    Join Date
    Nov 2013
    Rep Power

    Default Getting error messages when trying to run GUI.

    This is a GUI that I had to design for a Java 1 class. It compiles but when I try to run I get the following error messages:
    Java Code:
    Exception in thread "main" java.lang.NullPointerException
    	at java.awt.Container.addImpl(
    	at java.awt.Container.add(
    	at Lab7GUI.buildOutputPanel(
    	at Lab7GUI.<init>(
    	at Lab7GUIClient.main(
    My professor has been little to no help, as I am not sure that she knows what she is even doing. Below is the code for the service class, sorry it is so much. Any help would be greatly appreciated.

    Java Code:
    import java.awt.*;
    import javax.swing.* ; 
    import java.awt.event.*; 
    public class Lab7GUI extends JFrame
       private JPanel topPanel;
       private JPanel picturePanel;
       private JPanel centerPanel;
       private JPanel buttonPanel;
       private JPanel outputPanel;
       private JButton calcButton;
       private JLabel nameLabel;
       private JLabel classLabel;
       private JLabel welcomeLabel;
       private JLabel pictureLabel;
       private JLabel instructionLabel;
       private JLabel chickPromptLabel;
       private JLabel salPromptLabel;
       private JLabel chixChargeLabel;
       private JLabel salChargeLabel;
       private JLabel sandTotalLabel;
       private JLabel topChargeLabel;
       private JLabel orderAmountLabel;
       private JLabel serviceChargeLabel;
       private JLabel totalBillLabel;
       private JTextField chickenField;
       private JTextField salmonField;
       private final int WINDOW_WIDTH = 600;
       private final int WINDOW_HEIGHT =400;
       private final double CHICKEN_SANDWICH = 4.59;
       private final double SALMON_SANDWICH = 4.99;
       public Lab7GUI()
           //set window title
           setTitle("Lab #7 Part 2 GUI");
           //set the size of the window
           setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
           //set the background color
           //specify what happens when the close button is clicked
           //add BorderLayout
           setLayout(new BorderLayout());
           //build the top panel and add it to the frame
           //add the top panel to frame's content pane
           add(topPanel, BorderLayout.NORTH);
           //build the picture panel and add it to the frame
           //add picture panel to frame's content pane
           add(picturePanel, BorderLayout.NORTH);
           //build the center panel and add it to the frame
           //add center panel to the frame's content pane
           add(centerPanel, BorderLayout.CENTER);
           //build the button panel and add it to the frame
           //add button panel to the frame's content pane
           add(buttonPanel, BorderLayout.CENTER);
           //build the output panel and add it to the frame
           //add output panel to the frame's content pane
           add(outputPanel, BorderLayout.SOUTH);
           //display the window and pack
       }   //end constructor
     *   This method adds the necessary labels to the topPanel for display on the GUI.
     *   @param  none
     *   @return none
       private void buildTopPanel()
           //create name label to display name
           nameLabel = new JLabel("Programmer: Joshua Morgan");
           //create class label to display class/section
           classLabel = new JLabel("\nCSI 161 / Section 875");
           //create welcome label to display welcome message
           welcomeLabel = new JLabel("\nWelcome to Barker's Sandwich Shop Menu: ");
           //create a JPanel object and let the topPanel field reference it
           topPanel = new JPanel();
           //add the label compents to the panel
       }   //end buildTopPanel()
     *   This method adds a picture of a sandwich to the GUI.
     *   @param  none
     *   @return none
       private void buildPicturePanel()
           //create picture label to display a picture
           pictureLabel = new JLabel();
           JLabel pictureLabel = new JLabel(new ImageIcon("sandwich.png"));
           //creat a JPanel object and let the picturePanel reference it
           picturePanel = new JPanel();
           //add the picture label to the panel
       }   //end buildPicturePanel
     *   This method method adds necessary labels and text fields to the center panel for display
     *   @param  none
     *   @return none
       private void buildCenterPanel()
           //create instruction label for instructions on how to use menu
           instructionLabel = new JLabel("Enter Number of Sandwiches for each; 0 if not ordering");
           //create chicken sandwich prompt label and set color
           chickPromptLabel = new JLabel("Chicken Sandwiches @ $4.59 each  ");
           //create text field for the chicken sandwich 10 characters wide
           chickenField = new JTextField(10);
           //create salmon sandwich promprt label and set color
           salPromptLabel = new JLabel("Salmon Sandwiches @ $4.99 each ");
           //create text foeld for the salmon sandwich 10 characters wide
           salmonField = new JTextField(10);
           //create a JPanel object and let the centerPanel reference it
           centerPanel = new JPanel();
           //add the labels and fields to the center panel
       }   //end buildCenterPanel
     *   This method adds the calculate button to the button panel and adds an action listener
     *   @param  none
     *   @return none
        private void buildButtonPanel() 
            //create a button with the caption "Calculate Bill"
            calcButton = new JButton("Calculate Bill");
            //add an action listener to the button
            calcButton.addActionListener(new CalcButtonListener());
            //create a JPanel object and let the buttonPanel field reference it
            buttonPanel = new JPanel();
            //add the button to the panel
        }   //end button panel
     *   CalcButtonListener is an action listener class for the calculate button
     *   @param e   the event object
         private class CalcButtonListener implements ActionListener
             public void actionPerformed(ActionEvent e)
                 int numChicken = 0;   //to hold number of chicken sandwiches
                 int numSalmon = 0;   //to hold number of salmon sandwiches
                 int numSandwiches = 0; // holds total number of sandwiches for topping charge
                 double toppingCharges = 0.00;
                 double serviceCharge = 0.00;
                 double totalBill = 0.00;
                 double orderAmount = 0.00;   //sum of charges for all sandwiches plus the topping charge
                 double chickCharge = 0.00;   //holds charges for number of chicken sandwiches
                 double salmonCharge = 0.00;
                 double sandwichTotal = 0.00;   //charge for sandwiches before toppings
                 //get the text entered by the user for number of sandwiches
                 String chickStr = chickenField.getText();   //read string from chicken text field
                 numChicken = Integer.parseInt(chickStr);    //convert string read to int
                 String salmonStr = salmonField.getText();   //read string from salmon text field
                 numSalmon = Integer.parseInt(salmonStr);   //convert string read to int
                 //compute outputs and display labels
                 chickCharge = numChicken * CHICKEN_SANDWICH;
                 chixChargeLabel = new JLabel("Charge for Chicken Sandwiches\t\t $ " + chickCharge);
                 salmonCharge = numSalmon * SALMON_SANDWICH;
                 salChargeLabel = new JLabel("Charge for Salmn Sandwiches\t\t $ " + salmonCharge);
                 sandwichTotal = chickCharge + salmonCharge;
                 sandTotalLabel = new JLabel("Total for Sandwiches\t\t $ " + sandwichTotal);
                 numSandwiches = numChicken + numSalmon;
                 toppingCharges = numSandwiches * .70;
                 topChargeLabel = new JLabel("topping Charges\t\t $ " + toppingCharges);
                 orderAmount = sandwichTotal + toppingCharges;
                 orderAmountLabel = new JLabel("Charge for Sandwiches and Topping $ " + orderAmount);
                 serviceCharge = 0.15 * orderAmount;
                 serviceChargeLabel = new JLabel("Charge for Service\t\t $ " + serviceCharge);
                 totalBill = orderAmount + serviceCharge;
                 totalBillLabel = new JLabel("Total Bill\t\t = $" + totalBill); 
             }   //end actionPerformed
         }   //end calcButtonListner class
          private void buildOutputPanel()
                 //create a JPanel object and let the southPanel reference it
                 outputPanel = new JPanel();
                 //add the label to the south panel
          }   //end output panel
    }   //end service class
    Last edited by JosAH; 11-20-2013 at 08:24 PM. Reason: fixed the [code] ... [/tags]

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Rep Power

    Default Re: Getting error messages when trying to run GUI.

    The labels used in buildOutputPanel are all null, as they are instanciated in the actionListener which is not triggered until the GUI is up and running.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    joshmo72 is offline Member
    Join Date
    Nov 2013
    Rep Power

    Default Re: Getting error messages when trying to run GUI.

    So, it should be placed before the action listener?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power

    Default Re: Getting error messages when trying to run GUI.

    e.g. you create the totalBillLabel in your ActionListener but you're trying to use it in the buildOutputPanel; it isn't created yet when that method is called.

    kind regards,

    Build a wall around Donald Trump; I'll pay for it.

  5. #5
    joshmo72 is offline Member
    Join Date
    Nov 2013
    Rep Power

    Default Re: Getting error messages when trying to run GUI.

    ok, I see. Thank you.

Similar Threads

  1. Handling Error Messages?
    By DoubleDee in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 12-29-2011, 12:19 PM
  2. Help, please. I don't understand these error messages.
    By DarkExperience in forum New To Java
    Replies: 32
    Last Post: 12-05-2011, 07:58 PM
  3. Cannot figure out error messages
    By Teclis in forum New To Java
    Replies: 6
    Last Post: 03-29-2011, 09:31 PM
  4. [SOLVED] error messages
    By raghu9198 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-10-2008, 06:21 AM
  5. Error Messages Not Showing Up
    By nvidia in forum Web Frameworks
    Replies: 0
    Last Post: 04-07-2008, 10:41 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