import java.awt.*;
import java.awt.event.*;
import java.util.*;
class WindowTextArea extends Frame implements TextListener
{
TextArea text1,text2;
WindowTextArea()
{
setLayout(new FlowLayout());
text1=new TextArea(6,15);
text1=new TextArea(6,15);
add(text1);
add(text2);
text1.addTextListener(this);
setBounds(100,100,100,100);
setVisible(true);
validate();
}
public void textValueChanged(TextEvent e)
{ String s=text1.getText();
StringTokenizer fenxi=new StringTokenizer(s," ,'\n'");
int n=fenxi.countTokens();
String a[]=new String[n];
double sum=0,average=0,item=0;
for(int i=0;i<=n-1;i++)
{
String temp=fenxi.nextToken();
a[i]=temp;
try{
// item=Double.parseDouble(a[i]);
sum=sum+item;
average=sum/i;
text2.append(sum+average+"\n");//.toString());
}
catch(NumberFormatException ex)
{
System.out.println("你输入了非数字字符:"+ex);
}
}
}
}
public class P211_4
{
public static void main(String args[])
{
WindowTextArea win=new WindowTextArea();
}
}
编译后:NullPointerException错误?请指教,thx~