每次尝试使用其他类运行代码时,我都会收到NullPointerException
。
有些人可以在不运行的情况下以某种方式阅读它吗?因为你需要拍照。我只需要知道我做错了什么,我可能做了,因为它给了我这个例外。
我真的需要帮助我不知道该怎么做,我尝试了很多东西。
java.lang.NullPointerException
at BurgerMakerPanel$TopBreadListener.updateLabel(BurgerMakerPanel.java:174)
at BurgerMakerPanel.<init>(BurgerMakerPanel.java:40)
at BurgerMaker.main(BurgerMaker.java:10)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:271)
import javax.swing.JFrame;
public class BurgerMaker {
public static void main(String[] args) {
JFrame frame = new JFrame ("Burger Maker");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new BurgerMakerPanel());
frame.pack();
frame.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class BurgerMakerPanel extends JPanel{
private final int WIDTH = 600, HEIGHT = 1200;
private JLabel inputLabel, inputLabel2, inputLabel3, inputLabel4, inputLabel5, inputLabel6, inputLabel7, titleLabel, titleLabel2, topBreadPicture, bottomBreadPicture,
pattyPicture, cheesePicture, veggiePicture, saucePicture;
private JComboBox breadList, breadList2, pattyList, vegetableList, sauceList, cheeseList;
private JPanel primary, picturePanel, test, test2, test3, test4, titlePanel, spacePanel, topBreadPanel, bottomBreadPanel, pattyPanel, veggiePanel, saucePanel, cheesePanel;
private JButton push;
public BurgerMakerPanel() {
String[] breadStrings = {"White Bread Top", "Rye Bread Top", "Hamburger Bread Top", "Wheat Bread Top"};
String[] pattyStrings = {"Beef", "Chicken", "Pork"};
String[] veggieStrings = {"Lettuce", "Tomato", "Pickle", "Onion"};
String[] bread2Strings = {"White Bread Bottom", "Rye Bread Bottom", "Hamburger Bread Bottom", "Wheat Bread Bottom"};
String[] cheeseStrings = {"American", "Swiss Cheese", "Cheddar Cheese", "Pepper Jack Cheese"};
String[] sauceStrings = {"Mayonnaise", "Mustard", "Ketchup", "BBQ"};
push = new JButton("Create my Sandwich/Burger!");
TopBreadListener tbl = new TopBreadListener();
BottomBreadListener bbl = new BottomBreadListener();
PattyListener pl = new PattyListener();
CheeseListener cl = new CheeseListener();
SauceListener sl = new SauceListener();
VeggieListener vl = new VeggieListener();
JLabel separatorLabel = new JLabel(" ________________________________________________________________ " );
separatorLabel.setFont(new Font("Courier New", Font.BOLD, 14));
JLabel separatorLabel2 = new JLabel(" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - " );
separatorLabel2.setFont(new Font("Courier New", Font.BOLD, 14));
inputLabel = new JLabel("Select your type of bottom bread:", SwingConstants.CENTER);
inputLabel.setFont(new Font("Verdana", Font.PLAIN, 14));
breadList = new JComboBox(breadStrings);
breadList.setSelectedIndex(3);
breadList.addActionListener(new TopBreadListener());
tbl.updateLabel(breadStrings[breadList.getSelectedIndex()]);
inputLabel2 = new JLabel("Select your type of patty:", SwingConstants.CENTER);
inputLabel2.setFont(new Font("Verdana", Font.PLAIN, 14));
pattyList = new JComboBox(pattyStrings);
pattyList.setSelectedIndex(2);
pattyList.addActionListener(new PattyListener());
pl.updateLabel(pattyStrings[pattyList.getSelectedIndex()]);
inputLabel3 = new JLabel("Select your type of sauce:", SwingConstants.CENTER);
inputLabel3.setFont(new Font("Verdana", Font.PLAIN, 14));
sauceList = new JComboBox(sauceStrings);
sauceList.setSelectedIndex(3);
sauceList.addActionListener(new SauceListener());
sl.updateLabel(sauceStrings[sauceList.getSelectedIndex()]);
inputLabel4 = new JLabel("Select your type of cheese:", SwingConstants.CENTER);
inputLabel4.setFont(new Font("Verdana", Font.PLAIN, 14));
cheeseList = new JComboBox(cheeseStrings);
cheeseList.setSelectedIndex(3);
cheeseList.addActionListener(new CheeseListener());
cl.updateLabel(cheeseStrings[cheeseList.getSelectedIndex()]);
inputLabel5 = new JLabel("Select your type of vegetable:", SwingConstants.CENTER);
inputLabel5.setFont(new Font("Verdana", Font.PLAIN, 14));
vegetableList = new JComboBox(veggieStrings);
vegetableList.setSelectedIndex(3);
vegetableList.addActionListener(new VeggieListener());
vl.updateLabel(veggieStrings[vegetableList.getSelectedIndex()]);
inputLabel6 = new JLabel("Select your type of top bread:", SwingConstants.CENTER);
inputLabel6.setFont(new Font("Verdana", Font.PLAIN, 14));
breadList2 = new JComboBox(bread2Strings);
breadList2.setSelectedIndex(4);
breadList2.addActionListener(new BottomBreadListener());
bbl.updateLabel(bread2Strings[breadList2.getSelectedIndex()]);
inputLabel7 = new JLabel("Press this only when you are done making your sandwich/burger!", SwingConstants.CENTER);
inputLabel7.setFont(new Font("Verdana", Font.BOLD, 14));
titleLabel = new JLabel("This is a program that will help you make your own sandwich or burger!", SwingConstants.CENTER);
titleLabel2 = new JLabel("Follow all directions and have fun! Thank you!", SwingConstants.CENTER);
titleLabel.setFont(new Font("Verdana", Font.BOLD, 15));
titleLabel2.setFont(new Font("Verdana", Font.BOLD, 15));
primary = new JPanel();
// Set up first subpanel
picturePanel = new JPanel();
spacePanel = new JPanel();
titlePanel = new JPanel();
topBreadPanel = new JPanel();
saucePanel = new JPanel();
pattyPanel = new JPanel();
cheesePanel = new JPanel();
veggiePanel = new JPanel();
bottomBreadPanel = new JPanel();
topBreadPanel.setPreferredSize(new Dimension(600, 70));
saucePanel.setPreferredSize(new Dimension(600, 7));
pattyPanel.setPreferredSize(new Dimension(600, 127));
cheesePanel.setPreferredSize(new Dimension(600, 9));
veggiePanel.setPreferredSize(new Dimension(600, 58));
bottomBreadPanel.setPreferredSize(new Dimension(600, 70));
test = new JPanel();
test2 = new JPanel();
test3 = new JPanel();
test4 = new JPanel();
titlePanel.setPreferredSize(new Dimension(600, 60));
spacePanel.setPreferredSize(new Dimension(600, 5));
test.setPreferredSize(new Dimension(500, 80));
test2.setPreferredSize(new Dimension(450, 80));
test3.setPreferredSize(new Dimension(450, 80));
test4.setPreferredSize(new Dimension(600, 60));
picturePanel.setPreferredSize(new Dimension(600, 600));
JLabel label1 = new JLabel ("Here is your finished product!");
picturePanel.add(label1);
picturePanel.add(topBreadPanel);
picturePanel.add(saucePanel);
picturePanel.add(pattyPanel);
picturePanel.add(cheesePanel);
picturePanel.add(veggiePanel);
picturePanel.add(bottomBreadPanel);
titlePanel.add(titleLabel);
titlePanel.add(titleLabel2);
test.add(inputLabel);
test.add(breadList);
test.add(inputLabel6);
test.add(breadList2);
test2.add(inputLabel2);
test2.add(pattyList);
test2.add(inputLabel4);
test2.add(cheeseList);
test3.add(inputLabel5);
test3.add(vegetableList);
test3.add(inputLabel3);
test3.add(sauceList);
test4.add(inputLabel7);
test4.add(push);
primary.add(spacePanel);
primary.add(titlePanel);
primary.add(separatorLabel2);
primary.add(test);
primary.add(test2);
primary.add(test3);
primary.add(test4);
primary.add(separatorLabel);
primary.add(spacePanel);
primary.add(picturePanel);
primary.setPreferredSize(new Dimension(WIDTH, HEIGHT));
}
public JPanel getPanel(){
return primary;
}
private class TopBreadListener implements ActionListener{
public void actionPerformed(ActionEvent e){
JComboBox ceb1 = (JComboBox)e.getSource();
String ingredientName1 = (String)ceb1.getSelectedItem();
updateLabel(ingredientName1);
}
public void updateLabel(String name) {
ImageIcon icon1 = new ImageIcon(name + ".png");
topBreadPicture.setIcon(icon1);
}
}
private class BottomBreadListener implements ActionListener{
public void actionPerformed(ActionEvent e){
JComboBox ceb2 = (JComboBox)e.getSource();
String ingredientName2 = (String)ceb2.getSelectedItem();
updateLabel(ingredientName2);
}
public void updateLabel(String name) {
ImageIcon icon2 = new ImageIcon(name + ".png");
bottomBreadPicture.setIcon(icon2);
}
}
private class PattyListener implements ActionListener{
public void actionPerformed(ActionEvent e){
JComboBox ceb3 = (JComboBox)e.getSource();
String ingredientName3 = (String)ceb3.getSelectedItem();
updateLabel(ingredientName3);
}
public void updateLabel(String name) {
ImageIcon icon3 = new ImageIcon(name + ".png");
pattyPicture.setIcon(icon3);
}
}
private class CheeseListener implements ActionListener{
public void actionPerformed(ActionEvent e){
JComboBox ceb4 = (JComboBox)e.getSource();
String ingredientName4 = (String)ceb4.getSelectedItem();
updateLabel(ingredientName4);
}
public void updateLabel(String name) {
ImageIcon icon4 = new ImageIcon(name + ".png");
cheesePicture.setIcon(icon4);
}
}
private class VeggieListener implements ActionListener{
public void actionPerformed(ActionEvent e){
JComboBox ceb5 = (JComboBox)e.getSource();
String ingredientName5 = (String)ceb5.getSelectedItem();
updateLabel(ingredientName5);
}
public void updateLabel(String name) {
ImageIcon icon5 = new ImageIcon(name + ".png");
veggiePicture.setIcon(icon5);
}
}
private class SauceListener implements ActionListener{
public void actionPerformed(ActionEvent e){
JComboBox ceb6 = (JComboBox)e.getSource();
String ingredientName6 = (String)ceb6.getSelectedItem();
updateLabel(ingredientName6);
}
public void updateLabel(String name) {
ImageIcon icon6 = new ImageIcon(name + ".png");
saucePicture.setIcon(icon6);
}
}
}
答案 0 :(得分:3)
topBreadPicture
为null,未初始化。
topBreadPicture.setIcon(icon1);
答案 1 :(得分:1)
声明veggiePicture
等标签但从未实例化。
对于图像,最好将它们声明为类属性,并在添加标签(正在创建和添加)的同时加载它们。进一步在图像加载时,使用ImageIO.read(File/URL)
而不是使用ImageIcon
加载它。如果找不到图像,前者将抛出异常,而后者(AFAIR)将仅返回null
图像。
答案 2 :(得分:1)
JComboBox ceb1 = (JComboBox)e.getSource();
String ingredientName1 = (String)ceb1.getSelectedItem();
updateLabel(ingredientName1);
在调用getSelectedItem()
之前,ceb1有可能为null答案 3 :(得分:1)
ImageIcon icon5 = new ImageIcon(name +“。png”); veggiePicture.setIcon(icon5);
veggiePicture永远不会被初始化。
答案 4 :(得分:-1)
tbl.updateLabel(breadStrings [breadList.getSelectedIndex()]);
如果没有选择任何内容,breadList.getSelectedIndex()可以为-1
所以breadStrings [-1]会返回null,因此尝试传递空参数
可能是错的..
在传递之前先检查你的参数是什么,这仍然是一种很好的做法。