我正在使用一个GUI,使您可以订购饮料并计算总价格。但是
我在第163行有这个(线程“ AWT-EventQueue-0”中的异常java.lang.NullPointerException)错误。我尝试过多次尝试更改第163行的参数“ total = price.juice(“ Apple”,1) ,“小”);”但没有任何效果!
我的代码:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class Assignments1 extends JFrame implements ActionListener{
private JLabel lblFirst, lblSecond, lblThird, lblInfo;
private JTextField txtOne;
private JButton btnAdd, btnOrder;
private JRadioButton rdJuice, rdWater, rdTea, rdCoffee;
private ButtonGroup btnGroup;
private JComboBox comboBox;
private Prices price;
private OptionPane optionPane;
public Assignments1() {
//set Layout
setLayout(null);
gui(); //calling the gui method to show the program.
btnAdd.addActionListener(this);
comboBox.addActionListener(this);
//set the GUI parameters
setSize(500, 500);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
}
//design GUI
private void gui() {
//initialize global variables
lblFirst= new JLabel("Select size:");
lblFirst.setLocation(100, 10);
lblFirst.setSize(150,50);
add(lblFirst);
//creating the JComboBox:
String bevSize[] = {"Small", "Medium", "Large"}; //array of string contating cities
comboBox = new JComboBox(bevSize); //create checkbox
comboBox.setSelectedItem("Small");
comboBox.setLocation(110, 60);
comboBox.setSize(150, 30); // set the size of frame
add(comboBox);
//comboBox.show();
//////////////////////////////////////////////////////////////////////////////////////////
lblSecond= new JLabel("Select which type of beverage you want to order:");
lblSecond.setLocation(100, 120);
lblSecond.setSize(300,50);
add(lblSecond);
// set radio buttons
btnGroup= new ButtonGroup();
rdJuice= new JRadioButton("Juice");
rdJuice.setActionCommand("Juice");
rdJuice.setLocation(110, 170);
rdJuice.setSize(70, 50);
add(rdJuice);
rdWater= new JRadioButton("Water");
rdWater.setActionCommand("Water");
rdWater.setLocation(190, 170);
rdWater.setSize(70, 50);
add(rdWater);
rdTea= new JRadioButton("Tea");
rdTea.setActionCommand("Tea");
rdTea.setLocation(280, 170);
rdTea.setSize(70, 50);
add(rdTea);
rdCoffee= new JRadioButton("Coffee");
rdCoffee.setActionCommand("Coffee");
rdCoffee.setLocation(350, 170);
rdCoffee.setSize(70, 50);
add(rdCoffee);
//make a group for radio buttons
btnGroup.add(rdJuice);
btnGroup.add(rdWater);
btnGroup.add(rdTea);
btnGroup.add(rdCoffee);
rdJuice.setSelected(true);
/////////////////////////////////////////////////////////////////////
lblThird= new JLabel("Select how many glasses you want to order:");
lblThird.setLocation(100, 220);
lblThird.setSize(300,50);
add(lblThird);
// set txtTwo
txtOne= new JTextField();
txtOne.setLocation(100, 270);
txtOne.setSize(300, 30);
add(txtOne);
///////////////////////////////////////////////////////////////////////
// set Buttons
btnAdd= new JButton("Add");
btnAdd.setLocation(100, 310);
btnAdd.setSize(100, 50);
add(btnAdd);
btnOrder= new JButton("Order");
btnOrder.setLocation(300, 310);
btnOrder.setSize(100, 50);
add(btnOrder);
////////////////////////////////////////////////////////////////////////
// register buttons to respond actions
btnAdd.addActionListener(this);
btnOrder.addActionListener(this);
/////////////////////////////////////////////////////////////////////////
lblInfo= new JLabel();
lblInfo.setForeground(Color.BLACK);
lblInfo.setLocation(10,500);
lblInfo.setSize(200, 50);
add(lblInfo);
}
public void price(ActionEvent e) {
int total = 0;
int first = Integer.parseInt(txtOne.getText().trim());
//rdJuice, rdWater, rdTea, rdCoffee
String[] options = {"Apple", "Orange", "Pineapple"};
JComboBox <String> optionList = new JComboBox<>(options);
optionList.setSelectedIndex(0);
JOptionPane.showMessageDialog(this, optionList, "Select a fruit",
JOptionPane.OK_CANCEL_OPTION);
if(rdJuice.isSelected()) {
total = price.juice("Apple", 1, "Small");
}
else if(rdWater.isSelected()) {
total = price.water(optionPane.paneWater(), first, comboBox.getSelectedItem());
}
else if(rdTea.isSelected()) {
total = price.tea(optionPane.paneTea(), first, comboBox.getSelectedItem());
}
else if(rdCoffee.isSelected()) {
total = price.coffee(optionPane.paneTea(), first, comboBox.getSelectedItem());
}
/*
catch(NumberFormatException ee) {
JOptionPane.showMessageDialog(
this,"Input Ineger number","Unvalid NUmber",
JOptionPane.ERROR_MESSAGE
);
}*/
}
public static void main(String[]args) {
new Assignments1();
}
@Override
public void actionPerformed(ActionEvent arg0) {
if(arg0.getSource().equals(btnAdd))
price(arg0);
}
}
public class Prices {
public int juice(String kind, int amount, String size) {
int price = 0;
if(kind.equals("Apple") && size.equals("Small")) {
price = 5 * amount;
}
else if(kind.equals("Apple") && size.equals("Medium")) {
price = 10 * amount;
}
else if(kind.equals("Apple") && size.equals("Large")) {
price = 15 * amount;
}
if(kind.equals("Orange") && size.equals("Small")) {
price = 6 * amount;
}
else if(kind.equals("Orange") && size.equals("Medium")) {
price = 11 * amount;
}
else if(kind.equals("Orange") && size.equals("Large")) {
price = 16 * amount;
}
if(kind.equals("Pineapple") && size.equals("Small")) {
price = 7 * amount;
}
else if(kind.equals("Pineapple") && size.equals("Medium")) {
price = 12 * amount;
}
else if(kind.equals("Pineapple") && size.equals("Large")) {
price = 17 * amount;
}
return price;
}
public int water(boolean kind, int amount, Object size) {
int price = 0;
if(kind == true && size.equals("Small")) {
price = 4 * amount;
}
else if(kind == true && size.equals("Medium")) {
price = 5 * amount;
}
else if(kind == true && size.equals("Large")) {
price = 6 * amount;
}
if(kind == false && size.equals("Small")) {
price = 2 * amount;
}
else if(kind == false && size.equals("Medium")) {
price = 3 * amount;
}
else if(kind == false && size.equals("Large")) {
price = 4 * amount;
}
return price;
}
public int tea(boolean kind, int amount, Object size) {
int price = 0;
if(kind == true && size.equals("Small")) {
price = 4 * amount;
}
else if(kind == true && size.equals("Medium")) {
price = 5 * amount;
}
else if(kind == true && size.equals("Large")) {
price = 6 * amount;
}
if(kind == false && size.equals("Small")) {
price = 2 * amount;
}
else if(kind == false && size.equals("Medium")) {
price = 3 * amount;
}
else if(kind == false && size.equals("Large")) {
price = 4 * amount;
}
return price;
}
public int coffee(boolean kind, int amount, Object size) {
int price = 0;
if(kind == true && size.equals("Small")) {
price = 4 * amount;
}
else if(kind == true && size.equals("Medium")) {
price = 5 * amount;
}
else if(kind == true && size.equals("Large")) {
price = 6 * amount;
}
if(kind == false && size.equals("Small")) {
price = 2 * amount;
}
else if(kind == false && size.equals("Medium")) {
price = 3 * amount;
}
else if(kind == false && size.equals("Large")) {
price = 4 * amount;
}
return price;
}
}
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Assignments1.price(Assignments1.java:163)
at Assignments1.actionPerformed(Assignments1.java:195)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
答案 0 :(得分:0)
private Prices price;
您的“价格”变量为空。
如果要访问该类中定义的方法,则需要为其创建一个实例:
private Prices price = new Prices();