7 Replies - 4151 Views - Last Post: 20 April 2009 - 05:23 PM Rate Topic: -----

#1 drewlll2ll  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-April 09

getText Method Returning null When Text is Present

Posted 19 April 2009 - 03:58 PM

Hey everyone,
I just have a slight problem involving a null value returned when I call the getText method of a textField, even when there is text in the field.

Here is the code to create the GUI
  public static void createGUI()
  {
	  JFrame frame = new JFrame("Chatter");
	  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	  
	  JPanel panel = new JPanel(new BorderLayout());
	  panel.add(new ChatterGui(), BorderLayout.CENTER);
	  panel.add(new JPanel(), BorderLayout.NORTH);
	  panel.add(new JPanel(), BorderLayout.SOUTH);
	  panel.add(new JPanel(), BorderLayout.EAST);
	  panel.add(new JPanel(), BorderLayout.WEST);
	  
	  frame.add(panel);
	  
	  frame.pack();
	  frame.setVisible(true);
  }



Here is the constructor of the ChatterGui class
  public ChatterGui()
  {
	  super(new GridBagLayout());
	  JTextField chatInput = new JTextField(50);
	  chatInput.addActionListener(this);
	  chatInput.setFocusable(true);
	  
	  JTextArea chatList = new JTextArea(30, 50);
	  chatList.setEditable(false);
	  JScrollPane scrollPane = new JScrollPane(chatList);
	  
	  JButton submit = new JButton("Submit");
	  submit.addActionListener(this);
	  
	  GridBagConstraints c = new GridBagConstraints();
	  c.gridwidth = GridBagConstraints.REMAINDER;
	  
	  c.fill = GridBagConstraints.BOTH;
	  add(scrollPane, c);
	  c.weightx = 1.0;
	  c.weighty = 1.0;
	  
	  c.fill = GridBagConstraints.HORIZONTAL;
	  add(new JPanel(), c);
	  
	  c.fill = GridBagConstraints.HORIZONTAL;
	  add(chatInput, c);
	  
	  c.fill = GridBagConstraints.HORIZONTAL;
	  add(new JPanel(), c);
	  
	  c.fill = GridBagConstraints.HORIZONTAL;
	  add(submit, c);
  }



and here is the code for the actionListener for the JButton and the JTextField
  public void actionPerformed(ActionEvent evt)
  {
	  chatInput.selectAll();
	  String in = chatInput.getText();
	  Grammar.parseQuestion(in);
	  chatList.append("User: " + in + "\n");
	  chatList.append("Computer: " + Global.currentResponse + "\n");
	  Global.flushResponse();
	  chatInput.setText("");
  }



Any help would be appreciated, and sorry about the weird spacing, I wrote in editors with two different spacing schemes.

Is This A Good Question/Topic? 0
  • +

Replies To: getText Method Returning null When Text is Present

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: getText Method Returning null When Text is Present

Posted 19 April 2009 - 04:43 PM

Do you have a stack trace of the error? I dont do my ActionEvents like that so not sure if this is the problem. But you have an ActionEvent for both the JTextField and JButton, but you only have one actionPerformed method. How is it going to know what Action to react too? See if you get the text back if you press the submit button. Personally, i would provide listeners to all events like so
btnExit.setText("  EXIT  "); 
 		btnExit.addActionListener(new ActionListener() { 
 			public void actionPerformed(ActionEvent e) 
 			{ 
 				btnExit_actionPerformed(); 
 			} 
  
 		}); 


And then you can produce seperate methods for each event.
Was This Post Helpful? 0
  • +
  • -

#3 drewlll2ll  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-April 09

Re: getText Method Returning null When Text is Present

Posted 19 April 2009 - 04:54 PM

