Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 17 of 17

Thread: Exception in thread "main" java.lang.NullPointerException while implementing JFrame

  1. #1
    Junior Member
    Join Date
    May 2009
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Exception in thread "main" java.lang.NullPointerException while implementing JFrame

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

    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:

    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++) {
                [B]foodList[i] = new JComboBox(foodItems);[/B]
                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) {
            [B]graphicalUI gui = new graphicalUI();[/B]
            gui.setVisible(true);
        }
     
    }

    Lines highlighted in bold are the problem lines. Any solutions will be greatly appreciated!

    Ashley


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    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..
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3

    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:

    JComboBox[] foodList = new JComboBox[4];

    And thats it!

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    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:

    JComboBox[] foodList = new JComboBox[4];
    And thats it!
    Good work leandro, I was just posting the same thing.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  5. The Following User Says Thank You to JavaPF For This Useful Post:

    houreft (September 14th, 2010)

  6. #5
    Junior Member
    Join Date
    May 2009
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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; May 6th, 2009 at 03:33 AM.

  7. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Yes oscardog, the 4 is how many values it can hold...
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  8. #7
    Junior Member
    Join Date
    May 2009
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Added new code... throws

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

    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...
            [B]foodList[0].setBounds(10,50,150, 30);[/B]
            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) {
            [B]graphicalUI gui = new graphicalUI();[/B]
            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]);
        }
            });
            }
        }
     
    }

  9. #8
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    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..
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  10. #9
    Junior Member
    Join Date
    May 2009
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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

  11. #10
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: Exception in thread "main" java.lang.NullPointerException

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

    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.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  12. #11
    Junior Member
    Join Date
    May 2009
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Oh Right! Sorry, misunderstood.

    Yes the first code, with the fix, worked fine. But then i changed it so they all used the same sort of system and it returned to the null point exceptions

    Thanks for your ongoing help!

  13. #12
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Hello oscardog,

    The problem is with all of these im afraid:

            //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]);
    I'm not sure why this is happening yet exactly but your changes to the code to use the previous fix is causing this error.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  14. The Following User Says Thank You to JavaPF For This Useful Post:

    oscardog (May 6th, 2009)

  15. #13
    Junior Member
    Join Date
    May 2009
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    I found the problem. I had to initialize Widgets each time. So each time I wanted to use one i had to use

    widgetName[number] = new widget();

    Thanks for all your help!

  16. #14
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Quote Originally Posted by oscardog View Post
    I found the problem. I had to initialize Widgets each time. So each time I wanted to use one i had to use

    widgetName[number] = new widget();

    Thanks for all your help!
    Nice one for solving that oscardog! I still didn't have a solution for you
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  17. #15
    Junior Member
    Join Date
    Aug 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Re: Exception in thread "main" java.lang.NullPointerException

    final JLabel drinkLabel = new JLabel();

    Whats the use of declaring final in front of JLabel while creating a reference?

  18. #16
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Exception in thread "main" java.lang.NullPointerException

    final means you can never re-assign a new value to that variable.

    For example, say you have a double Pi. It would be catastrophic if Pi's value was ever to change.

    However in Java, final objects are not the same as constants, as they can still be changed internally (for example you can still add/remove items from a final ArrayList).

  19. #17
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Quote Originally Posted by helloworld922 View Post
    final means you can never re-assign a new value to that variable.

    For example, say you have a double Pi. It would be catastrophic if Pi's value was ever to change.

    However in Java, final objects are not the same as constants, as they can still be changed internally (for example you can still add/remove items from a final ArrayList).
    How would PI's value change without you doing it on purpose?

Similar Threads

  1. Replies: 2
    Last Post: March 23rd, 2010, 01:38 AM
  2. Replies: 3
    Last Post: April 20th, 2009, 08:35 AM
  3. [SOLVED] "GridLayout" problem in Java program
    By antitru5t in forum AWT / Java Swing
    Replies: 3
    Last Post: April 16th, 2009, 10:26 AM
  4. Books and tutorials for beginners in Java and Visual Basic
    By Professor Spider in forum The Cafe
    Replies: 5
    Last Post: April 9th, 2009, 10:57 AM