Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / November 2003

Tip: Looking for answers? Try searching our database.

Ahhhh....

Thread view: 
Connell Gauld - 29 Nov 2003 20:27 GMT
Hi,

I have a very annoying problem. (I'm running latest version of Sun's J2SDK)
I have a class which extends a Frame. On the frame I have several TextField
objects. All are defined as protected in the class. I can use the getText()
function of these TextField objects without any problems in the constructor
function (where they are added to the Frame). I have written the class with
an action(Event evt, Object whichAction) method so that it will react when
a Button is pressed. I get the following error when I use getText() in this
method (note qlsettings is the name of the class which extends the Frame):

java.lang.NullPointerException
       at qlsettings.outer(qlsettings.java:66)
       at qlsettings.action(qlsettings.java:58)
       at java.awt.Component.handleEvent(Component.java:5341)
       at java.awt.Window.postEvent(Window.java:1614)
       at java.awt.Component.postEvent(Component.java:3895)
       at java.awt.Component.dispatchEventImpl(Component.java:3631)
       at java.awt.Component.dispatchEvent(Component.java:3477)
       at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
       at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
       at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
       at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
       at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
       at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)

Help please?

Thanks
Connell
Christophe Vanfleteren - 29 Nov 2003 22:09 GMT
> Hi,
>
[quoted text clipped - 17 lines]
>         at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
>         at

java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
>         at

java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
>         at
> java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
[quoted text clipped - 6 lines]
> Thanks
> Connell

You'll need to show your code.
Show us what you do on line 66 in qlsettings, and show where you initialize
the objects you use on that line.

Signature

Regards,
Christophe Vanfleteren

Connell Gauld - 29 Nov 2003 22:25 GMT
I removed some unimportant stuff from below but line 66 was:

String str=new String(txtDatabaseAddress.getText());

The rest of the code is below:

import java.awt.*;
import java.io.*;
import javax.swing.*;

public class qlsettings extends Frame {
       protected Button cmdSave;
       protected Button cmdCancel;
       protected TextField txtServerAddress;
       protected TextField txtServerPassword;
       private JTextField txtServerPort;
       protected TextField txtDatabaseAddress;
       qi uParent;

       public qlsettings(qi parent, Frame mth){
               uParent=parent;
               setResizable(false);
               setBackground(new Color(223,223,223));
               setTitle("Settings");
               setLayout(new GridLayout(5,2,3,3));
               add(new Label("Server Address"));

               TextField txtServerAddress=new TextField(uParent.qlserveraddress);
               add(txtServerAddress);

               add(new Label("Server Port"));

               JTextField txtServerPort=new JTextField(uParent.qlserverport);
               add(txtServerPort);

               add(new Label("Server Password"));

               TextField txtServerPassword=new TextField(uParent.qlserverpassword);
               txtServerPassword.setEchoChar('*');
               add(txtServerPassword);

               add(new Label("Database Address"));

               TextField txtDatabaseAddress=new TextField(uParent.dbserveraddress);
               add(txtDatabaseAddress);
               cmdSave=new Button("Save");
               cmdCancel=new Button("Cancel");
               add(cmdSave);
               add(cmdCancel);
               pack();
               Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
               setLocation((screen.width-getWidth())/2,(screen.height-getHeight())/2);
               show();
               System.out.println(txtDatabaseAddress.getText());
       }
       public boolean action(Event evt, Object whichAction){
               if (evt.target==cmdSave){
                       outer();
               }
               if (evt.target==cmdCancel){
                       hide();
               }
               return true;
       }
       public void outer(){
               String str=new String(txtDatabaseAddress.getText());
       }
}
Christophe Vanfleteren - 30 Nov 2003 02:02 GMT
> I removed some unimportant stuff from below but line 66 was:
>
> String str=new String(txtDatabaseAddress.getText());

Actually, you don't need to use the String constructor for that (you almost
never need it).
Just typing "String str = txtDatabaseAddress.getText()" will do.

> The rest of the code is below:
>
[quoted text clipped - 21 lines]
>                 TextField txtServerAddress=new
>                 TextField(uParent.qlserveraddress); add(txtServerAddress);
These kind of lines are the problem:

You're defining a new variable of type TextField with the same name as one of
your instance variables. So in this scope (the scope of the constructor),
txtServerAddress is not the same variable as the txtServerAddress of your
class. Because of this, your "normal", instance variable txtServerAddress
never gets a value, and hence will throw nullpointerexceptions if you try to
dereference it.

Just leave the declaration of the type away and type 'txtServerAddress = ..."
instead.

The compiler should at least issue warnings in cases like these IMO.

Signature

Regards,
Christophe Vanfleteren

Connell Gauld - 30 Nov 2003 11:55 GMT
Worked great, thanks!

>> I removed some unimportant stuff from below but line 66 was:
>>
[quoted text clipped - 43 lines]
>
> The compiler should at least issue warnings in cases like these IMO.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.