由于数组的for循环,JApplet崩溃

时间:2014-03-12 18:28:59

标签: java arrays swing for-loop japplet

这是我的第一篇文章,所以对我很轻松。 我正在做一些单一的工作,我不是要求整个项目提供任何帮助或建议。 基本上,当我注释掉一个for循环时,整个事情确实有点工作:

          for(int i=0;i<words.length;i++)
          {
          textArea.append(words[i] + "\n");
          }

这是为了通过数组工作,但NetBeans一直告诉我将其更改为增强的for循环,但当我这样做时崩溃。整个程序旨在从JAplet接口获取输入,将其发送到单独的java文件中的stringAnalyser,记录单词的长度以及该长度的单词出现的频率,然后将其发回到界面并在中央控制台的屏幕上打印它们。

以下是我使用的两个文件,如果有帮助的话:

package Assignment2;

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.GridLayout;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowListener;
    import javax.swing.*;
    import javax.swing.border.BevelBorder;
    import javax.swing.JPanel;

    public class GUIAssign extends JApplet 
    {
    private JPanel pnorth,psouth,pcentre,pwest,peast;
    private JButton btnEnter,btnReset,btnSave,btnLoad;
    private JTextField txtEnter;
    private JLabel label1,label2,header;
    private JTextArea textArea;
    private Container c;
    private paintPanel panelpaint;
    private String inputString,shape;
    private int x,y,size;
    private stringAnalyser tim;
        @Override
        public void init()
        {
    this.setVisible(true);
    this.setSize(800,600);
    initComponents();
    initPanels();
    initButtons();
    initText();
    initLabels();
    tim=new stringAnalyser();

        }
public void initComponents()
{
x=0;
y=0;
size=0;
this.panelpaint=new paintPanel();
}
public void initPanels()
{
 pnorth= new JPanel();
 pnorth.setBackground(Color.CYAN);
 this.psouth = new JPanel();
 psouth.setBackground(Color.BLUE);
 this.peast =new JPanel();
 peast.setBackground(Color.CYAN);
 this.pwest = new JPanel();
 pwest.setBackground(Color.CYAN);
 this.pcentre = new JPanel();
 pcentre.setBackground(Color.yellow);

}
String words[];
 public void initButtons()
{

  this.btnEnter=new JButton("Enter");
 btnEnter.addActionListener(new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent ae) {
        inputString=txtEnter.getText();
          tim.setText(inputString);
          words=tim.sendBack();

          for(int i=0;i<words.length;i++)
          {
          textArea.append(words[i] + "\n");
          }
          tim.output();


      }
  });


 this.btnReset=new JButton("Reset");
  btnReset.addActionListener(new ActionListener(){

      @Override
      public void actionPerformed(ActionEvent ae) {
          txtEnter.setText("");
          }

  });


  this.btnSave=new JButton("Save");
  this.btnLoad=new JButton("Load");
}
 public void initText()
 {
   txtEnter=new JTextField();
   textArea=new JTextArea();


         }
         public void initLabels()
         {
     this.label1=new JLabel("");
     this.label2=new JLabel("");
         }

       //structure
          @Override
       public void start()
       {
           panelEast();
           panelWest();
           panelNorth();
           panelSouth();
           panelCentre();
         c=this.getContentPane();
        c.setBackground(Color.LIGHT_GRAY);
         c.setLayout(new BorderLayout());
         c.add(pnorth,BorderLayout.NORTH);
         c.add(peast,BorderLayout.EAST);
         c.add(psouth,BorderLayout.SOUTH);
         c.add(pwest,BorderLayout.WEST);
         c.add(pcentre,BorderLayout.CENTER);

       }

public void panelNorth()
{
    Font f=new Font("Jokerman",Font.BOLD,16);
   pnorth.setLayout(new GridLayout(1,3));
   pnorth.add(new JLabel("           "));
    pnorth.add(new JLabel("Tim's String Lenght Tester"));
    pnorth.add(new JLabel("           "));

}
public void panelWest()
{
  pwest.add(new JLabel("                "));
   //pwest.add(new JLabel(new ImageIcon("src/tim.jpg")));
}
 public void panelEast()
{
  peast.add(new JLabel("                "));
}
public void panelSouth()
{
    psouth.setLayout(new FlowLayout());
    JPanel psouth1=new JPanel();
    JPanel psouth2=new JPanel();


    psouth1.setLayout(new GridLayout(1,3));
    psouth1.setBackground(Color.CYAN);
    psouth1.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED,Color.cyan, Color.black, Color.black, Color.cyan));
    psouth1.add(this.txtEnter);
    psouth1.add(this.btnEnter);
    psouth1.add(this.btnReset);


    psouth2.setLayout(new GridLayout(1,2));
    psouth2.setBackground(Color.LIGHT_GRAY);
    psouth2.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED,Color.cyan, Color.black, Color.black, Color.cyan));

    psouth2.add(this.btnSave);
    psouth2.add(this.btnLoad);





    psouth.add(psouth1);
     psouth.add(psouth2);
}
public void panelCentre()
{

    pcentre.setLayout(new GridLayout(1,2));
    JPanel pcentre1=new JPanel();
    pcentre.setBackground(Color.GREEN);
    pcentre.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED,Color.green,Color.green));
    pcentre.add(textArea);
    textArea.setBackground(Color.LIGHT_GRAY);
    textArea.setForeground(Color.YELLOW);
    pcentre.add(pcentre1);


        }


         @Override
        public void setName(String n) {
            this.inputString=n;

        }

            public String getNameString() {
           return inputString;
        }


    @Override
       public void stop(){}
    @Override
       public void destroy(){}
    @Override
       public void paint(Graphics g){

       }




    }//close project`

文件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`

提前感谢您提供任何和所有意见。

1 个答案:

答案 0 :(得分:0)

似乎在btnEnter.addActionListener中你只调用设置输入的tim.setText(inputString);。但是你永远不会在分析器上调用output()或任何其他必需的方法来处理输入。因此,tim.sendBack()会返回nullstringAnalyser.sendString仅在output()内初始化。在那之前它仍然是null。因此words.length会触发NullPointerException,因为words数组是null

发布的代码的缩进和命名有时难以遵循。请参阅Java Code ConventionsNaming Conventions部分。大多数工具都可以自动缩进代码,即Eclipse中的 ctrl + shift + f