Here is the stack trace,
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at ChatterGui.actionPerformed(ChatterGui.java:61)
	at javax.swing.JTextField.fireActionPerformed(Unknown Source)
	at javax.swing.JTextField.postActionEvent(Unknown Source)
	at javax.swing.JTextField$NotifyAction.actionPerformed(Unknown Source)
	at javax.swing.SwingUtilities.notifyAction(Unknown Source)
	at javax.swing.JComponent.processKeyBinding(Unknown Source)
	at javax.swing.JComponent.processKeyBindings(Unknown Source)
	at javax.swing.JComponent.processKeyEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
	at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
	at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
	at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
	at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source)


The button returns the same trace.

I originally had it setup in the format you are suggesting, but, I figured that since both actionListeners were reacting with exactly the same code being executed that it would be easier to make a single listener triggered by both. For future reference, is it okay to do that, or should I go back to the original format?
Was This Post Helpful? 0
  • +
  • -

#4 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: getText Method Returning null When Text is Present

Posted 19 April 2009 - 05:04 PM

Is there any need then to have two listeners if they both do the same thing? Would make more sense just controlling it all through a JButton. Without seeing all your code, its difficult to tell what exactly is causing the error. Remoce some stuff you dont really need e.g.
chatInput.selectAll();

Not doing anything usefull. Under
String in = chatInput.getText();

Chuck a
System.out.println("TEST" + in);

Too see if this is the cause of the error. If it prints to your console whats in your textfield, then atleast we know the error is not here. Have a try at that for now.
Was This Post Helpful? 0
  • +
  • -

#5 drewlll2ll  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-April 09

Re: getText Method Returning null When Text is Present

Posted 19 April 2009 - 05:07 PM

I had added in the call to selectAll to see if it would be able to reference the field, but the error moved to that line, sorry for not mentioning it earlier. I had tried that when the error took place, it prints nothing, indicating a null value.
I can post the entirety of the file if you want.

This post has been edited by drewlll2ll: 19 April 2009 - 05:08 PM

Was This Post Helpful? 0
  • +
  • -

#6 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: getText Method Returning null When Text is Present

Posted 19 April 2009 - 05:09 PM

P.s
whats
Grammar.parseQuestion 


I dont see a grammar class in the api so i presume this is one of your classes or an external library? Or i just missed it in api.

K, post the code then i can check it out.

This post has been edited by nick2price: 19 April 2009 - 05:10 PM

Was This Post Helpful? 0
  • +
  • -

#7 drewlll2ll  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-April 09

Re: getText Method Returning null When Text is Present

Posted 19 April 2009 - 05:13 PM

Its one of the classes from my project, it handles parsing the input from the gui, and producing a response.

Here's the full code, I've been modifying it so its a bit different from what I was first posting from.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.*;

import java.awt.BorderLayout;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Point;
import java.awt.event.*;

