晚上好。我一直绞尽脑汁试图自己回答这个问题,但我不知所措,并考虑扔东西。让我先说一下我的问题,这是一个学校项目,导师让我重新提交,试图通过成绩,我不是在寻找直接复制和过去的代码,但我需要帮助弄清楚如何解决这个问题呢。原始代码打开applet,我得到开始:applet未初始化错误。在Eclipse中,我得到了 java.lang.NullPointerException ,有几行我不知道它们来自哪里。请参阅代码和错误列表。提前谢谢!
Error List
java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1091)
at java.awt.Container.add(Container.java:971)
at SodaMachine.init(SodaMachine.java:43)
at sun.applet.AppletPanel.run(AppletPanel.java:434)
at java.lang.Thread.run(Thread.java:745)
import java.applet.Applet;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
@SuppressWarnings("serial")
public class SodaMachine extends Applet
{
private TextField feedbackText;
private TextField changeText;
private TextField creditText;
private Panel userMoneyPanel; //To hold Panel that shows current amount of money in machine, money due to user at end of transaction
private Panel drinksPanel; //To hold Panel that shows available drink selections
private Panel depositPanel; //To hold Panel that shows what money can be entered by user
private Panel userFeedbackPanel;
int colaStock = 3;
int lemonLimeStock = 3;
int grapeStock = 3;
int rootBeerStock = 3;
int waterStock = 3;
double cost = 0.75;
double credit = 0.00;
double change = 0.00;
public void init()
{
//Panels to be built
builduserMoneyPanel();
builddrinksPanel();
builddepositPanel();
builduserFeedbackPanel();
//layout manager for applet
setLayout(new BorderLayout());
//add Panels to applet
add(depositPanel, BorderLayout.NORTH);
add(userFeedbackPanel, BorderLayout.SOUTH);
add(drinksPanel, BorderLayout.EAST);
add(userMoneyPanel, BorderLayout.WEST);
}
//user Money Panel build method to show money in machine and user's change
private void builduserMoneyPanel()
{
Panel userMoney = new Panel();
Label creditLabel = new Label("Credit");
Label changeLabel = new Label("Change");
TextField creditText = new TextField(10);
creditText.setEditable(false);
TextField changeText = new TextField(10);
changeText.setEditable(false);
userMoney.setLayout(new GridLayout(2, 2));
userMoney.add(creditLabel);
userMoney.add(changeLabel);
userMoney.add(creditText);
userMoney.add(changeText);
}
//deposit method for user to add money to machine to make a transaction
private void builddepositPanel()
{
Panel depositPanel = new Panel();
Button nickelButton = new Button("INSERT NICKEL");
Button dimeButton = new Button("INSERT DIME");
Button quarterButton = new Button("INSERT QUARTER");
Button dollarButton = new Button("INSERT DOLLAR");
nickelButton.addActionListener(new nickelButtonListener());
dimeButton.addActionListener(new dimeButtonListener());
quarterButton.addActionListener(new quarterButtonListener());
dollarButton.addActionListener(new dollarButtonListener());
depositPanel.setLayout(new GridLayout(1, 4));
depositPanel.add(nickelButton);
depositPanel.add(dimeButton);
depositPanel.add(quarterButton);
depositPanel.add(dollarButton);
}
//method to show user available drink selection to choose from
private void builddrinksPanel()
{
drinksPanel = new Panel();
Button colaButton = new Button("COLA");
Button lemonLimeButton = new Button("LEMON LIME");
Button grapeButton = new Button("GRAPE");
Button rootBeerButton = new Button("ROOT BEER");
Button waterButton = new Button("WATER");
colaButton.addActionListener(new colaButtonListener());
lemonLimeButton.addActionListener(new lemonLimeButtonListener());
grapeButton.addActionListener(new grapeButtonListener());
rootBeerButton.addActionListener(new rootBeerButtonListener());
waterButton.addActionListener(new waterButtonListener());
drinksPanel.setLayout(new GridLayout(5, 1));
drinksPanel.add(colaButton);
drinksPanel.add(lemonLimeButton);
drinksPanel.add(grapeButton);
drinksPanel.add(rootBeerButton);
drinksPanel.add(waterButton);
}
//method to tell user if drink is out of stock or if they have not added sufficent money to purchase drink
private void builduserFeedbackPanel()
{
userFeedbackPanel = new Panel();
TextField feedbackText = new TextField();
feedbackText.setEditable(false);
}
//listeners for all drink choice buttons in applet
private class colaButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
DecimalFormat dollar = new DecimalFormat("'$'0.00");
if(colaStock == 0)
{
feedbackText.setText("Insufficent stock available. Please make another selection.");
}
if(credit < cost)
{
feedbackText.setText("Insufficent funds. Please insert more money.");
}
if(credit >= cost)
{
colaStock -= 1;
change = credit - cost;
if(change == 0.00)
{
changeText.setText("");
}
else
{
changeText.setText(dollar.format(change));
}
}
}
}
private class lemonLimeButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
DecimalFormat dollar = new DecimalFormat("'$'0.00");
if(lemonLimeStock == 0)
{
feedbackText.setText("Insufficent stock available. Please make another selection.");
}
if(credit < cost)
{
feedbackText.setText("Insufficent funds. Please insert more money.");
}
if(credit >= cost)
{
lemonLimeStock -= 1;
change = credit - cost;
if(change == 0.00)
{
changeText.setText("");
}
else
{
changeText.setText(dollar.format(change));
}
}
}
}
private class grapeButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
DecimalFormat dollar = new DecimalFormat("'$'0.00");
if(grapeStock == 0)
{
feedbackText.setText("Insufficent stock available. Please make another selection.");
}
if(credit < cost)
{
feedbackText.setText("Insufficent funds. Please insert more money.");
}
if(credit >= cost)
{
grapeStock -= 1;
change = credit - cost;
if(change == 0.00)
{
changeText.setText("");
}
else
{
changeText.setText(dollar.format(change));
}
}
}
}
private class rootBeerButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
DecimalFormat dollar = new DecimalFormat("'$'0.00");
if(rootBeerStock == 0)
{
feedbackText.setText("Insufficent stock available. Please make another selection.");
}
if(credit < cost)
{
feedbackText.setText("Insufficent funds. Please insert more money.");
}
if(credit >= cost)
{
rootBeerStock -= 1;
change = credit - cost;
if(change == 0.00)
{
changeText.setText("");
}
else
{
changeText.setText(dollar.format(change));
}
}
}
}
private class waterButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
DecimalFormat dollar = new DecimalFormat("'$'0.00");
if(waterStock == 0)
{
feedbackText.setText("Insufficent stock available. Please make another selection.");
}
if(credit < cost)
{
feedbackText.setText("Insufficent funds. Please insert more money.");
}
if(credit >= cost)
{
waterStock -= 1;
change = credit - cost;
if(change == 0.00)
{
changeText.setText("");
}
else
{
changeText.setText(dollar.format(change));
}
}
}
}
//listeners for all monetary choices available to user for purchasing drink
private class nickelButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
DecimalFormat dollar = new DecimalFormat("'$'0.00");
credit += .05;
creditText.setText(dollar.format(credit));
}
}
private class dimeButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
DecimalFormat dollar = new DecimalFormat("'$'0.00");
credit += .10;
creditText.setText(dollar.format(credit));
}
}
private class quarterButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
DecimalFormat dollar = new DecimalFormat("'$'0.00");
credit += .25;
creditText.setText(dollar.format(credit));
}
}
private class dollarButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
DecimalFormat dollar = new DecimalFormat("'$'0.00");
credit += 1.00;
creditText.setText(dollar.format(credit));
}
}
}
答案 0 :(得分:0)
在每个&#39; build&#39;的第一行中。您似乎在声明Panel变量的方法。大多数这些声明都是错误的。变量已经在类中声明为字段,您只需要实例化它们。 drinksPanel = new Panel();
是正确的; Panel userMoney = new Panel();
错了。因此,私有字段userMoneyPanel
从未实例化,从而导致异常。但是,这是很多代码,如果我错误地识别了问题,那就很抱歉。
答案 1 :(得分:0)
您没有在构建器方法中设置字段。这意味着当您将字段添加到容器时,字段为null
。
例如
private Panel userMoneyPanel;
...
private void builduserMoneyPanel()
{
// Panel userMoney = new Panel(); // this is wrong...
userMoneyPanel = new Panel();
...
答案 2 :(得分:0)
在构建面板的不同方法中,您使用本地变量而不是初始化类中的字段。例如:
private void builduserMoneyPanel()
{
Panel userMoney = new Panel(); // <= This is not good !
//...
}
执行:
private void builduserMoneyPanel()
{
userMoney = new Panel(); // initialise this.userMoney
//...
}
4种建筑方法相同。