Java JApplet因阵列的for循环而崩溃
这是我的第一篇帖子,所以对我放松点。 我正在做一些简单的工作,在整个项目中我不需要任何帮助或建议。 基本上,当我对for循环进行注释时,整个过程都能正常工作:Java JApplet因阵列的for循环而崩溃,java,arrays,swing,for-loop,japplet,Java,Arrays,Swing,For Loop,Japplet,这是我的第一篇帖子,所以对我放松点。 我正在做一些简单的工作,在整个项目中我不需要任何帮助或建议。 基本上,当我对for循环进行注释时,整个过程都能正常工作: for(int i=0;i<words.length;i++) { textArea.append(words[i] + "\n"); } 文件2,这是分析java文件: package Assignment2; public cl
for(int i=0;i<words.length;i++)
{
textArea.append(words[i] + "\n");
}
文件2,这是分析java文件:
package Assignment2;
public class stringAnalyser{
//static String inputArray[]= {"Tim","John","Craig", "Andrew", "Bob", "Tom", "Phillipa","Billie-Bob"};
private String inputString = "";
private String removedCharString;//will store the string with removed chars
private String array3="",array4="", array5="", array6="", array7="", array8="", array9="", array10="",array11="",
array12="",array13="",array14="",array15="";
private String[] sendString;
private int count=0;
//will add the words with the appropriate length to a string for printing
public void setText(String input)
{
this.inputString=input;
}
public String showme()
{
return "Think is not difficult believe is the word and look at me I am the great evidence!!!";
}
public void output()
{
int three=0, four=0, five=0, six=0, seven=0, eight=0, nine=0, ten=0,eleven=0,twelve=0,thirt=0,fort=0,fift=0;
// these ints will count how many times the words relevent length have accoured in a string
String w[];
removedCharString = inputString.replaceAll("[^a-zA-Z\\s]", "");//remove chars
w=removedCharString.split(" ");
this.sendString=new String[w.length];
for (String retval: removedCharString.split(" "))//split at any spaces
{
if (retval.length()==3){
three++;
array3 = array3 + retval + ", ";
}//if 3
if (retval.length()==4){
four++;
array4 = array4 + retval + ", ";
}//if 4
if (retval.length()==5){
five++;
array5 = array5 + retval + ", ";
}//if 5
if (retval.length()==6){
six++;
array6 = array6 + retval + ", ";
}//if 6
if (retval.length()==7){
seven++;
array7 = array7 + retval + ", ";
}//if 7
if (retval.length()==8){
eight++;
array8 = array8 + retval + ", ";
}//if 8
if (retval.length()==9){
nine++;
array9 = array9 + retval + ", ";
}//if 9
if (retval.length()==10){
ten++;
array10 = array10 + retval + ", ";
}//if 10
if (retval.length()==11){
eleven++;
array11 = array11 + retval + ", ";
}//if 10
if (retval.length()==12){
twelve++;
array12 = array12 + retval + ", ";
}//if 10
if (retval.length()==13){
thirt++;
array13 = array13 + retval + ", ";
}//if 10
if (retval.length()==14){
fort++;
array14 = array14 + retval + ", ";
}//if 10
if (retval.length()==15){
fift++;
array15 = array15 + retval + ", ";
}//if 10
}//for string retval
// print the results
System.out.println(inputString);
if (three!=0){
count++;
sendString[count]="There are: "+three+" three letter word/s; "+array3;
System.out.println("There are: "+three+" three letter word/s; "+array3);}
if (four!=0){
count++;
sendString[count]="There are: "+four+" four letter word/s; "+array4;
System.out.println("There are: "+four+" four letter word/s; "+array4);}
if (five!=0){
System.out.println("There are: "+five+" five letter word/s; "+array5);}
if (six !=0){
System.out.println("There are: "+six+" six letter word/s; "+array6);}
if (seven !=0){
System.out.println("There are: "+seven+" seven letter word/s; "+array7);}
if (eight!=0){
System.out.println("There are: "+eight+" eigth letter word/s; "+array8);}
if (nine!=0){
System.out.println("There are: "+nine+" nine letter word/s; "+array9);}
if (ten!=0){
System.out.println("There are: "+ten+" ten letter word/s;"+array10);}
if (eleven!=0){
System.out.println("There are: "+eleven+" eleven letter word/s;"+array11);}
if (twelve!=0){
System.out.println("There are: "+twelve+" twelve letter word/s;"+array12);}
if (thirt!=0){
System.out.println("There are: "+thirt+" thirteen letter word/s;"+array13);}
if (fort!=0){
System.out.println("There are: "+fort+" forteen letter word/s;"+array14);}
if (fift!=0){
System.out.println("There are: "+fift+" fifteen letter word/s;"+array15);}
}//output
public String[] sendBack()
{
return this.sendString;
}
public static void main(String []args)
{
stringAnalyser a=new stringAnalyser();
a.output();
}//close main
}//class`
提前感谢您的所有输入。在
btenter.addActionListener
中,您似乎只调用tim.setText(inputString)设置输入的代码>。但是您从不调用output()
或分析器上任何其他需要处理输入的方法。因此,tim.sendBack()
返回null
stringanalyzer.sendString
仅在output()
内初始化。在此之前,它将保持null
。所以words.length
触发NullPointerException
,因为words
数组是null
所发布代码的缩进和命名有时有点难以理解。请参阅Java,特别是第节。大多数工具都可以为您自动缩进代码,即Eclipse中的ctrl+shift+f 欢迎来到SO!请只张贴相关代码(如果我们需要更多,我们会询问)。另外,请发布哪一行崩溃,以及错误类型。“NetBeans一直告诉我将其更改为增强的for循环,但当我这样做时崩溃”——这只是一个建议。你不必这么做。还要定义“崩溃”谢谢你在这方面的帮助,很抱歉在回复中等待了很长时间。这个项目确实完成了,我似乎对我试图实现的目标和我试图实现它的方式有很大的误解。再次感谢您的帮助和方便的ctrl+shift+f快捷方式。提姆。
package Assignment2;
public class stringAnalyser{
//static String inputArray[]= {"Tim","John","Craig", "Andrew", "Bob", "Tom", "Phillipa","Billie-Bob"};
private String inputString = "";
private String removedCharString;//will store the string with removed chars
private String array3="",array4="", array5="", array6="", array7="", array8="", array9="", array10="",array11="",
array12="",array13="",array14="",array15="";
private String[] sendString;
private int count=0;
//will add the words with the appropriate length to a string for printing
public void setText(String input)
{
this.inputString=input;
}
public String showme()
{
return "Think is not difficult believe is the word and look at me I am the great evidence!!!";
}
public void output()
{
int three=0, four=0, five=0, six=0, seven=0, eight=0, nine=0, ten=0,eleven=0,twelve=0,thirt=0,fort=0,fift=0;
// these ints will count how many times the words relevent length have accoured in a string
String w[];
removedCharString = inputString.replaceAll("[^a-zA-Z\\s]", "");//remove chars
w=removedCharString.split(" ");
this.sendString=new String[w.length];
for (String retval: removedCharString.split(" "))//split at any spaces
{
if (retval.length()==3){
three++;
array3 = array3 + retval + ", ";
}//if 3
if (retval.length()==4){
four++;
array4 = array4 + retval + ", ";
}//if 4
if (retval.length()==5){
five++;
array5 = array5 + retval + ", ";
}//if 5
if (retval.length()==6){
six++;
array6 = array6 + retval + ", ";
}//if 6
if (retval.length()==7){
seven++;
array7 = array7 + retval + ", ";
}//if 7
if (retval.length()==8){
eight++;
array8 = array8 + retval + ", ";
}//if 8
if (retval.length()==9){
nine++;
array9 = array9 + retval + ", ";
}//if 9
if (retval.length()==10){
ten++;
array10 = array10 + retval + ", ";
}//if 10
if (retval.length()==11){
eleven++;
array11 = array11 + retval + ", ";
}//if 10
if (retval.length()==12){
twelve++;
array12 = array12 + retval + ", ";
}//if 10
if (retval.length()==13){
thirt++;
array13 = array13 + retval + ", ";
}//if 10
if (retval.length()==14){
fort++;
array14 = array14 + retval + ", ";
}//if 10
if (retval.length()==15){
fift++;
array15 = array15 + retval + ", ";
}//if 10
}//for string retval
// print the results
System.out.println(inputString);
if (three!=0){
count++;
sendString[count]="There are: "+three+" three letter word/s; "+array3;
System.out.println("There are: "+three+" three letter word/s; "+array3);}
if (four!=0){
count++;
sendString[count]="There are: "+four+" four letter word/s; "+array4;
System.out.println("There are: "+four+" four letter word/s; "+array4);}
if (five!=0){
System.out.println("There are: "+five+" five letter word/s; "+array5);}
if (six !=0){
System.out.println("There are: "+six+" six letter word/s; "+array6);}
if (seven !=0){
System.out.println("There are: "+seven+" seven letter word/s; "+array7);}
if (eight!=0){
System.out.println("There are: "+eight+" eigth letter word/s; "+array8);}
if (nine!=0){
System.out.println("There are: "+nine+" nine letter word/s; "+array9);}
if (ten!=0){
System.out.println("There are: "+ten+" ten letter word/s;"+array10);}
if (eleven!=0){
System.out.println("There are: "+eleven+" eleven letter word/s;"+array11);}
if (twelve!=0){
System.out.println("There are: "+twelve+" twelve letter word/s;"+array12);}
if (thirt!=0){
System.out.println("There are: "+thirt+" thirteen letter word/s;"+array13);}
if (fort!=0){
System.out.println("There are: "+fort+" forteen letter word/s;"+array14);}
if (fift!=0){
System.out.println("There are: "+fift+" fifteen letter word/s;"+array15);}
}//output
public String[] sendBack()
{
return this.sendString;
}
public static void main(String []args)
{
stringAnalyser a=new stringAnalyser();
a.output();
}//close main
}//class`