public class ChatterGui
  extends JPanel implements ActionListener
{
  private JTextField chatInput;
  private JTextArea chatList;
  private JButton submit;
  
  public ChatterGui()
  {
	  super(new GridBagLayout());
	  JTextField chatInput = new JTextField(50);
	  chatInput.addActionListener(this);
	  chatInput.setFocusable(true);
	  
	  JTextArea chatList = new JTextArea(30, 50);
	  chatList.setEditable(false);
	  JScrollPane scrollPane = new JScrollPane(chatList);
	  
	  JButton submit = new JButton("Submit");
	  submit.addActionListener(this);
	  
	  GridBagConstraints c = new GridBagConstraints();
	  c.gridwidth = GridBagConstraints.REMAINDER;
	  
	  c.fill = GridBagConstraints.BOTH;
	  add(scrollPane, c);
	  c.weightx = 1.0;
	  c.weighty = 1.0;
	  
	  c.fill = GridBagConstraints.HORIZONTAL;
	  add(new JPanel(), c);
	  
	  c.fill = GridBagConstraints.HORIZONTAL;
	  add(chatInput, c);
	  
	  c.fill = GridBagConstraints.HORIZONTAL;
	  add(new JPanel(), c);
	  
	  c.fill = GridBagConstraints.HORIZONTAL;
	  add(submit, c);
  }
  
  public void actionPerformed(ActionEvent evt)
  {
	  chatInput.selectAll();
	  String in = chatInput.getText();
	  Grammar.parseQuestion(in);
	  chatList.append("User: " + in + "\n");
	  chatList.append("Computer: " + Global.currentResponse + "\n");
	  Global.flushResponse();
	  chatInput.setText("");
  }
  
  private static void initialize()
  {
	  boolean succeeded = false;
	  System.out.println("Initializing Files...");
	  System.out.print("   Checking for Settings:");
	  try
	  {
		  BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("settings.ini")));
		  Global.path = in.readLine();
		  in.close();
		  System.out.println(Global.spacing("   Checking for Settings:") + "[File Found]");
		  Global.importAll();
		  TraitPicker.selectTraits();
		  succeeded = true;
		  createGUI();
	  }
	  catch(IOException e)
	  {
		  System.out.println(Global.spacing("   Checking for Settings:") + "[File Not Found]");
		System.out.print("   Requesting Path:");
		  String path = JOptionPane.showInputDialog(null, "Path of Java Words Directory:", "",
				  JOptionPane.QUESTION_MESSAGE);
		  if(path == null)
		  {
			  Global.path = null;
			  System.out.println(Global.spacing("   Requesting Path:") + "[Failed]");
			  System.exit(0);
		  }
		  else
		  {
			  Global.path = path;
			  System.out.println(Global.spacing("   Requesting Path:") + "[Succeeded]");
			  succeeded = true;
		  }
		  System.out.print("   Setting Path:");
		  try
		  {
			  Global.setPath();
			  System.out.println(Global.spacing("   Setting Path:") + "[Succeeded]");
			  System.out.println("   Saving Path:");
			  try
			  {
				  if(!Global.path.equals(null))
				  {
					  BufferedWriter out = new BufferedWriter(new FileWriter("settings.ini"));
					  out.write(Global.path);
					  System.out.println(Global.spacing("   Saving Path:" + "[Succeeded]"));
					  out.close();
				  }
				  else
				  {
					  System.out.println(Global.spacing("   Saving Path:" + "[Failed]"));
					  System.exit(0);
				  }
					  
			  }
			  catch(IOException h)
			  {
				  System.out.println(Global.spacing("   Saving Path:" + "[Failed]"));
			  }
			  Global.importAll();
			  TraitPicker.selectTraits();
			  succeeded = true;
			  createGUI();
		  }
		  catch (IOException h)
		  {
			  System.out.println(Global.spacing("   Setting Path:") + "[Failed]");
			  succeeded = false;
		  }
	  }
	  if(succeeded == true)
		  JOptionPane.showMessageDialog(null, "File Location Set");
	  else
	  {
		  JOptionPane.showMessageDialog(null, "An Error Has Occured");
		  System.exit(0);
	  }
  }
  
  public static void createGUI()
  {
	  JFrame frame = new JFrame("Chatter");
	  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	  
	  JPanel panel = new JPanel(new BorderLayout());
	  panel.add(new ChatterGui(), BorderLayout.CENTER);
	  panel.add(new JPanel(), BorderLayout.NORTH);
	  panel.add(new JPanel(), BorderLayout.SOUTH);
	  panel.add(new JPanel(), BorderLayout.EAST);
	  panel.add(new JPanel(), BorderLayout.WEST);
	  
	  frame.add(panel);
	  
	  frame.pack();
	  frame.setVisible(true);
  }
  
  public static void main(String[] args)
  {
	  javax.swing.SwingUtilities.invokeLater(new Runnable() {
		  public void run() {
			  initialize(); 
		  }
	  });
  }
}


Was This Post Helpful? 0
  • +
  • -

#8 drewlll2ll  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-April 09

Re: getText Method Returning null When Text is Present

Posted 20 April 2009 - 05:23 PM

Problem solved, the declaration in the constructor was unnecessary, causing it to overwrite the global variable.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1