1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Keeps returning null when passing objects between private classes

Discussion in 'Java' started by 9erNumber16, Feb 26, 2011.

  1. 9erNumber16

    9erNumber16 New Member

    Joined:
    Feb 26, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    At line 132, orderUp.getItem() keeps returning null. Why is the radio button not assigning a value at orderUp.setItem()?

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class OrderPanel extends JPanel
    {
        private JRadioButton pizza, beverage, mozzSticks, breadSticks, small, medium, large;
        private JLabel totalPrice;
        private JButton order;
        
        private static int count = 0;
        private static int total = 0;
        
        public static FoodItem orderUp = new FoodItem();
        
        public OrderPanel()
        {
            
            totalPrice = new JLabel("Total ");
            
            order = new JButton("Order");
            ButtonListener buttonListen = new ButtonListener();
            order.addActionListener(buttonListen);
        
            pizza = new JRadioButton("Pizza", true);
            pizza.setBackground(Color.red);
            beverage = new JRadioButton("Beverage");
            beverage.setBackground(Color.red);
            mozzSticks = new JRadioButton("Mozzarella Sticks");
            mozzSticks.setBackground(Color.red);
            breadSticks = new JRadioButton("Breadsticks");
            breadSticks.setBackground(Color.red);
        
            ButtonGroup items = new ButtonGroup();
            items.add(pizza);
            items.add(beverage);
            items.add(mozzSticks);
            items.add(breadSticks);
            
            ItemListener itemListen = new ItemListener();
            pizza.addActionListener(itemListen);
            beverage.addActionListener(itemListen);
            mozzSticks.addActionListener(itemListen);
            breadSticks.addActionListener(itemListen);
        
            small = new JRadioButton("Small", true);
            small.setBackground(Color.red);
            medium = new JRadioButton("Medium");
            medium.setBackground(Color.red);
            large = new JRadioButton("Large");
            large.setBackground(Color.red);
            
            SizeListener sizeListen = new SizeListener();
            small.addActionListener(sizeListen);
            medium.addActionListener(sizeListen);
            large.addActionListener(sizeListen);
        
            ButtonGroup sizes = new ButtonGroup();
            sizes.add(small);
            sizes.add(medium);
            sizes.add(large);
            
            add(pizza);
            add(beverage);
            add(mozzSticks);
            add(breadSticks);
            
            add(small);
            add(medium);
            add(large);
            
            add(totalPrice);
            add(order);
            
            setBackground(Color.RED);
            setPreferredSize(new Dimension (400, 100));
            
            
        }
        
        private class ItemListener implements ActionListener
        {
            public void actionPerformed(ActionEvent event)
            {
                Object source = event.getSource();
                //orderUp.equals(orderUp);
                if (source == pizza)
                {
                    orderUp.setItem("Pizza");
                }
                else if (source == beverage)
                {
                    orderUp.setItem("Beverage");
                }
                else if (source == mozzSticks)
                {
                    orderUp.setItem("Mozzarella Sticks");
                }
                else if (source == breadSticks)
                {
                    orderUp.setItem("Breadsticks");
                }
            }
            
        }
        
        private class SizeListener implements ActionListener
        {
            public void actionPerformed(ActionEvent event)
            {
                Object source = event.getSource();
                //orderUp.equals(orderUp);
                if (source == small)
                {
                    orderUp.setSize("Small");
                }
                else if (source == medium)
                {
                    orderUp.setSize("Medium");
                }
                else if (source == large)
                {
                    orderUp.setSize("Large");
                }
            }
        }
        
        private class ButtonListener implements ActionListener
        {
            public void actionPerformed(ActionEvent event)
            {
                int tempPrice = 0;
    
                if (orderUp.getItem().equals("Pizza"))
                {
                    if (orderUp.getSize().equals("Small"))
                    {
                        tempPrice = 7;
                    }
                    else if (orderUp.getSize().equals("Medium"))
                    {
                        tempPrice = 10;
                    }
                    else if (orderUp.getSize().equals("Large"))
                    {
                        tempPrice = 15;
                    }
                }
                else if (orderUp.equals("Beverage"))
                {
                    if (orderUp.getSize().equals("Small"))
                    {
                        tempPrice = 2;
                    }
                    else if (orderUp.getSize().equals("Medium"))
                    {
                        tempPrice = 3;
                    }
                    else if (orderUp.getSize().equals("Large"))
                    {
                        tempPrice = 4;
                    }
                }
                else if (orderUp.equals("Mozzarella Sticks"))
                {
                    if (orderUp.getSize().equals("Small"))
                    {
                        tempPrice = 5;
                    }
                    else if (orderUp.getSize().equals("Medium"))
                    {
                        tempPrice = 7;
                    }
                    else if (orderUp.getSize().equals("Large"))
                    {
                        tempPrice = 9;
                    }
                }
                else if (orderUp.equals("Breadsticks"))
                {
                    if (orderUp.getSize().equals("Small"))
                    {
                        tempPrice = 4;
                    }
                    else if (orderUp.getSize().equals("Medium"))
                    {
                        tempPrice = 6;
                    }
                    else if (orderUp.getSize().equals("Large"))
                    {
                        tempPrice = 8;
                    }
                }
                total += tempPrice;
                totalPrice.setText("Total: " + total);
                count++;
            }
        }
    }
     
  2. 9erNumber16

    9erNumber16 New Member

    Joined:
    Feb 26, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Never mind, figured it out.
     

Share This Page