this is my first post so go easy on me. I'm doing some uni work and im not asking for any help or advice for the whole project. Basically the whole thing does work kinda when i comment out a for loop:
for(int i=0;i<words.length;i++)
{
textArea.append(words[i] + "\n");
}
this is designed to work its way through an array but NetBeans keeps telling me to change it into an enhanced for loop but when i do it crashes. The whole program is designed to take an input from a JAplet
interface, send it to stringAnalyser
in a separate java file, record the length of words and how often words of that length occur, then send them back to the interface and print them onscreen in the centre console.
Here are both files i'm using if it helps:
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`
file 2, this is the analysing java file:
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`
thank you in advance for any and all input.
It seems that in btnEnter.addActionListener
you only invoke tim.setText(inputString);
which sets the input. But you never call output()
or any other required methods on the analyzer that supposed to process the input. As a result, tim.sendBack()
returns null
. stringAnalyser.sendString
is initialized only inside output()
. Until then it remains null
. So words.length
triggers NullPointerException
because words
array is null
.
The indentation and naming of the posted code is bit hard to follow at times. See Java Code Conventions, Naming Conventions section in particular. Most tools can auto indent the code for you, ie ctrl+shift+f in Eclipse.