//I am just practing java in vacation.So i tried to create JFrame with Events Handling
//But getting error .Also i want to know that when i click submit button the new frame //appear and the old frame must get disappear .PLS help

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SDlogin extends JFrame
{
private JButton abt,sub,clr,exit;
private JTextField log;
private JPasswordField pwd;
public SDlogin()
{
this.setTitle("EXAMPLE");

this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container contentPane=this.getContentPane();
abt=new JButton("About");
sub=new JButton("Submit");
clr=new JButton("Clear");
exit=new JButton("Exit");
Buttonevent dmg=new Buttonevent(log,pwd);
abt.addActionListener(dmg);
exit.addActionListener(dmg);
clr.addActionListener(dmg);
//log.addActionListener(dmg);                //ERROR
//pwd.addActionListener(dmg);                //ERROR
sub.addActionListener(dmg);

abt.setBounds(70,305,80,30);
sub.setBounds(260,210,80,30);
clr.setBounds(360,210,80,30);
exit.setBounds(470,305,80,30);
JPanel pan=new JPanel();
pan.setLayout(null);
pan.add(abt);
pan.add(sub);
pan.add(clr);
pan.add(exit);

log=new JTextField();
pwd=new JPasswordField();

JLabel usrLabel = new JLabel("Username:");
usrLabel.setBounds(180,120,70,30);
pan.add(usrLabel);
JLabel pwdLabel = new JLabel("Password:");
pwdLabel.setBounds(180,160,70,30);
pan.add(pwdLabel);
log.setBounds(255,120,190,25);
pwd.setBounds(255,160,190,25);
pan.add(log);
pan.add(pwd);

contentPane.add(pan);
}


public static void main(String [] args)
{
    SDlogin f = new SDlogin();
    f.setBounds(125,30,640,480);

    f.setVisible(true);
}
}


///////////////////////////////////////////Event handling Class////////////////////////

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Buttonevent implements ActionListener
{

private JTextField log;
private JPasswordField pwd;

public Buttonevent(JTextField log,JPasswordField pwd)
{

this.log=log;
this.pwd=pwd;

}
public void actionPerformed(ActionEvent e)
{
String str=e.getActionCommand();
if(str.equals("About"))
{
JOptionPane.showMessageDialog(null,
"This Program is Developed BY  AJAXX");
}
else if(str.equals("Clear"))
{
try{
System.out.println("saddsa");
log.setText("");
pwd.setText("");
}catch(Exception ef){System.out.println("aaa");}
}
else if(str.equals("Exit"))
{
System.exit(0);
}
else if(str.equals("Submit"))
{
new logged();
//setVisible(false);
}

}
}

///////////////////////////////////Another frame class////////////////////////

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class logged extends JFrame
{

JButton ret,logout,entry,neww;
JTextField txtfld=new JTextField(30);

public logged()
{
setTitle("EXAMPLE");
setBounds(200,100,500,360);
//this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container contentPane=this.getContentPane();
JLabel lab=new JLabel("EXAMPLE");
lab.setFont(new Font("Arial",Font.BOLD,28));
ret=new JButton("Retrieve");
logout=new JButton("Log Out");
neww=new JButton("New");
entry=new JButton("Enter Details");
ret.setBounds(140,150,90,30);
neww.setBounds(40,260,60,30);
entry.setBounds(245,150,120,30);
logout.setBounds(380,260,80,30);
JLabel endet=new JLabel("Enter Name:");
endet.setBounds(140,100,80,30);
txtfld.setBounds(230,100,120,30);
lab.setBounds(150,20,250,50);
JPanel pan=new JPanel();
pan.setLayout(null);
pan.add(lab);
pan.add(ret);
pan.add(logout);
pan.add(neww);
pan.add(entry);
pan.add(logout);
pan.add(endet);
pan.add(txtfld);
contentPane.add(pan);

public static void main(String args[])
{
logged lg=new logged();
lg.setBounds(200,100,500,360);
lg.setVisible(true);
}
}
*/
}

Where does the NullPointerException occur?
Look at that line in the source and see what variable could be null. Then figure out why that variable is null.

Sir Thanks for your reply.

According to your answer the two textfield log,pwd i used shows error.
According my program when i click the Clear button it should clear the TextField log & pwd.So why it is showing nullpointer error as these textfield will be null initially.

Pls help me.

why it is showing nullpointer error as these textfield will be null

That is exactly why you get the NPE, the variables are null. You need to give them values.

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.