471,337 Members | 996 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

help with Exception in thread "main" java.lang.NullPointerException

HI i am writing this for college i know i have loads of combo boxes with nothing in the i havent got that far yet. but every time i run this is comes up with this erro
run:
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1041)
at java.awt.Container.add(Container.java:365)
at orderingsystem.OrderingSystem.<init>(OrderingSyste m.java:261)
at orderingsystem.OrderingSystem.main(OrderingSystem. java:361)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
thanks for any help


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package orderingsystem;

import javax.swing.*;
import java.lang.String;
import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat;
import java.util.Locale;


/**
*
* @author Scott.Emberson12
*/
public class OrderingSystem extends JFrame {
//widgets

//Genral widegets
JPanel panelWidget;
Toolkit toolkit;
JButton buttonWidget;
//Jlabels
JLabel PizzaShackWidget;
JLabel priceLabel;
JLabel subtotalLabel;
JLabel totalLabel;
JLabel numberLabel1;
JLabel numberLabel2;
JLabel numberLabel3;
JLabel numberLabel4;
JLabel numberLabel5;
JLabel numberLabel6;
JLabel numberLabel7;
JLabel numberLabel8;
JLabel numberLabel9;
JLabel numberLabel10;

//JCombox
JComboBox DrinksComboBoxWidget;
JComboBox DrinksCombo1;
JComboBox PizzaCombo1;
JComboBox PizzaBaseCombo1;
JComboBox IceCombo1;

JComboBox DrinksCombo2;
JComboBox PizzaCombo2;
JComboBox PizzaBaseCombo2;
JComboBox IceCombo2;

JComboBox DrinksCombo3;
JComboBox PizzaCombo3;
JComboBox PizzaBaseCombo3;
JComboBox IceCombo3;

JComboBox DrinksCombo4;
JComboBox PizzaCombo4;
JComboBox PizzaBaseCombo4;
JComboBox IceCombo4;

JComboBox DrinksCombo5;
JComboBox PizzaCombo5;
JComboBox PizzaBaseCombo5;
JComboBox IceCombo5;

JComboBox DrinksCombo6;
JComboBox PizzaCombo6;
JComboBox PizzaBaseCombo6;
JComboBox IceCombo6;

JComboBox DrinksCombo7;
JComboBox PizzaCombo7;
JComboBox PizzaBaseCombo7;
JComboBox IceCombo7;

JComboBox DrinksCombo8;
JComboBox PizzaCombo8;
JComboBox PizzaBaseCombo8;
JComboBox IceCombo8;

JComboBox DrinksCombo9;
JComboBox PizzaCombo9;
JComboBox PizzaBaseCombo9;
JComboBox IceCombo9;

JComboBox DrinksCombo10;
JComboBox PizzaCombo10;
JComboBox PizzaBaseCombo10;
JComboBox IceCombo10;


//JTextField
JTextField NameWidget;
JTextField NumbweOfPeopleWidget;
JTextField TimeSeatedWidget;
JTextField quantityTextField;


//Close of Widgets
public OrderingSystem() {
setSize(900, 600);
setTitle("Graphical User Interface");
setDefaultCloseOperation(EXIT_ON_CLOSE);

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);

priceLabel = new JLabel();
priceLabel.setBounds(220, 10, 100, 25);
priceLabel.setText("0.00");

quantityTextField = new JTextField();
quantityTextField.setBounds(330, 10, 100, 35);
quantityTextField.setText("0.00");
quantityTextField.addFocusListener(new FocusListener() {

public void focusGained(FocusEvent event) {
}

public void focusLost(FocusEvent event) {
recalculate();
}
});
subtotalLabel = new JLabel();
subtotalLabel.setBounds(440, 10, 100, 35);
subtotalLabel.setText("0.00");

totalLabel = new JLabel();
totalLabel.setBounds(440, 500, 100, 35);
totalLabel.setText("0.00");

//Order 1

numberLabel1 = new JLabel();
numberLabel1.setBounds(10, 65, 200, 20);
numberLabel1.setText("Order 1");

//Pizza Shack Label
PizzaShackWidget = new JLabel();
PizzaShackWidget.setBounds(10, 10, 200, 20);
PizzaShackWidget.setText("Pizza Shack");


//Table Selector
String[] TableStrings = {"Please Select Table Number", "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"};

//table number
JComboBox TableList = new JComboBox(TableStrings);
TableList.setBounds(200, 10, 200, 20);
TableList.setSelectedIndex(0);
TableList.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent event) {
}
});



//Pizza Combo box
String[] Pizza = {"Please select a Pizza", "Cheese and tomato", "Ham and Pineapple", "Vegetarian", "Meat Feast", "Seafood"};
PizzaCombo1 = new JComboBox(Pizza);
PizzaCombo1.setBounds(75, 60, 150, 30);
PizzaCombo1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent event) {
if (PizzaCombo1.getSelectedItem().equals("Cheese and tomato")) {
priceLabel.setText("3.50");
} else if (PizzaCombo1.getSelectedItem().equals("Ham and Pineapple")) {
priceLabel.setText("4.20");
} else if (PizzaCombo1.getSelectedItem().equals("Vegetarian" )) {
priceLabel.setText("5.20");
} else if (PizzaCombo1.getSelectedItem().equals("Meat Feast")) {
priceLabel.setText("5.80");
} else if (PizzaCombo1.getSelectedItem().equals("Seafood")) {
priceLabel.setText("5.60");
}
recalculate();
}
});
//Base Combo
String[] Base = {"Please select a Pizza Base", "Thin & Crispy", "Traditional", "Stuffed Crust", "Deep Pan", "Cheesey Bites"};
PizzaBaseCombo1 = new JComboBox(Base);
PizzaBaseCombo1.setBounds(235, 60, 195, 30);
PizzaBaseCombo1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent event) {
if (PizzaCombo1.getSelectedItem().equals("Thin & Crispy")) {
priceLabel.setText("0.0");
} else if (PizzaCombo1.getSelectedItem().equals("Traditional ")) {
priceLabel.setText("0.0");
} else if (PizzaCombo1.getSelectedItem().equals("Stuffed Crust")) {
priceLabel.setText("0.0");
} else if (PizzaCombo1.getSelectedItem().equals("Deep Pan")) {
priceLabel.setText("0.0");
} else if (PizzaCombo1.getSelectedItem().equals("Cheesey Bites")) {
priceLabel.setText("0.0");
}
recalculate();
}
});



