2 Replies - 3007 Views - Last Post: 26 July 2007 - 05:42 PM Rate Topic: -----

#1 yogi_bear_79  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-July 07

NullPointerException error passing JTextArea contents

Post icon  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.


 
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

}



Is This A Good Question/Topic? 0
  • +

Replies To: NullPointerException error passing JTextArea contents

#2 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

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:

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

}

Was This Post Helpful? 0
  • +
  • -

#3 yogi_bear_79  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-July 07

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!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1