import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class WordCounter extends JFrame implements ActionListener
{
public JTextArea textPane;
public JTextField fieldWordCount;
public WordCounter()
{
super("Tammy's Word Counter Demo");
Container c = getContentPane();
//button panel
JPanel buttonPanel = new JPanel();
JButton countButton = new JButton("Count Words");
buttonPanel.add(countButton);
countButton.addActionListener(this);
JLabel labelWordCount = new JLabel("Word Count = ");
fieldWordCount = new JTextField(5);
buttonPanel.add(labelWordCount);
buttonPanel.add(fieldWordCount);
c.add(buttonPanel, "South");
//text panel
JPanel textPanel = new JPanel();
c.add( textPanel, "Center");
JTextArea textPane = new JTextArea(12,32);
textPane.setLineWrap(true);
JScrollPane scrollPane = new JScrollPane(textPane);
textPanel.add(scrollPane);
}//end WordCounter constructor
public void actionPerformed(ActionEvent e)
{
String s = textPane.getText(); // get text that was input from user
fieldWordCount.setText( Integer.toString( words( s ) ) );
repaint();
}
public int words( String sentence ) // method for counting words
{
String stringToTokenize = sentence;
StringTokenizer tokens =
new StringTokenizer( stringToTokenize );
return tokens.countTokens();
} // end words method
public static void main(java.lang.String[] args)
{
WordCounter frame = new WordCounter();
frame.setSize(500,200);
frame.setVisible(true);
}//end main method
}
2 Replies - 2423 Views - Last Post: 26 July 2007 - 05:42 PM
#1
NullPointerException error passing JTextArea contents
Posted 26 July 2007 - 04:12 PM
My code compiles, however after I type my sample string into the JTextarea and click the CountButton it fails. I get a NullPointerException error in my command window. I thought that String s = textPane.getText(); would retrive the text typed into the window but I guess I missed something.
Replies To: NullPointerException error passing JTextArea contents
#2
Re: NullPointerException error passing JTextArea contents
Posted 26 July 2007 - 05:25 PM
Hi I made it work and you wouldn't believe what was the problem.
It was declaring twice the text area
first you've made this:
and a little below you made this:
that was the whole problem.
So just remove the second declaration and it'll work.
This is the working code:
It was declaring twice the text area
first you've made this:
Quote
public JTextArea textPane;
and a little below you made this:
Quote
JTextArea textPane = new JTextArea(12,32);
that was the whole problem.
So just remove the second declaration and it'll work.
This is the working code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class WordCounter extends JFrame implements ActionListener
{
public JTextArea textPane;
public JTextField fieldWordCount;
public WordCounter()
{
super("Tammy's Word Counter Demo");
Container c = getContentPane();
//button panel
JPanel buttonPanel = new JPanel();
JButton countButton = new JButton("Count Words");
buttonPanel.add(countButton);
countButton.addActionListener(this);
JLabel labelWordCount = new JLabel("Word Count = ");
fieldWordCount = new JTextField(5);
buttonPanel.add(labelWordCount);
buttonPanel.add(fieldWordCount);
c.add(buttonPanel, "South");
//text panel
JPanel textPanel = new JPanel();
c.add( textPanel, "Center");
textPane = new JTextArea(20,35);
textPane.setLineWrap(true);
JScrollPane scrollPane = new JScrollPane(textPane);
textPanel.add(scrollPane);
}//end WordCounter constructor
public void actionPerformed(ActionEvent e)
{
String s = textPane.getText(); // get text that was input from user
fieldWordCount.setText(""+words( s )+"");
//repaint();
}
public int words( String sentence ) // method for counting words
{
String stringToTokenize = sentence;
StringTokenizer tokens = new StringTokenizer( stringToTokenize );
return tokens.countTokens();
} // end words method
public static void main(java.lang.String[] args)
{
WordCounter frame = new WordCounter();
frame.setSize(500,200);
frame.setVisible(true);
}//end main method
}
#3
Re: NullPointerException error passing JTextArea contents
Posted 26 July 2007 - 05:42 PM
Well I can't thank you enough! I was focusing on the button and the actionlistener code. Being the novice I am, I might never have noticed that little glitch!! Thanks again!
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|