Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

why cant i get text from a textfield? 3

Status
Not open for further replies.

monkey85

Programmer
Mar 31, 2005
7
0
0
GB
I want to get text from a JTextField when a button is pressed, however at the moment I'm getting a huge error message from Java at the command prompt when i press the button.
The code is below
Code:
    private void newPage()
    {
	page = fieldad.getText();
        fieldad.setEnabled(false);
    }

    private void Limit()
    {
		limitHist = Integer.parseInt ( fieldlim.getText().trim() );
		fieldlim.setEnabled(false);
	}

Both of the above methods are called by buttons using action listeners.

Thanks
 
The error displayed is

Exception occurred during event dispatching:
java.lang.NullPointerException
at ChangeHome.newHome(ChangeHome.java:219)
at ChangeHome.access$100(ChangeHome.java:7)
at ChangeHome$2.actionPerformed(ChangeHome.java:125)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:14
50)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(Abstra
ctButton.java:1504)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel
.java:378)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:250
)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonL
istener.java:216)
at java.awt.Component.processMouseEvent(Component.java:3715)
at java.awt.Component.processEvent(Component.java:3544)
at java.awt.Container.processEvent(Container.java:1164)
at java.awt.Component.dispatchEventImpl(Component.java:2593)
at java.awt.Container.dispatchEventImpl(Container.java:1213)
at java.awt.Component.dispatchEvent(Component.java:2497)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:2451
)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:2216)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:2125)
at java.awt.Container.dispatchEventImpl(Container.java:1200)
at java.awt.Window.dispatchEventImpl(Window.java:926)
at java.awt.Component.dispatchEvent(Component.java:2497)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:339)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchTh
read.java:131)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:98)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:85)

Thanks
 
What's at line 219 in ChangeHome.java?
 
219 is page = fieldad.getText() from the section of code below

Code:
private void newPage()
    {
    page = fieldad.getText();
        fieldad.setEnabled(false);
    }

    private void Limit()
    {
        limitHist = Integer.parseInt ( fieldlim.getText().trim() );
        fieldlim.setEnabled(false);
    }
 
Well, you have instantiated 'fieldad' before this line is executed, yes? Something like
Code:
JTextField fieldad = new JTextField();
 
You may have to post the file, or at least the bits of it which relate to the fieldad component. It's not possible that you set it to null at some point after it's created is it?
 
the only place that JTextField is used is in the following code
Code:
public void createHome()
    {
        homep = new JPanel();
        homep.setLayout( null);

        JLabel infoH = new JLabel("Test");
        infoH.setBounds(30, 10, 375, 20);
        homep.add(infoH);

        JLabel home1 = new JLabel( "Current:" );
        home1.setBounds( 30, 35, 150, 20 );
        homep.add( home1 );

        JTextField fieldad = new JTextField();
        fieldad.setBounds( 30, 55, 375, 20 );
        homep.add( fieldad );

        JButton changeA = new JButton("Change");
        changeA.setBounds(30, 80, 100, 20);
        homep.add(changeA);
        changeA.addActionListener( new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                newHome();
            }
        });

        JLabel previous = new JLabel( "Previously Used:" );
        previous.setBounds( 30, 130, 150, 20 );
        homep.add( previous );

        JButton clear = new JButton("Clear");
        clear.setBounds(330, 130, 75, 20);
        homep.add( clear );

        JTextArea previousAd = new JTextArea();
        previousAd.setBounds( 30, 160, 375, 160);
        previousAd.setEditable(false);
        homep.add(previousAd);
    }

Thanks for your help btw
 
... and how does newHome() end up calling newPage()?

Are you using an IDE btw? Could you animate the code and stick a break-point on line 219?
 
You're instantiating fieldad within createHome, so it's a local object. You must have another declaration of fieldad at the global level, otherwise you'ld get a compiler error in newPage().

If you change

JTextField fieldad = new JTextField();

to

fieldad = new JTextField();

it will probably work.
 
B**ger .. if I wasn't in so much of a hurry I'd have spotted that. That is indeed the problem.
 
... in fact, idarke, I'll give you a star for preventing me from flapping on for several more replies :)
 
Yeah it working now!! I should have seen that lol Thank you all
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top