Sagewire Logo

Help with Button action

4 Message(s) by 2 Author(s) originally posted in java language


From: Yogi_Bear_79 Date:   Thursday, July 26, 2007
I am having a problem with my code below. It compiles, however whenever I
click the Countbutton I get a slew of output information in my command
window. It starts with: Exception in thread "AWT-EventQueue-0"
JAVA.lang.NullPointerException at
Word Cunter.actionPerformed{WordCounter.JAVA:43)

I think I have the program pretty much correct, I am stuck on getting the
results (words counted) to appear in the JtextField after I press the
CountButton, Thanks for any help.

import JAVA.awt.*;
import JAVA.awt.event.*;
import JAVAx.swing.*;
import JAVA.util.*;

public class WordCounter extends JFrame implements ActionListener
{

public JTextArea text Pane;
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 string ToTokenize = 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

}


From: Lew Date:   Thursday, July 26, 2007
wrote in message:
I am having a problem with my code below. It compiles, however whenever I
click the Countbutton I get a slew of output information in my command
window. It starts with: Exception in thread "AWT-EventQueue-0"
JAVA.lang.NullPointerException at
WordCunter.actionPerformed{WordCounter.JAVA:43)
I think I have the program pretty much correct, I am stuck on getting the



Except for the NPE, caused by the fact that you never set the "textPane"
member variable to a non-null value.

results (words counted) to appear in the JtextField after I press the
CountButton, Thanks for any help.

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);



This declaration masks the member variable.

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



The "textPane" variable was initialized to the default value of null and never
assigned any other value, hence the NPE.

--
Lew


From: Yogi_Bear_79 Date:   Thursday, July 26, 2007
wrote in message:
I am having a problem with my code below. It compiles, however whenever I
click the Countbutton I get a slew of output information in my command
window. It starts with: Exception in thread "AWT-EventQueue-0"
JAVA.lang.NullPointerException at
WordCunter.actionPerformed{WordCounter.JAVA:43)

I think I have the program pretty much correct, I am stuck on getting the
Except for the NPE, caused by the fact that you never set the "textPane"
member variable to a non-null value.
results (words counted) to appear in the JtextField after I press the
CountButton, Thanks for any help.
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);
This declaration masks the member variable.
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
The "textPane" variable was initialized to the default value of null and
never assigned any other value, hence the NPE.
--
Lew



Ok, I am lost. I thought I was reading the contents with this line of code
String s = textPane.getText();...any help in resolving this last sticking
point on this project'd be most appriciated. Thanks


From: Lew Date:   Friday, July 27, 2007
wrote in message:
public class WordCounter extends JFrame implements ActionListener
{
public JTextArea textPane;



Here you define a member variable textPane and it initializes to its default
value of null.

public JTextField fieldWordCount;
public WordCounter()
{


...
JTextArea textPane = new JTextArea(12,32);



This declaration masks the member variable.

textPane.setLineWrap(true);
JScrollPane scrollPane = new JScrollPane(textPane);
textPanel.add(scrollPane);
}//end WordCounter constructor



At this point the local variable "textPane" goes out of scope. The member
variable "textPane" is still set to null.

public void actionPerformed(ActionEvent e)
{
String s = textPane.getText(); // get text that was input from user



The "textPane" variable was initialized to the default value of null and never
assigned any other value, hence the NPE.

wrote in message:
Ok, I am lost. I thought I was reading the contents with this line of code
String s = textPane.getText();...any help in resolving this last sticking
point on this project'd be most appriciated. Thanks



You are not, because you cannot call getText() on a null variable. At this
point textPane is null. There is no way to "read... the contents" through a
null variable. You've to set the variable to point to an object first.

--
Lew



Next Message: NoClassDefFoundError; Exception in thread "main"


Blogs related to Help with Button action

Release Notes 3.1 Beta 2 (updated)
Bug, HHQ-820, java.lang.ClassNotFoundException: org.hyperic.hq.ui.action.resource.common.control.UpdatedStatusAction, Charles Lee, MuleSource, Resolved, FIXED, Apr 16, 2007, Jul 06, 2007 ...

Re: Maven2 release process in Continuum
ReleasePrepareAction.processProject(ReleasePrepareAction.java:277) > > > at org.apache.maven.continuum.web.action.ReleasePrepareAction.input(ReleasePrepareAction.java:119) > > > at sun.reflect.NativeMethodAccessorImpl.invoke0(Native ...

Visual Editor (Part 2)
Via JAXB, we generated Java objects so that we can traverse the elements and attributes of XML files that conform to the JavaHelp DTD. Today, we'll see how to do that in the IDE. We'll use a code template that I discussed a few days ago ...

RE: [Announce] Continuum 1.1-beta-1 is released
Any disclosure, copying, or distribution of this message, or the taking of any action based on it, is strictly prohibited. [vE1] > > > > > > > > -- Large Systems Suck: This rule is 100% transitive. If you build one, you suck" -- S. ...

RE: how to access the properties files from java script
Hi David! Thank you for giving this noce solution. It is working fine. But now the requirement is to make 'OK' and 'CANCEL' button multilingual. Is it possible to do like this..if yes pls help me.. With warm regards. ...

Re: Maven2 release process in Continuum
In fact you could just add that kinda support as a project action in continuum, add a button that pops out to a page that lets you resolve snapshot dependencies without having put your fingers in the xml directly. in my experience ...


Programming | Sports | Autos

copyright 2006
Valid XHTML 1.0 Transitional