-
Multi event listeners
Hi everyone,
I have a bit of a problem. I am trying to use the multi event listeners but keeping getting this error:
cannot find symbol
symbol: class EavesDropper
location: class aloeandcycaddatabasegui.AloeAndCycadDatabaseLogin
--
(Alt-Enter shows hints)
The first action listener works 100% but I am battling with this one.
Here is some of the code that I have:
// Adding the action listener to the button
loginBtn.addActionListener(this);
loginBtn.addActionListener(new EavesDropper(loginBtn));
// The Eavesdropper class
class Eavesdropper implements ActionListener {
JButton loginBtn;
public Eavesdropper(JButton lb) {
loginBtn = lb;
}
public void actionPerformed(ActionEvent e) {
}
}
I have also imported these packages:
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
Any help is greatly appreciated
-
-
Eavesdropper != EavesDropper.
-
Thanks Kevin, that was really dumb on my part!
-
1 Attachment(s)
Hi guys,
I am getting a null pointer exception when I try and use the login button. Is there a way to fix this? I have attached the file so you guys can see what I am doing. I think the problem is in the EavesDropper class but I don't know how to fix it.
Any help is appreciated.
Thanks in advance,
Wesley
-
For future reference please try not to post full projects... make a small, simple, compileable and correct program which contains as little as neccesary to reproduce the error.
Also, when you describe the errors please copy the full stack trace and the offending lines of code.
As is stands, i cannot run your code straight away, as you didnt include the picture file.
I managed to get round that by not making the picture, and this is the second null pointer i came accross: it might not be the one you are experiencing - im just guessing :P
anyway, the stack is:
Code:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at aloeandcycaddatabasegui.AloeAndCycadDatabaseLogin.actionPerformed(AloeAndCycadDatabaseLogin.java:236)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6267)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6032)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
The offending code is
Code:
String firstname = fNameTF.getText();
Make sure fNameTF.getText() is not null (hence null pointer exception).
Your actionPeformed seems to only deal with the situation where the user is not registered. You need to account for when they are logging in, not registering.
-
Thank your for the reply. i will make sure to do that next time. That is the same error that I am getting. Can you please tell me how I can rectify it? I dont know how it points to the firstname as I told it to pint to only the username and password fields in the login screen.
-
Its difficult to say exactly how as i dont fully understand your code.
As far as i can tell, you have two "screens" - one for logging in and one for registering.
In your action listener you could put something like
Code:
if (registering) {
// deal with registering the user
String firstname = fNameTF.getText();
String lastname = lNameTF.getText();
String age = ageTF.getText();
String country = countryTF.getText();
String username = usernameTF.getText();
String password = new String(passwordTF.getPassword());
// etc
} else {
// deal with logging in
}
where the boolean value "registering" is defined when the user clicks the "yes" or "no" button when asked if they are registered.
-
I know exactly what you mean, but dont know how to code it. Could you help me please by showing me what to do more or less?
-
berkeleybross has given you plenty to work with.
Aren't you going to even give it a go?
-
Thanks guys, I got it to work. I have just started out with Java so I am not too familiar with everything but I managed to decipher everything :)
-
Well done,
glad you worked it out yourself in the end ^^