Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Trying to retrieve info. from JTextField
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
RichardP




PostPosted: Thu May 17, 2007 11:05 pm   Post subject: Trying to retrieve info. from JTextField

Bonjour,
I am writing a small applet which converts grades into percentages. I am having a problem with actually retrieving or writing something from a JTextField. Everything compiles fine, but when I click a button which calls a method to retrieve the data, my applet crashes and spits out a NullPointerException error. I am writing the whole applet in one file, with the GUI defined in my init method with my "working" methods being called through actionPerformed.

Any help would be appreciated. I am new to Java (obviously).

Here is a sample of my code:
// Packages de noyau Java.
import java.awt.*;
import java.awt.event.*;

// Packages d'extension Java.
import javax.swing.*;

public class CalculeNotesApp extends JApplet implements ActionListener {
...
GUI vbles defined here
...
public void init () {

Container conteneur = getContentPane();
conteneur.setLayout( new GridLayout(5,1,10,10) );
...stuff...
// I have 5 JPanels which each contain JTextFields, JLabels and / or buttons
JPanel ranger2 = new JPanel();
JLabel labelPonderanceCC = new JLabel("CC", JLabel.RIGHT);
JTextField valeurPonderanceCC = new JTextField(5);
ranger2.add(labelPonderanceCC);
ranger2.add(valeurPonderanceCC);
conteneur.add(ranger2);
...
}

After going through actionPerformed, I call the following method:
public void verifieLaPonderance () {

System.out.println ("in verifieLaPonderance");
String tmp = valeurPonderanceCC.getText ();
...
}

Everything compiles fine, but when I click the appropriate button and call
the method verifieLaPonderance I get the following dump:
java.lang.NullPointerException
at CalculeNotesApp.verifieLaPonderance (CalculeNotesApp.java:327)
...

1 - What does NullPointerException mean here?
2 - How do I access the JTextField? I have tried dereferencing ranger2 and conteneur to no avail.
(ranger2.valeurPonderanceCC.getText(); -> does not compile)

Again, thanks for any help you can offer,

RichardP
Sponsor
Sponsor
Sponsor
sponsor
Luny




PostPosted: Thu May 17, 2007 11:45 pm   Post subject: RE:Trying to retrieve info. from JTextField

NullPointerException means an object has been declared but not initialized. Can you post all of your code?

EDIT: I see the problem. You declared and initialized your JTextField in the init() method which created a local variable and after that method ends, there is no way to access that object again. Declare your JTextField object outside your methods so it becomes an instance variable. Thats when all method have access to that object. It should work then!
RichardP




PostPosted: Sun May 20, 2007 6:41 pm   Post subject: RE:Trying to retrieve info. from JTextField

Thanks Luny. That was was I needed! I forgto that I was effectively declaring a local variable... 8-{

Have a nice day.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: