【问题标题】:Java initialization questionJava初始化问题
【发布时间】:2011-08-20 06:59:39
【问题描述】:
JPanel p2 = new JPanel(new GridLayout(6,1));
ButtonGroup tubtype = new ButtonGroup();
JRadioButton roundrButton = new JRadioButton("Round", true);
tubtype.add(roundrButton);
JRadioButton ovalrButton = new JRadioButton("Oval", false);
tubtype.add(ovalrButton);
calcButton = new JButton("Calculate Volume");
exitButton = new JButton("Exit");
hlength = new JTextField(5);
hwidth = new JTextField(5);
hdepth = new JTextField(5);
hvolume = new JTextField(5);
lengthLabel = new JLabel("Enter the tub's length (ft):");
widthLabel = new JLabel("Enter the tub's width (ft):");
depthLabel = new JLabel("Enter the tub's depth (ft):");
volumeLabel = new JLabel("The tub's volume (ft^3):");
p2.add(roundrButton);
p2.add(ovalrButton);
p2.add(lengthLabel);
p2.add(hlength);
p2.add(widthLabel);
p2.add(hwidth);
p2.add(depthLabel);
p2.add(hdepth);
p2.add(volumeLabel);
p2.add(hvolume);
p2.add(calcButton);
p2.add(exitButton);
tab.addTab( "Hot Tubs", null, p2, " Panel #1" );

calcButtonHandler2 ihandler =new calcButtonHandler2();
calcButton.addActionListener(ihandler);
exitButtonHandler ghandler =new exitButtonHandler();
exitButton.addActionListener(ghandler);
FocusHandler hhandler =new FocusHandler();
hlength.addFocusListener(hhandler);
hwidth.addFocusListener(hhandler);
hdepth.addFocusListener(hhandler);
hvolume.addFocusListener(hhandler);

// add JTabbedPane to container
getContentPane().add( tab );
setSize( 550, 500 );
setVisible( true );
} 
public class calcButtonHandler implements ActionListener {

public void actionPerformed(ActionEvent e) {
DecimalFormat num =new DecimalFormat(",###.##");
double sLength, sWidth, sdepth, Total;

sLength = Double.valueOf(plength.getText());

sWidth =Double.valueOf(pwidth.getText());

sdepth =Double.valueOf(pdepth.getText());

if(e.getSource() == pcalcButton) {
Total = sLength * sWidth * sdepth;
pvolume.setText(num.format(Total));
try{
String value=pvolume.getText();
File file = new File("output.txt");
FileWriter fstream = new FileWriter(file,true);
BufferedWriter out = new BufferedWriter(fstream);
out.write("Length= "+sLength+", Width= "+sWidth+", Depth= "+sdepth+" so the volume of Swimming  Pool is "+value);
out.newLine();
out.close();
}
catch(Exception ex){}
}
}
}

public class calcButtonHandler2 implements ActionListener {

public void actionPerformed(ActionEvent g) {
DecimalFormat num =new DecimalFormat(",###.##");
double cLength, cWidth, cdepth, Total;

cLength = Double.valueOf(hlength.getText());

cWidth = Double.valueOf(hwidth.getText());

cdepth = Double.valueOf(hdepth.getText());

try
{
AbstractButton roundrButton;
if(roundrButton.isSelected())
{

Total = Math.PI * Math.pow(cLength / 2.0, 2) * cdepth;
}
else 
{
Total = Math.PI * Math.pow(cLength * cWidth, 2) * cdepth;
}
hvolume.setText(""+num.format(Total));
}

catch(Exception ex){}
}
}
}



class exitButtonHandler implements ActionListener {
    public void actionPerformed(ActionEvent g){
        System.exit(0);
    }
}
class FocusHandler implements FocusListener {
    public void focusGained(FocusEvent e) {
    }
    public void focusLost(FocusEvent e) {
    }


public static void main( String args[] )
{
    Final tabs = new Final();
    tabs.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}

我收到一条错误消息,提示我的 roundrButton 可能尚未初始化。请帮忙。

【问题讨论】:

  • 您需要包含整个课程,或者包含一个最小但完整的课程来展示问题。不幸的是,您提供的代码对于确定您的问题不是特别有用:它确实说明了问题所在,但这不是您感兴趣的内容。完整的代码示例和编译器错误的粘贴将非常有帮助找出问题所在。

标签: variables initialization local


【解决方案1】:

问题出在这几行:

try
{
    AbstractButton roundrButton;
    if(roundrButton.isSelected())
    {

您正在声明一个名为roundrButton 的变量,然后尝试对其调用.isSelected()。但是,您永远不会将任何内容分配给 roundButton,因此它将是 null

您似乎想要引用您之前声明的roundrButton。您是否考虑过将其作为您班级的一个领域?

【讨论】:

  • 不,因为我不知道该怎么做。我认为如果我调用 .isSelected 它会知道我想要它做什么......
【解决方案2】:

错误发生在这里:

try {
    AbstractButton roundrButton;
    if (roundrButton.isSelected()) {

您声明了一个名为 roundrButton 的变量,但没有为其分配任何对象。

然后你调用了一个方法,好吧,谁知道呢?没有可调用该方法的对象,因为变量roundrButton 从未收到值。

【讨论】:

    【解决方案3】:

    我猜你的问题出在 calcButtonHandler2 类的 actionPerformed 方法中:

        try { 
    AbstractButton roundrButton;
    if(roundrButton.isSelected())
    

    roundrButton 确实没有初始化,运行时会导致 NullPointerException。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-16
      • 1970-01-01
      • 1970-01-01
      • 2020-12-25
      • 2011-10-13
      • 2011-10-29
      • 2020-10-17
      • 2020-09-24
      相关资源
      最近更新 更多