Click to See Complete Forum and Search --> : Trouble with arrays


fowlergod09
04-20-2007, 09:09 PM
I am having trouble with my program, I am trying to add the price of the items selected from a combo box which are stored in an array.When i click the sum total button the for loop i have used is supposed to add the selections together and display the total in text field list2.
When i compile my program it is fine and runs but when i have made selections and then press the sumtotal button. I get the following output :
--------------------Configuration: MySuperMktPro - JDK version 1.6.0_01 <Default> - <Default>--------------------
Starting My Supermarket Project...
Action performed from: Sum Total
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at CheckoutView.actionPerformed(MySuperMktPro.java:213)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6038)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)

Would someone be so kind to look at my code and point me in the right direction please.
Thank you


[/CODE]
class CheckoutView implements ActionListener {

private OfficeView off1;
private JFrame chk1;
private Container cont1;
private Dimension screensize,tempDim;
private JPanel pan1,pan2,pan3,pan4,pan5;
private JButton buyBut,prodCode;
private JButton purchase,cashBack,manual,remove,disCo;
private JButton but1,but2,finaLi1,but4,but5,but6;
private JTextArea tArea1,tArea2;
private JTextField tf1,tf2,tf3,list2;
private JComboBox prodList;
private JLabel lab1,lab2,lab3,lab4,lab5,lab6,lab7;
private int checkoutID; //New private int for all methods of this class
private int passedX,passedY,counter;
private String prods[];
private JButton rb1,rb2,rb3,rb4;
private double price[],total; // not used
private int code[];

public CheckoutView(int passedInteger, int passedX, int passedY) {
checkoutID = passedInteger; //Store the int passed into the method
this.passedX = passedX;
this.passedY = passedY;
drawCheckoutGui();
}

public void drawCheckoutGui() {
prods= new String[]{"Products","Beans", "Eggs", "bread", "Jam", "Butter",
"Cream", "Sugar", "Peas", "Milk", "Bacon", "Spaghetti",
"Corn Flakes", "Carrots", "Oranges", "Bananas", "Snickers",
"Wine", "Beer", "Lager", "Cheese"};

code = new int [21];
for(int b = 0; b < code.length; b++) {
code[b] = b+1;
}

price = new double[]{0.0,0.65, 0.84, 0.98, 0.75, 0.45, 0.65,
1.78, 1.14, 0.98, 0.99, 0.98, 0.65, 1.69, 2.99,
0.99, 2.68, 0.89, 5.99, .54, 2.99};
for(int i=0; i<price.length; i++)
total=total+price[i];

screensize = Toolkit.getDefaultToolkit().getScreenSize();
chk1 = new JFrame();
chk1.setTitle("Checkout #" + checkoutID); //Use the new stored int
chk1.setSize(300,350);
chk1.setLocation(passedX,passedY);
chk1.setLayout(new GridLayout(5,1));
//chk1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cont1 = chk1.getContentPane();
//chk1.setLayout(new BorderLayout());

pan1 = new JPanel();
pan2 = new JPanel();
pan3 = new JPanel();
pan4 = new JPanel();
pan5 = new JPanel();

//panel 1
pan1.setLayout(new FlowLayout());
pan1.setBackground(Color.black);
prodList = new JComboBox(prods);
prodList.setMaximumRowCount(2);
prodList.addActionListener(this);
but1 = new JButton("Buy");
but1.addActionListener(this);
lab1 = new JLabel("Products");
lab1.setForeground(Color.white);
lab1.setBorder(BorderFactory.createLineBorder(Color.white));
pan1.add(lab1);
pan1.add(prodList);
pan1.add(but1);

//panel 2
pan2 = new JPanel();
pan2.setLayout(new BorderLayout());
pan2.setBackground(Color.black);
lab3 = new JLabel(" Enter Product Code");
lab3.setForeground(Color.white);
tArea2 = new JTextArea(8,10);
tArea2.setBorder(BorderFactory.createLineBorder(Color.white));
lab5 = new JLabel(" Tesco's ");
lab5.setForeground(Color.white);
lab5.setBorder(BorderFactory.createLineBorder(Color.white));
lab6 = new JLabel("Checkout");
lab6.setForeground(Color.white);
lab6.setBorder(BorderFactory.createLineBorder(Color.white));
lab7 = new JLabel("Your preferred payment method is ");
lab7.setForeground(Color.cyan);
//lab7.setBorder(BorderFactory.createLineBorder(Color.white));
pan2.add(lab7,"North");
pan2.add(lab6,"East");
pan2.add(tArea2,"Center");
pan2.add(lab5,"West");
pan2.add(lab3,"South");

// panel 3
pan3 = new JPanel();
pan3.setLayout(new FlowLayout());
pan3.setBackground(Color.black);
manual = new JButton("Manual");
manual.addActionListener(this);
manual.setBorder(BorderFactory.createLineBorder(Color.white));
remove = new JButton("Remove");
remove.addActionListener(this);
remove.setBorder(BorderFactory.createLineBorder(Color.white));
tf1 = new JTextField(5);
pan3.add(manual);
pan3.add(tf1);
pan3.add(remove);

// panel 4
pan4 = new JPanel();
pan4.setLayout(new FlowLayout());
pan4.setBackground(Color.black);
finaLi1 = new JButton("Finalise");
// finaLi1.addActionListener(this);
cashBack = new JButton("Cashback");
JTextField list2 = new JTextField(5);
but4 = new JButton(" Sum Total");
but4.addActionListener(this);
but4.setBorder(BorderFactory.createLineBorder(Color.white));
cashBack.setBorder(BorderFactory.createLineBorder(Color.white));
pan4.add(cashBack);
pan4.add(list2);
pan4.add(but4);

// panel 5
pan5 = new JPanel();
pan5.setLayout(new GridLayout(2,3));
pan5.setBackground(Color.black);
disCo = new JButton("Discount");
disCo.addActionListener(this);
disCo.setBorder(BorderFactory.createLineBorder(Color.black));
but6 = new JButton("Finalise");
but6.addActionListener(this);
but6.setBorder(BorderFactory.createLineBorder(Color.black));
rb1 = new JButton("Cash");
rb1.addActionListener(this);
rb1.setBorder(BorderFactory.createLineBorder(Color.black));
rb2 = new JButton("Card");
rb2.addActionListener(this);
rb2.setBorder(BorderFactory.createLineBorder(Color.black));
rb3 = new JButton("Cheque");
rb3.addActionListener(this);
rb3.setBorder(BorderFactory.createLineBorder(Color.black));
rb4 = new JButton("Voucher");
rb4.addActionListener(this);
rb4.setBorder(BorderFactory.createLineBorder(Color.black));
pan5.add(disCo);
pan5.add(but6);
pan5.add(rb1);
pan5.add(rb2);
pan5.add(rb3);
pan5.add(rb4);
//add the panels to the container
cont1.add(pan1);
cont1.add(pan4);
cont1.add(pan2);
cont1.add(pan3);
cont1.add(pan5);
chk1.setContentPane(cont1);
chk1.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
int index = prodList.getSelectedIndex();
if(counter==0)
{
setTextMethod(index);
counter =1;
}
if(counter==1)
{
Object source = e.getActionCommand();
System.out.println("Action performed from: " + source);
if (source.toString() == "Buy") {
setTextMethod(index);
}
if(e.getSource()==but4)
{
list2.setText("£" + total);
}
}
}
public void setTextMethod(int index)
{
tArea2.setText(prods[index]
+ " ; Price is £" + price[index]);
tf1.setText(""+code[index]);
}
}

agent_x91
04-21-2007, 05:59 PM
I don't have time to sift through your code, but hopefully this will help you solve the problem yourself.

A NullPointerException is thrown when you try to call a method of an object which is set to null. To avoid this, you should always check whether the object is null before trying to call any of its methods.

The top of the stack trace indicates that the problem in your code where you've tried to do this lies on line 213:


at CheckoutView.actionPerformed(MySuperMktPro.java:213)


Look at that line, look at which object had a method called on it, and wrap the line in an if statement to check whether it's null before trying to call its methods, or if more appropriate, exit that method using return if the object is null.