2014-03-12 36 views
0

這是我的第一篇文章,所以對我來說很簡單。 我正在做一些uni的工作,我沒有要求任何幫助或整個項目的建議。 基本上當我註釋掉整個事情確實有點兒工作一個循環: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

歡迎來到SO!請只發布相關代碼(如果我們需要更多,我們會問)。另外,請發佈什麼行崩潰,錯誤類型。 –

+0

_「NetBeans不斷告訴我將其改爲增強的for循環,但是當我這樣做時崩潰」_ - 這只是一個建議。你沒有這樣做。 –

+0

另外定義_「崩潰」_ –

回答

0

看來,在btnEnter.addActionListener你只能調用tim.setText(inputString);它設置輸入。但是您絕對不要在分析儀上調用output()或任何其他必需的方法來處理輸入。因此,tim.sendBack()返回nullstringAnalyser.sendString僅在output()內初始化。在此之前它仍然是null。所以words.length觸發器NullPointerException因爲words數組是null

張貼代碼的縮進和命名有時難以遵循。特別參見Java Code Conventions,Naming Conventions部分。大多數工具都可以自動縮進代碼,例如ctrl + shift + f在Eclipse中。

+1

感謝您對此的幫助,抱歉在回覆中漫長的等待。這個項目確實完成了,似乎我對我試圖實現的目標以及我試圖實現它的方式產生了很大的誤解。 再次感謝您的幫助和方便的ctrl + shift + f快捷方式。 Tim。 Tim。 – techytimB

+0

@techytimB不客氣!我很高興這一切工作:) – tenorsax