- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个 GUI,它可以让你订购饮料并计算它们的总价。但是
我在第 163 行遇到此错误 (Exception in thread "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)
最佳答案
private Prices price;
您的“价格”变量为空。
如果您想访问类中定义的方法,则需要创建它的实例:
private Prices price = new Prices();
关于java - 如何解决 "Exception in thread "AWT-EventQueue-0"java.lang.NullPointerException"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58248908/
AWT-EventQueue 线程和 AWT-Shutdown 线程没有在我们的应用程序中关闭。有没有一种调试技术可以找出它们不存在的原因?有什么特别的东西要寻找吗? 最佳答案 如果你的意思是关闭所有
java.awt.* 和 java.awt.event.* 有什么区别? 最佳答案 这只是两个不同的包。 当您说import java.awt.*时,它仅导入那些完全属于java.awt包的类,而不是
我正在将 aadhar 集成到 liferay 中。我尝试了这个链接 https://developer.uidai.gov.in/site/book/export/html/18 所以我想将其集成到
我想知道如何确定 Java.awt.Rectangle 是否包含特定 Java.awt.Color 的像素。我一直在到处寻找,但找不到任何关于此的信息,甚至找不到任何可能的信息。 所以我想知道如何确定
我试图在组件和图像之间切换面板的内容,它适用于组件: imgpanel.removeAll(); Component comp; if ((comp = player.getVisualCompone
我在使用 JAVA 编码时遇到一些错误,我一直在尝试解决这个问题,也试图找到其他有同样问题的人并修复它,但没有任何效果... 嗯..这是代码 package ca.vanzeben.game;
我想我可以尝试一下 JAXB 来处理存储和恢复设置。但即使是“最简单”的例子我也遇到了麻烦: import java.awt.Point; public class Config { public
这个问题已经有答案了: Import package.* vs import package.SpecificType [duplicate] (10 个回答) 已关闭 7 年前。 在我现在正在进行的
private static byte[] get_byte_data(BufferedImage image) { //WritableRaster raster = image.get
是否有可能获得标准 AWT Cursor以位图图像的形式(例如 BufferedImage )或任何可在 Graphics2D 上绘制的图像?例如,文本光标 new Cursor(Cursor.TEX
我的代码中有三个点,我想填充它们之间的区域,或者换句话说,在 3 个点之间绘制并填充一个三角形。 我想过简单地用 for 循环绘制线条(从 x1 到 x2),但我认为这不会有效,是否有其他更有效的方法
我正在制作一个小脚本,我使用鼠标键来节省我的工作时间。我可以正确、良好地使用鼠标键。但是,当使用 java.awt.Robot 和 java.awt.event.KeyEvent 时,鼠标键基本上被忽
我正在尝试在 scala 中使用 java awt 来制作一个简单的桌面应用程序。我已经在它上面工作了几天,没有任何问题,直到我有 2 天没有碰它,当我回来时,我得到一个 java.lang.NoCl
我在 VisualVM 和线程 View 中监视一个 JavaFX 程序,不断有 AWT-EventQueue-0 和 AWT-Shutdown 线程被创建和销毁。这是正常行为吗?这是什么原因? 最佳
我需要将 java.awt.geom.Area 或 java.awt.Shape 转换为 java.awt.Polygon。我所知道的是:isSingular = true、isPolygonal =
我正在重新使用 Java 并审查我的一些旧代码,并且我看到了很多我已经完成的地方 import javax.swing.*; import java.awt.*; 或者实际上从 swing/awt 包
晚上, 我在玩一个小的 swing 应用程序,我添加了一个按钮来响应按下。因此我需要实现 ActionListener。我已经添加了这一行: import java.awt.*; 但它告诉我找不到“A
我有这个 java 代码: Editor() { javax.swing.SwingUtilities.invokeLater(new Runnable() { pub
请帮我解决这个问题 sun.awt.image.ToolkitImage 无法转换为 java.awt.image.BufferedImage if (shape.hasImage())
嗨 Stackoverflow 的 friend 们 我最近将 Jenkins 服务器配置到 Apache Tomcat 7.0.42我制作的程序是将 jenkins.war 文件部署到 tomcat
我是一名优秀的程序员,十分优秀!