please help me in finding the proble in the following code

import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;

class button extends JButton
{
    public button(String s)
    {
        setName(s);
        setVisible(true);

    }
}

class label extends JLabel
{
    public label (String s)
    {
        setText(s);
        setVisible(true);
    } 


}


class loginform extends JFrame implements ActionListener , TextListener
{
    public Container con;
    private TextField nam;
    private JLabel usr,pass;
    private JPasswordField pas;
    private Button ok,cancel;
    public loginform(String s)
    {
        super(s);
        addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });
        Container con=getContentPane();
        setResizable(false);
        usr=new label("Employee Id");
        pass=new label("Password"); 
        con.add(pass);
        con.add(usr);
        TextField nam=new TextField();
        JPasswordField pas=new JPasswordField();
        con.add(nam);
        con.add(pas);
        nam.setEditable(true);
        pas.setEditable(true);
        usr.setBounds(50,50,100,25);
        pass.setBounds(50,100,75,25);
        nam.setBounds(150,50,150,25);
        pas.setBounds(150,100,100,25);
        ok=new Button("Ok");
        cancel=new Button("Cancel");
        con.add(ok);
        con.add(cancel);
        ok.setBounds(100, 150, 50, 25);
        cancel.setBounds(175, 150,75, 25);
        JLabel j=new JLabel("Shashank");
        con.add(j);
        ok.addActionListener(this);
        cancel.addActionListener(this);
        nam.addTextListener(this);


    }

    public static void main(String args[])
    {
        JFrame f=new loginform("Login Form");   
        f.setVisible(true);
        f.setBounds(300,200,400,300);
        f.setLayout(null);

    }

    public void actionPerformed (ActionEvent e)
    {
        Object ob=e.getSource();
        if(ob==ok)
        {

            String u,p,us,ps;

            u="shashank";
            p="khede";
            //us=nam.getText();
            //ps=pas.getPassword();
            if(u.equals(nam.getText()))
            {   if(p.equals(pas.getPassword()))
            {
                menuform f=new menuform("Main Form");
            }
            }
            //menuform f=new menuform("Main Form");
            //setVisible(false);
        }
        if(ob==cancel)
        {
            /*try
            {
                nam.setText(null);
                pas.setText(null);
            }
            catch(NullPointerException ex)
            {
                nam.setText("");
                pas.setText("");
            }*/
            System.exit(0);

        }

    }

    public void textValueChanged(TextEvent e) 
    {


    }
}


the error is
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at loginform.actionPerformed(loginform.java:99)
    at java.awt.Button.processActionEvent(Unknown Source)
    at java.awt.Button.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

Recommended Answers

All 4 Replies

The stack trace clearly indicates that in your actionPerformed method on line 99 that you are attempting to call a method on an object that is null. Examine the call that is being made on line 99 and examine why it might be null.

Sir,
I cannot understand that how the object is null ..

can u please improve the code or tell me an alternative for the same.

"nam" and "pas" are both still null at the class level because you re-defined these variables locally in your constructor when you added them to the panel.

TextField nam = new TextField();
        JPasswordField pas = new JPasswordField();

This should instead initialize your instance-level variables

nam = new TextField();
        pas = new JPasswordField();

Thank You...
Very Much Sir
Can you please tell me how can we see the stack run as we can see in C by pressing F7..
please give me your mail id
.......!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.