View Single Post
  #1 (permalink)  
Old 05-05-2009, 12:47 PM
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 Exception in thread "main" java.lang.NullPointerException

So i'm doing this thing for one of my classes and it's so close to working except it throws the following exception:

Quote:
Exception in thread "main" java.lang.NullPointerException
at graphicalui.graphicalUI.<init>(graphicalUI.java:95 )
at graphicalui.graphicalUI.main(graphicalUI.java:193)
Java Result: 1
And here is my code:

Java Code
package graphicalui;

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

public class graphicalUI extends JFrame {
    
    JComboBox[] foodList;
    int i = 0;
    /** 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);
        
        JPanel panel = new JPanel();
        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 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 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 String[] rowCountList = {"1", "2", "3", "4", "5", "6","7","8","9","10"};
        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);
        
        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" };
        
        final JButton calculateButton = new JButton("Calculate");
        calculateButton.setBounds(875, 400, 100, 30);
        panel.add(calculateButton);
        /* END OF NON-LOOP CODE */
        
        
        //Creating the combo boxes...
        for(i = 0; i < 4; i++) {
            foodList[i] = new JComboBox(foodItems);
            foodList[i].setBounds(i*10,50,150, 30);
            panel.add(foodList[i]);

        final JLabel foodLabel = new JLabel();
        foodLabel.setBounds(160, 55, 50, 20);
        panel.add(foodLabel);
        
        final JComboBox drinkList = new JComboBox(drinkItems);
        drinkList.setBounds(360, 50, 125, 30);
        panel.add(drinkList);
        
        final JLabel drinkLabel = new JLabel();
        drinkLabel.setBounds(490, 55, 50, 20);
        panel.add(drinkLabel);
        
        final JComboBox pizzaType = new JComboBox(pizzaTypesList);
        pizzaType.setBounds(205, 50, 150, 30);
        panel.add(pizzaType);
        
        final JComboBox drinkType = new JComboBox(drinkTypesList);
        drinkType.setBounds(530, 50, 150, 30);
        panel.add(drinkType);
        
        final JLabel quantityLabel = new JLabel("Quantity:");
        quantityLabel.setBounds(690, 50, 50, 30);
        panel.add(quantityLabel);
        
        final JTextField quantityField = new JTextField();
        quantityField.setBounds(750, 50, 50, 30);
        panel.add(quantityField);
        
        final JLabel subTotalLabel = new JLabel("Sub-Total:");
        subTotalLabel.setBounds(810, 50, 250, 30);
        panel.add(subTotalLabel);
        
        final JLabel subTotal = new JLabel("");
        subTotal.setBounds(880, 50, 100, 30);
        panel.add(subTotal);
        
        final JLabel subTotal2 = new JLabel("£0.00");
        subTotal2.setBounds(875, 50, 100, 30);
        panel.add(subTotal2);
       
        
        calculateButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                String receivedQuantity = quantityField.getText();
                int finalQuantity = Integer.parseInt(receivedQuantity);
                int selectedFoodIndex2 = foodList[i].getSelectedIndex();
                int selectedDrinkIndex2 = drinkList.getSelectedIndex();
                double finalTotal = Double.parseDouble(foodPrices[selectedFoodIndex2]) + Double.parseDouble(foodPrices[selectedDrinkIndex2]);
                double finalSubTotal = finalTotal * finalQuantity;
                String finalSubTotalDisplay = Double.toString(finalSubTotal);
                subTotal2.setText("£" + finalSubTotalDisplay);
            }
        });
        
       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);
            }
        });

        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.setText("£" + foodPrices[selectedFoodIndex]);
    }
        });
        
        drinkList.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        JComboBox cbDrink = (JComboBox)e.getSource();
        String selectedDrink = (String)cbDrink.getSelectedItem();
        int selectedDrinkIndex = cbDrink.getSelectedIndex();
        drinkLabel.setText("£" + drinkPrices[selectedDrinkIndex]);
    }
        });
        
    }
    }

    public static void main(String[] args) {
        graphicalUI gui = new graphicalUI();
        gui.setVisible(true);
    }
    
}
Lines highlighted in bold are the problem lines. Any solutions will be greatly appreciated!

Ashley
Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence