Kaoru
Kaoru

Reputation: 2883

ResetField in Java

i am getting error while i pressed button reset in java. Update: I already create a JTextField and JPasswordField which is textField1 and passwordField1 in LoginForm.java in the design view, the thing is i want access both of it in the ResetField.java .

here is my code:

public class ResetField 
{
    JTextField textField1;
    JPasswordField passwordField1;

    public void ResetAction()
    {
        textField1.setText("");
        passwordField1.setText("");
    }
}

private void button2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        _resetField.ResetAction();
    }

Here is the error text:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at inspection.management.system.ResetField.ResetAction(ResetField.java:17) at inspection.management.system.LoginForm.button2ActionPerformed(LoginForm.java:170) at inspection.management.system.LoginForm.access$100(LoginForm.java:10) at inspection.management.system.LoginForm$2.actionPerformed(LoginForm.java:73) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6505) at javax.swing.JComponent.processMouseEvent(JComponent.java:3320) at java.awt.Component.processEvent(Component.java:6270) at java.awt.Container.processEvent(Container.java:2229) at java.awt.Component.dispatchEventImpl(Component.java:4861) at java.awt.Container.dispatchEventImpl(Container.java:2287) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) at java.awt.Container.dispatchEventImpl(Container.java:2273) at java.awt.Window.dispatchEventImpl(Window.java:2719) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735) at java.awt.EventQueue.access$200(EventQueue.java:103) at java.awt.EventQueue$3.run(EventQueue.java:694) at java.awt.EventQueue$3.run(EventQueue.java:692) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) at java.awt.EventQueue$4.run(EventQueue.java:708) at java.awt.EventQueue$4.run(EventQueue.java:706) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:705) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

Upvotes: 0

Views: 303

Answers (4)

Dodd10x
Dodd10x

Reputation: 3349

You don't understand how variables work. textField1 and passwordField1 are declared inside your class. That means they can only be accessed from inside your class. Even if they were public you would still need an instance of the containing class to reference them.

You created textField1 and passwordField1 in more than one class but that does not make them the same. If you want to be able to reference the exact JTextField and JPasswordField in your GUI from another class, you must pass a reference to it.

public class ResetField 
{
    JTextField textField1;
    JPasswordField passwordField1;

    public void ResetAction(JTextFile textField, JPasswordFiled passwordField)
    {
        textField1 = textField;
        passwordField1 = passwordField;
        textField1.setText("");
        passwordField1.setText("");
    }
}

That said, you do not need a new class in order to reset those fields in the constructor. You should instead create a method in the class that contains your fields and reset them there.

Upvotes: 0

user2948112
user2948112

Reputation:

you are just creating a reference but not creating an object.passwordField1 is a reference not an object.Similarly textField1 is a reference not an object. create an object like this JTextField textField1=new JTextField();

public class ResetField { JTextField textField1; JPasswordField passwordField1;

public void ResetAction()
{
    textField1=new JTextField();
     passwordField1=new JPasswordField();
    textField1.setText("");
    passwordField1.setText("");
}
}

private void button2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        _resetField.ResetAction();
    }

Upvotes: 1

gla3dr
gla3dr

Reputation: 2309

You have to instantiate your objects. Right now, textField1 and passwordField1 are just null references. Instead of this:

JTextField textField1;
JPasswordField passwordField1;

You need this:

JTextField textField1 = new JTextField();
JPasswordField passwordField1 = new JPasswordField();

Upvotes: 1

Masudul
Masudul

Reputation: 21971

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at inspection.management.system.ResetField.ResetAction(ResetField.java:17)

Why you used _resetField with resetAction

 private void button2ActionPerformed(java.awt.event.ActionEvent evt) {
    ResetAction();// Remove _resetField.
 }

Also you need to initiate following fields.

JTextField textField1=new JTextField();
JPasswordField passwordField1=new JPasswordField();

Upvotes: 0

Related Questions