2014-03-12 2 views
0

이것은 내 첫 번째 게시물이므로 쉽게 이동하십시오. 나는 하나의 일을하고 있으며, 전체 프로젝트에 대한 도움이나 조언을 구하지 않고있다. 전 주석 때 는 기본적으로 모든 일이 좀 작동합니까 루프에 대한이가 배열을 통해 그것의 방법을 작동하도록 설계되어 있지만 넷빈즈가 향상된 루프로 변경하는 말해 계속하지만배열에 대한 for 루프 때문에 JApplet이 충돌 함

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

내가 할 때 그것은 추락한다. 전체 프로그램은 JAplet 인터페이스의 입력을 받아서 별도의 java 파일로 stringAnalyser으로 보내고 단어의 길이와 그 길이의 단어가 기록 된 빈도를 기록한 다음 다시 인터페이스로 보내고 화면에 인쇄합니다. 센터 콘솔.

는 여기에 도움이된다면 내가 사용하고 두 파일은 다음과 같습니다

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` 

입력 일체의 사전에 감사 :

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, 이것은 분석 자바 파일입니다.

+1

안녕하세요! 관련 코드 만 게시하십시오 (자세한 내용이 필요하면 요청할 것입니다). 또한 어떤 줄이 충돌하고 오류 유형을 게시하십시오. –

+0

_ "NetBeans는 강화 된 for 루프로 변경하라고 알려주지 만 충돌이 발생할 때"_ - 그냥 제안입니다. 당신은 그것을하지 않아도됩니다. –

+0

_ "충돌"정의 _ –

답변

0

btnEnter.addActionListener에서 입력을 설정하는 사람은 tim.setText(inputString);입니다. 그러나 당신은 output() 또는 입력을 처리하기로되어있는 분석기에 요구되는 다른 방법을 결코 호출하지 않습니다. 결과적으로 tim.sendBack()null을 반환합니다. stringAnalyser.sendStringoutput() 내부에서만 초기화됩니다. 그때까지는 null입니다. 따라서 words 배열이 null이기 때문에 words.lengthNullPointerException을 트리거합니다.

게시 된 코드의 들여 쓰기 및 이름 지정은 때때로 어렵습니다. 특히 Java Code Conventions, Naming Conventions 섹션을 참조하십시오. 대부분의 도구는 코드를 자동 들여 쓰기 할 수 있습니다. 즉, ctrl + shift + f입니다.

+1

답장을 보내 주셔서 대단히 죄송합니다. 이 프로젝트는 완료되었고, 내가 달성하려고 시도했던 것과 내가 그것을 구현하려고 시도한 방식에 대한 큰 오해가 있었던 것 같습니다. 도움과 ctrl + shift + f 단축키를 이용해 주셔서 다시 한번 감사드립니다. Tim. – techytimB

+0

@techytimB 환영합니다! 나는 그것이 다 잘 되었기 때문에 기쁘다 :) – tenorsax