我有一個運行時錯誤,指出線程中的異常「AWT-EventQueue-0」java.lang.NullPointerException at Status.copyState(Status.java:29)這是一個類即撥打呼叫的狀態(ST)的一個實例:主類是:調用另一個Java類的方法的問題
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
/**
*
*
* @author (Jason Sizemore)
* @version (11-20-09 HW09)
*/
public class BetterCalculator extends Calculator
{
//attributes
private JButton undo;
private String undoText;
private Status st;
public BetterCalculator()
{
super();
st = new Status();
}
public void createUserInterface3()
{
createUserInterface2();
undo = new JButton("undo");
jPanel.add(undo);
undo.setBackground(Color.red);
undo.setToolTipText("This is the undo feature");
undo.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
super.actionPerformed(e);
while(displayBox.getText() != null)
{
st.copyState();
}
if(e.getSource() == undo)
{
Status st;
st = new Status();
undoText = st.returnState();
displayBox.setText(undoText);
}
}
public static void main(String[] args)
{
BetterCalculator myCalc;
myCalc = new BetterCalculator();
myCalc.createUserInterface3();
}
}
它是通過狀態的一個實例(ST)下面是狀態類
import java.util.*;
import java.awt.event.*;
import java.awt.*;
/**
* Write a description of class Status here.
*
* @author (Jason Sizemore)
* @version (HW09 11-21-09)
* This is a class to get the status for the undo feature
*/
public class Status extends BasicCalculator
{
//attributes
private ArrayList<String> lastState;
public String ls;
public String rls;
//constructors
public Status()
{
lastState = new ArrayList<String>(10);
}
//Methods
public void copyState()
{
//operand1 = Double.parseDouble(displayBox.getText());
ls = displayBox.getText();
lastState.add(ls);
}
public String returnState()
{
int sizeOfArrayList;
sizeOfArrayList = lastState.size();
rls = lastState.get(sizeOfArrayList);
return rls;
}
}
我知道問題是與行ls = displayBox.getText();我有一個對象需要在對象上返回一個方法。我在這裏錯過了什麼。
感謝您的任何幫助。
'dispayBox'聲明或甚至實例化在哪裏? – 2009-11-23 17:25:52
displayBox繼承自這個類繼承的Calculator類 – 2009-11-23 17:30:41