2 Replies - 722 Views - Last Post: 18 December 2012 - 07:14 AM Rate Topic: -----

#1 ShawnaInMaine   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 05-May 11

Second set of eyes

Posted 17 December 2012 - 09:55 PM

My eyes are starting to cross trying to figure out this null value error I am getting. I want to make sure the rest of my code is good but the null value throws me off every time I run. Can someone be a second set of eyes and tell me what is causing the null value?

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package retailsalecalc;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

/**
 *
 * @author Shawna Rowe
 * Prg 421
 * 12/17/2012
 * Yi Mi
 * University of Phoenix
 */
public class RetailSaleCalc extends JFrame {
    
    private JPanel retailPanel; //panel to hold components
    private JTextField itemNumber; //text field for item number
    private JLabel itemNumberLabel;  //label for text field item numnber
    private JList nameBox;  //combo box for list of departments
    private JLabel deptLabel;  //label for department combo box
    private JTextField originalPrice;  //text field for original price
    private JLabel originalPriceLabel;  //label for original price text field
    private JTextField percentOff;   //text field for percentage sale discount
    private JLabel percentOffLabel;  //label for percentage discount
    private JButton addButton;      
    private JButton computeButton;
    static JTextArea listitems;
   private JLabel displayLabel;
  

    Double salePrice;
    static JPanel displayPanel;
   static String sale;
   private JLabel saleTextField;
    final int width = 1450,  //window width
                height = 900;   //window height
    
    public RetailSaleCalc()
    {
        //call the JFrame Constructor and pass the title
        super ("Retail Sales Calculator");

        //set size of window
        setSize(width, height);
        
        //set layout 
        setLayout(new GridLayout(2, 1));
        
        //specify what happens when window is closed
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
     //build panel and add to frame
         buildPanel();
         displayPanel();
        //add the panels
        add (retailPanel);
        add (displayPanel);
        
 
        pack();
        //display the window with panel
      
    
        retailPanel.setVisible(true);
        displayPanel.setVisible(true);
       
    }
    private void buildPanel(){
        //create components
          //create the panel to hold components
       retailPanel = new JPanel();
        //create custom panels
             
       retailPanel.setLayout(new GridLayout(5, 3, 50, 100));
        
        //create the label, and text field for item number
        
        itemNumberLabel = new JLabel("Enter the item number: ");
        itemNumber = new JTextField(8);
        
        
        //create the label and combo box for departments
        deptLabel = new JLabel("Choose the department for the item: ");
        String[] names = { "Womens", "Mens", "Childrens", "Toys", "Electronics"};
          nameBox = new JList(names);
        

   

  
       //create the label and text field for original price
        originalPriceLabel = new JLabel("Enter the item's original price: ");
        originalPrice = new JTextField(10);
        
                
        //create label and text field for percentage off
        percentOffLabel = new JLabel("Enter the 2 digit percent off for sale: ");
        percentOff = new JTextField(2);
    
        
        //add the item to the list button
        addButton = new JButton("Add this item to calculate sale price.");
        addButton.addActionListener(new AddButtonListener());
        
        
        //compute all items once done
       computeButton = new JButton("Done adding items COMPUTE NOW.");
        computeButton.addActionListener(new CalcButtonListener());
     
       //add components to panel
           retailPanel.add(nameBox);
           retailPanel.add(deptLabel);
             retailPanel.add(itemNumberLabel);
       retailPanel.add(itemNumber);
       retailPanel.add(originalPriceLabel);     
         retailPanel.add(originalPrice);
       retailPanel.add(percentOffLabel);
       retailPanel.add(percentOff);
         retailPanel.add(addButton);
       retailPanel.add(computeButton);
    
       
       
    }
   
    private class AddButtonListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent f)
        {
   
          String itemname = itemNumber.getText();
        String original = originalPrice.getText();
        String percentage = percentOff.getText(); 
        String dept =(String) nameBox.getSelectedValue();
       


   //get the input info and display results in list
 
         listitems = new JTextArea(6, 10);
        
    listitems.setText(dept);
    listitems.setText(itemname);
    listitems.setText(original);
    listitems.setText(percentage);

      
      
   
    //display message if user has not completed all fields
       
              
        }
    }
    private class CalcButtonListener implements ActionListener
    {  
        
        @Override
             public void actionPerformed(ActionEvent e)
            {
                //get userinput
                 String itemname = itemNumber.getText();
        String original = originalPrice.getText();
        String percentage = percentOff.getText(); 
        String dept =(String) nameBox.getSelectedValue();
    
         //when calc button is pushed convert text entered into int
                
                //after conversion compute the sale price
        double g = Double.parseDouble(original);
        double p = Double.parseDouble(percentage);
        salePrice =(g-(g*p));
        double toBeString = salePrice;
                String fromDouble ="" + toBeString; 
                fromDouble = sale;
                //update display to reflect sale price
         listitems = new JTextArea(6, 10);
        
    listitems.setText(dept);
    listitems.setText(itemname);
    listitems.setText(original);
    listitems.setText(percentage);
            }
    }
     private void displayPanel()
    {
        
             displayPanel = new JPanel();
        displayPanel.setLayout(new GridLayout(5, 3, 5, 10));
        displayPanel.add(listitems);
      displayLabel = new JLabel("Items you have added to compute sale prices.");
        saleTextField = new JLabel("Sale price is: " + sale);
  

   
   
      
    

            }

     

    
    
    
    
    
    

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
    new RetailSaleCalc().setVisible(true);
     
    }
}





errors:


run:
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1090)
at java.awt.Container.add(Container.java:410)
at retailsalecalc.RetailSaleCalc.displayPanel(RetailSaleCalc.java:196)
at retailsalecalc.RetailSaleCalc.<init>(RetailSaleCalc.java:59)
at retailsalecalc.RetailSaleCalc.main(RetailSaleCalc.java:222)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

Is This A Good Question/Topic? 0
  • +

Replies To: Second set of eyes

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: Second set of eyes

Posted 18 December 2012 - 03:02 AM

The code in displayPanel() depends on 'listitems' having been initialized, which isn't, owing to its being done in the actionPerformed of AddButtonListener
Was This Post Helpful? 2
  • +
  • -

#3 Flukeshot   User is offline

  • A little too OCD
  • member icon

Reputation: 418
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Second set of eyes

Posted 18 December 2012 - 07:14 AM

I don't intend to sound mean, but your eyes might have an easier time in future if you made your code structure a little neater. I may have a touch of OCD but I find that if my code is slightly out of structure, it's harder for me to read it.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1