//Drinks Combo Box
String[] drinks = {"Please select a drink", "Cola", "Lemonade", "Fanta"};
DrinksCombo1 = new JComboBox(drinks);
DrinksCombo1.setBounds(445, 60, 150, 30);
DrinksCombo1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent event) {
if (DrinksCombo1.getSelectedItem().equals("Cola")) {
priceLabel.setText("0.90");
} else if (DrinksCombo1.getSelectedItem().equals("Lemonade") ) {
priceLabel.setText("0.95");
} else if (DrinksCombo1.getSelectedItem().equals("Fanta")) {
priceLabel.setText("0.95");
}
recalculate();
}
});
//Ice or not
String[] ice = {"Ice", "With Ice", "Without Ice"};
IceCombo1 = new JComboBox(ice);
IceCombo1.setBounds(600, 60, 80, 30);
IceCombo1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent event) {
if (IceCombo1.getSelectedItem().equals("With Ice")) {
priceLabel.setText("0.0");
} else if (IceCombo1.getSelectedItem().equals("WithoutIce")) {
priceLabel.setText("0.0");
}
recalculate();
}
});




//To Add items to the panel

panel.add(PizzaShackWidget);
panel.add(subtotalLabel);
panel.add(totalLabel);
panel.add(TableList);

panel.add(DrinksCombo1);
panel.add(PizzaCombo1);
panel.add(numberLabel1);
panel.add(PizzaBaseCombo1);
panel.add(IceCombo1);

panel.add(DrinksCombo2);
panel.add(PizzaCombo2);
panel.add(numberLabel2);
panel.add(PizzaBaseCombo2);
panel.add(IceCombo2);

panel.add(DrinksCombo3);
panel.add(PizzaCombo3);
panel.add(numberLabel3);
panel.add(PizzaBaseCombo3);
panel.add(IceCombo3);

panel.add(DrinksCombo4);
panel.add(PizzaCombo4);
panel.add(numberLabel4);
panel.add(PizzaBaseCombo4);
panel.add(IceCombo4);

panel.add(DrinksCombo5);
panel.add(PizzaCombo5);
panel.add(numberLabel5);
panel.add(PizzaBaseCombo5);
panel.add(IceCombo5);

panel.add(DrinksCombo6);
panel.add(PizzaCombo6);
panel.add(numberLabel6);
panel.add(PizzaBaseCombo6);
panel.add(IceCombo6);

panel.add(DrinksCombo6);
panel.add(PizzaCombo6);
panel.add(numberLabel6);
panel.add(PizzaBaseCombo6);
panel.add(IceCombo6);

panel.add(DrinksCombo7);
panel.add(PizzaCombo7);
panel.add(numberLabel7);
panel.add(PizzaBaseCombo7);
panel.add(IceCombo7);

panel.add(DrinksCombo8);
panel.add(PizzaCombo8);
panel.add(numberLabel8);
panel.add(PizzaBaseCombo8);
panel.add(IceCombo8);

panel.add(DrinksCombo9);
panel.add(PizzaCombo9);
panel.add(numberLabel9);
panel.add(PizzaBaseCombo9);
panel.add(IceCombo9);

panel.add(DrinksCombo10);
panel.add(PizzaCombo10);
panel.add(numberLabel10);
panel.add(PizzaBaseCombo10);
panel.add(IceCombo10);


}

private void recalculate() {

// recalculate subTotal
double price;
// int quantity;
double subtotal;
double total;
String subtotalOutput;
String totalOutput;
NumberFormat subtotalFormat;
NumberFormat totalFormat;

price = Double.parseDouble(priceLabel.getText());
int quantity = Integer.parseInt(quantityTextField.getText());
subtotal = price * (double)quantity / 100.0;
// see Java in Easy Steps page 129 - formating numbers
// subtotalFormat = NumberFormat.getNumberInstance();
// subtotalOutput = String.valueOf(subtotal) + subtotalFormat.format(0.00);
//subtotalLabel.setText(subtotalOutput);
// now recalculate total
total = Double.parseDouble(subtotalLabel.getText());
// NOTE: if you have many subtotal labels get the
// text from each one using:
// total = Double.parseDouble(subtotalLabelOne.getText())
// + Double.parseDouble(subtotalLabelTwo.getText());
totalFormat = NumberFormat.getCurrencyInstance(Locale.UK);
totalOutput = totalFormat.format(total);
totalLabel.setText(totalOutput);

}

/**
*
* @param args
*/
public static void main(String[] args) {
// TODO code application logic here
OrderingSystem gui = new OrderingSystem();
gui.setVisible(true);

}
}




May 17 '09 #1
1 6518
JosAH
11,448 Expert 8TB
@onlinegear
Look at line 261 of your class OrderingSystem; it is somewhere in the constructor of that class. You pass something (as a parameter) that happens to be equal to null; the calling method called the constructor at line 361.

Always read those stack traces, they tell you where the error is.

kind regards,

Jos
May 17 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by news | last post: by
oll3i
4 posts views Thread by oll3i | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.