View Single Post
  #7 (permalink)  
Old 06-05-2009, 09:37 AM
oscardog oscardog is offline
Junior Member
 

Join Date: May 2009
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
oscardog is on a distinguished road
Default Re: Exception in thread "main" java.lang.NullPointerException

Added new code... throws

Quote:
Exception in thread "main" java.lang.NullPointerException
at graphicalui.graphicalUI.<init>(graphicalUI.java:10 6)
at graphicalui.graphicalUI.main(graphicalUI.java:172)
Duno why

Java Code
package graphicalui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class graphicalUI extends JFrame {
    
    int i;
    int q;
    JComboBox[] foodList = new JComboBox[10];
    JLabel[] foodLabel = new JLabel[10];
    JComboBox[] drinkList = new JComboBox[10];
    JLabel[] drinkLabel = new JLabel[10];
    JComboBox[] pizzaType = new JComboBox[10];
    JComboBox[] drinkType = new JComboBox[10];
    JLabel[] quantityLabel = new JLabel[10];
    JTextField[] quantityField = new JTextField[10];
    JLabel[] subTotalLabel = new JLabel[10];
    JLabel[] subTotal = new JLabel[10];
    JLabel[] subTotal2 = new JLabel[10];
    JButton calculateButton = new JButton();
    JPanel panel = new JPanel();     
    
        final String[] foodItems = { "", "Cheese and Tomato", "Ham and Pineapple", "Vegetarian", "Meat Feast", "Seafood" };
        final String[] foodPrices = { "", "3.50", "4.20", "5.20", "5.80", "5.60" };
        final String[] tableNumbersList = {"1", "2", "3", "4", "5", "6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25" };
        final String[] rowCountList = {"1", "2", "3", "4", "5", "6","7","8","9","10"};
        final String[] drinkItems = { "", "Cola", "Lemonade", "Fizzy Orange" };
        final String[] drinkPrices = { "", "0.90", "0.80", "0.90" };
        final String[] pizzaTypesList = {"", "Thin and Crispy", "Traditional" };
        final String[] drinkTypesList = {"", "With Ice", "Without Ice" };
    /** Creates a new instance of graphicalUI */
    public graphicalUI() {
        /* START OF NON-LOOPING CODE */
        setSize(1000,500);
        setTitle("Microsoft Cake");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        
        final Toolkit toolkit = getToolkit();
        Dimension size = toolkit.getScreenSize();
        setLocation(size.width/2 - getWidth()/2, size.height/2 - getHeight()/2);
        
        getContentPane().add(panel);
        panel.setLayout(null);
        

        
// Creates a menubar for a JFrame
        JMenuBar menuBar = new JMenuBar();
        
        // Add the menubar to the frame
        setJMenuBar(menuBar);
        
        // Define and add two drop down menu to the menubar
        JMenu fileMenu = new JMenu("File");
        JMenu editMenu = new JMenu("Edit");
        menuBar.add(fileMenu);
        menuBar.add(editMenu);
        
        // Create and add simple menu item to one of the drop down menu
        JMenuItem newAction = new JMenuItem("New");
        JMenuItem openAction = new JMenuItem("Open");
        JMenuItem exitAction = new JMenuItem("Exit");
        JMenuItem cutAction = new JMenuItem("Cut");
        JMenuItem copyAction = new JMenuItem("Copy");
        JMenuItem pasteAction = new JMenuItem("Paste");
        
        // Create a ButtonGroup and add both radio Button to it. Only one radio
        // button in a ButtonGroup can be selected at a time.
        ButtonGroup bg = new ButtonGroup();
        fileMenu.add(newAction);
        fileMenu.add(openAction);
        fileMenu.addSeparator();
        fileMenu.add(exitAction);
        editMenu.add(cutAction);
        editMenu.add(copyAction);
        editMenu.add(pasteAction);
        
        
        
        final JComboBox tableNumbers = new JComboBox(tableNumbersList);
        tableNumbers.setBounds(65, 10, 50, 20);
        panel.add(tableNumbers);
        
        final JLabel tableNumbersLabel = new JLabel("Table No:");
        tableNumbersLabel.setBounds(5,10,60,20);
        panel.add(tableNumbersLabel);

        final JComboBox rowCountCombo = new JComboBox(rowCountList);
        rowCountCombo.setBounds(940, 10, 50, 20);
        panel.add(rowCountCombo);
        
        final JLabel rowCountLabel = new JLabel("Rows:");
        rowCountLabel.setBounds(900,10,50,20);
        panel.add(rowCountLabel);

        calculateButton.setBounds(875, 400, 100, 30);
        panel.add(calculateButton);
        /* END OF NON-LOOP CODE */
        
        
        //Creating the combo boxes...
        foodList[0].setBounds(10,50,150, 30);
        panel.add(foodList[0]);

        foodLabel[0].setBounds(160, 55, 50, 20);
        panel.add(foodLabel[0]);
        
        drinkList[0].setBounds(360, 50, 125, 30);
        panel.add(drinkList[0]);
        
        drinkLabel[0].setBounds(490, 55, 50, 20);
        panel.add(drinkLabel[0]);
        
        pizzaType[0].setBounds(205, 50, 150, 30);
        panel.add(pizzaType[0]);
        
        drinkType[0].setBounds(530, 50, 150, 30);
        panel.add(drinkType[0]);
        
        quantityLabel[0].setBounds(690, 50, 50, 30);
        panel.add(quantityLabel[0]);
        
        quantityField[0].setBounds(750, 50, 50, 30);
        panel.add(quantityField[0]);
        
        subTotalLabel[0].setBounds(810, 50, 250, 30);
        panel.add(subTotalLabel[0]);
        
        subTotal[0].setBounds(880, 50, 100, 30);
        panel.add(subTotal[0]);
        
        subTotal2[0].setBounds(875, 50, 100, 30);
        panel.add(subTotal2[0]);
       
        rowCountCombo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                JComboBox cbRow = (JComboBox)event.getSource();
                String receivedRowCount = (String)cbRow.getSelectedItem();
                int sentRowCount = Integer.parseInt(receivedRowCount);
                generateRows(sentRowCount);
            }
        });
        
        
        
       newAction.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                //Add in stuff here
            }
        });
        
        openAction.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                //Add in stuff here
            }
        });
        
        exitAction.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                System.exit(0);
            }
        });

        
    }

    public static void main(String[] args) {
        graphicalUI gui = new graphicalUI();
        gui.setVisible(true);
    }
    
    void generateRows(int rowCountReceived) {
        for(i = 1; i < rowCountReceived; i++) {
        q = 50*i;
        foodList[i].setBounds(10,50+q,150, 30);
        panel.add(foodList[i]);

        foodLabel[i].setBounds(160, 55+q, 50, 20);
        panel.add(foodLabel[i]);
        
        drinkList[i].setBounds(360, 50+q, 125, 30);
        panel.add(drinkList[i]);
        
        drinkLabel[i].setBounds(490, 55+q, 50, 20);
        panel.add(drinkLabel[i]);
        
        pizzaType[i].setBounds(205, 50+q, 150, 30);
        panel.add(pizzaType[i]);
        
        drinkType[i].setBounds(530, 50+q, 150, 30);
        panel.add(drinkType[i]);
        
        quantityLabel[i].setBounds(690, 50+q, 50, 30);
        panel.add(quantityLabel[i]);
        
        quantityField[i].setBounds(750, 50+q, 50, 30);
        panel.add(quantityField[i]);
        
        subTotalLabel[i].setBounds(810, 50+q, 250, 30);
        panel.add(subTotalLabel[i]);
        
        subTotal[i].setBounds(880, 50+q, 100, 30);
        panel.add(subTotal[i]);
        
        subTotal2[i].setBounds(875, 50+q, 100, 30);
        panel.add(subTotal2[i]);
        
        calculateButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                String receivedQuantity = quantityField[i].getText();
                int finalQuantity = Integer.parseInt(receivedQuantity);
                int selectedFoodIndex2 = foodList[i].getSelectedIndex();
                int selectedDrinkIndex2 = drinkList[i].getSelectedIndex();
                double finalTotal = Double.parseDouble(foodPrices[selectedFoodIndex2]) + Double.parseDouble(foodPrices[selectedDrinkIndex2]);
                double finalSubTotal = finalTotal * finalQuantity;
                String finalSubTotalDisplay = Double.toString(finalSubTotal);
                subTotal2[i].setText("£" + finalSubTotalDisplay);
            }
        });
        
        foodList[i].addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        JComboBox cbFood = (JComboBox)e.getSource();
        String selectedFood = (String)cbFood.getSelectedItem();
        int selectedFoodIndex = cbFood.getSelectedIndex();
        foodLabel[i].setText("£" + foodPrices[selectedFoodIndex]);
    }
        });
        
        drinkList[i].addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        JComboBox cbDrink = (JComboBox)e.getSource();
        String selectedDrink = (String)cbDrink.getSelectedItem();
        int selectedDrinkIndex = cbDrink.getSelectedIndex();
        drinkLabel[i].setText("£" + drinkPrices[selectedDrinkIndex]);
    }
        });
        }
    }
    
}
Reply With Quote