Go Back   Java Programming Forums > Java Standard Edition Programming Help > Exceptions


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-05-2009, 12:47 PM
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
  #2 (permalink)  
Old 05-05-2009, 02:02 PM
JavaPF's Avatar
mmm.. coffee
 
8 Highscores

Join Date: May 2008
Location: United Kingdom
Posts: 1,543
Thanks: 98
Thanked 92 Times in 85 Posts
JavaPF is someone you want to know!JavaPF is someone you want to know!JavaPF is someone you want to know!

I'm feeling Relaxed
Default Re: Exception in thread "main" java.lang.NullPointerException

Hello oscardog and welcome to the Java Programming Forums

The NullPointerException is pointing here:

foodList[i] = new JComboBox(foodItems);

It looks like a problem with your foodList array. I'm taking a better look at the code now..
__________________
Don't forget to add syntax highlighted code tags around your code: [highlight=Java] code here [/highlight]

Forum Tip: Add to peoples reputation () by clicking the button on their useful posts.
Reply With Quote
  #3 (permalink)  
Old 05-05-2009, 02:03 PM
leandro's Avatar
Member
 

Join Date: Mar 2009
Posts: 30
Thanks: 0
Thanked 3 Times in 3 Posts
leandro is on a distinguished road
Default Re: Exception in thread "main" java.lang.NullPointerException

Hi Ashley,

You forgot to initialize your foodList var:

Put this code when you declare the var:

Java Code
JComboBox[] foodList = new JComboBox[4];
And thats it!
Reply With Quote
  #4 (permalink)  
Old 05-05-2009, 02:33 PM
JavaPF's Avatar
mmm.. coffee
 
8 Highscores

Join Date: May 2008
Location: United Kingdom
Posts: 1,543
Thanks: 98
Thanked 92 Times in 85 Posts
JavaPF is someone you want to know!JavaPF is someone you want to know!JavaPF is someone you want to know!

I'm feeling Relaxed
Thumbs up Re: Exception in thread "main" java.lang.NullPointerException

Quote:
Originally Posted by leandro View Post
Hi Ashley,

You forgot to initialize your foodList var:

Put this code when you declare the var:

Java Code
JComboBox[] foodList = new JComboBox[4];
And thats it!
Good work leandro, I was just posting the same thing.
__________________
Don't forget to add syntax highlighted code tags around your code: [highlight=Java] code here [/highlight]

Forum Tip: Add to peoples reputation () by clicking the button on their useful posts.
Reply With Quote
  #5 (permalink)  
Old 06-05-2009, 08:24 AM
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

Quote:
Originally Posted by JavaPF View Post
Good work leandro, I was just posting the same thing.
Thanks - it now works.

Is the 4 in the [] how many combo boxes im going to declare?

Edit: Answered my own question, cheers!

Oscardog

Last edited by oscardog; 06-05-2009 at 08:33 AM.
Reply With Quote
  #6 (permalink)  
Old 06-05-2009, 08:40 AM
JavaPF's Avatar
mmm.. coffee
 
8 Highscores

Join Date: May 2008
Location: United Kingdom
Posts: 1,543
Thanks: 98
Thanked 92 Times in 85 Posts
JavaPF is someone you want to know!JavaPF is someone you want to know!JavaPF is someone you want to know!

I'm feeling Relaxed
Default Re: Exception in thread "main" java.lang.NullPointerException

Yes oscardog, the 4 is how many values it can hold...
__________________
Don't forget to add syntax highlighted code tags around your code: [highlight=Java] code here [/highlight]

Forum Tip: Add to peoples reputation () by clicking the button on their useful posts.
Reply With Quote
  #7 (permalink)  
Old 06-05-2009, 09:37 AM
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
  #8 (permalink)  
Old 06-05-2009, 10:31 AM
JavaPF's Avatar
mmm.. coffee
 
8 Highscores

Join Date: May 2008
Location: United Kingdom
Posts: 1,543
Thanks: 98
Thanked 92 Times in 85 Posts
JavaPF is someone you want to know!JavaPF is someone you want to know!JavaPF is someone you want to know!

I'm feeling Relaxed
Default Re: Exception in thread "main" java.lang.NullPointerException

Hey oscardog.

This is a new error with your updated code. When adding the fix to the code you posted originally it all seems to work fine..
__________________
Don't forget to add syntax highlighted code tags around your code: [highlight=Java] code here [/highlight]

Forum Tip: Add to peoples reputation () by clicking the button on their useful posts.
Reply With Quote
  #9 (permalink)  
Old 06-05-2009, 10:40 AM
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

No? Because the fix I applied, adding the "new JComboBox[10];" is on the second code yet it still throws the error?

Or did I miss a fix because i'm sure that was the fix...

Oscardog
Reply With Quote
  #10 (permalink)  
Old 06-05-2009, 10:50 AM
JavaPF's Avatar
mmm.. coffee
 
8 Highscores

Join Date: May 2008
Location: United Kingdom
Posts: 1,543
Thanks: 98
Thanked 92 Times in 85 Posts
JavaPF is someone you want to know!JavaPF is someone you want to know!JavaPF is someone you want to know!

I'm feeling Relaxed
Default Re: Exception in thread "main" java.lang.NullPointerException

I'm talking about this original code which seems to differ from the one above.

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

public class graphicalUI extends JFrame {
    
    JComboBox[] foodList = new JComboBox[4];
    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);
    }
    
}
This compiles fine. I'm not sure what is going on with your updated version.. I'm looking into it now.
__________________
Don't forget to add syntax highlighted code tags around your code: [highlight=Java] code here [/highlight]

Forum Tip: Add to peoples reputation () by clicking the button on their useful posts.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception in thread "main" java.util.IllegalFormatWidthException: 13 xtrive Exceptions 2 23-03-2010 05:38 AM
JTextPane does not display after text initialized with "" orfano AWT / Java Swing 3 20-04-2009 01:35 PM
[SOLVED] Problem with Java: "GridLayout" laying out the top part of my assignment? antitru5t AWT / Java Swing 3 16-04-2009 03:26 PM
Oh dear, an "I'm a beginner" thread Professor Spider The Cafe 5 09-04-2009 03:57 PM


100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java addactionlistener addactionlistener java convert double to integer java double format java double to integer in java double to integer java drag en drop programmeren java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double in java format double java get mouse position java java 2d arraylist java actionlistener java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming practice problems java send keystrokes to another application java two dimensional arraylist java.io.ioexception: premature eof java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton action jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics smack api two dimensional arraylist two dimensional arraylist java unable to sendviapost to url what is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

All times are GMT. The time now is 02:07 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.