当前位置: 代码迷 >> Java相关 >> NullPointerException错误
  详细解决方案

NullPointerException错误

热度:609   发布时间:2007-05-12 11:57:37.0
NullPointerException错误

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~

搜索更多相关的解决方案: public  import  

----------------解决方案--------------------------------------------------------
以下是引用Sieben7在2007-5-12 11:57:37的发言:

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);//你这里初始化两次text1干什么,text2没有初始化,当然是空指针了
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~

还有,其实你要学会自己去找错误

异常信息已经很详细地告诉你哪里错了,异常如下
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1027)
at java.awt.Container.add(Container.java:352)
at WindowTextArea.<init>(P211_4.java:14)//在这里就提示了你,在构造里面里面,在第14行.
at P211_4.main(P211_4.java:53)


----------------解决方案--------------------------------------------------------
嗯。谢谢斑斑o(∩_∩)o...
----------------解决方案--------------------------------------------------------
  相关解决方案