Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Please Help me with this Java Program . It's based on Event Handling .?

/*

* This program creates a basic user login form using AWT package of Java on an Applet

*/

import java.applet.Applet;

import java.awt.*;

import java.awt.event.*;

public class LoginForm extends Applet

{

//Creating Component References

Label l1,l2;

TextField t1,t2;

Button b1,b2;

public void init() {

setLayout(new GridLayout(3,2,50,50));

l1=new Label("Enter UserName : ");

l2=new Label("Enter Password : ");

t1=new TextField(20);

t2=new TextField(20);

b1=new Button("Login");

b2=new Button("Clear");

//Adding the components

add(l1);

add(t1);

add(l2);

add(t2);

add(b1);

add(b2);

b1.addActionListener(new MyActionListener()); //Register the Source

b2.addActionListener(new MyActionListener()); //Register the Source

}

}

//Class Implementing ActionListener Interface

class MyActionListener extends LoginForm implements ActionListener

{

//implementing the actionPerformed method

public void actionPerformed(ActionEvent ae)

{

try

{

if(ae.getSource()==b1) //finding the correct source of the event

{

String uname,dname,dpwd,upwd;

dname="Bhushan";

dpwd="JavaProg";

uname=t1.getText();

upwd=t2.getText();

if(dname.equalsIgnoreCase(uname)&&dpwd.equals(upwd))

{

showStatus("Valid Login"); //setting message on status bar

}

else

{

showStatus("Invalid Login"); //setting message on status bar

}

}

if(ae.getSource()==b2) //finding the correct source of the event

{

t1.setText("");

t2.setText("");

}

}

catch(NullPointerException npe){}

}

}

I get a NullPointerException if i do not use a try-catch block . If i use a try-catch block as in the program , then i do not get any action on clicking the Login or Clear button . However if i implement the ActionPerformed method in the class LoginForm , then it works properly .

Please tell me whats the error , and if possible kindly explain the error . Thank You .

2 Answers

Relevance
  • Anonymous
    1 decade ago
    Favorite Answer

    Your action listener shouldn't be extending LoginForm. The problem is that when you reference b1, t1, etc., in your listener, you're referencing the listener's own (uninitialized) copies of these fields (because it *is* a LoginForm itself). So you have two LoginForms - one that is initialized properly, and one that is also an ActionListener, and not initialized.

    What you need to do is remove the "extends LoginForm" from your action listener class definition. If you need access to those widgets in your listener, you can provide access to them through the listener's constructor. That way, the listener has references to the same widgets created by the LoginForm and will not generate an NPE when it tries to access them.

  • 1 decade ago

    The MyActionListener class should probably be an inner class of LoginForm rather than extending it. Your NullPointerException is probably occuring when the parent class default constructor is called. Remember that Applets are initialized by Java calling the default constructor then the init method. When you extend

    LoginForm this results in only the default constructor being called and the init method being bypassed.

    A better plan would be for the LoginForm class to implement ActionListener and forget about the MyActionListener class altogether.

Still have questions? Get your answers by asking now.