exception in "main" java.lang.NullPointerException
Error:
Exception in thread "main" java.lang.NullPointerException
at adminlogin.launchframe(adminlogin.java:30)
at adminlogin.main(adminlogin.java:68)
my code is:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class adminlogin implements ActionListener//,FocusListener
{
private JFrame f;
private JLabel l1,l2,l3;
private JTextField tf1;
private JPasswordField pf1;
private JButton b1;
public adminlogin()
{
l1=new JLabel("Admin Login");
l2=new JLabel("Enter Username");
l3=new JLabel("Enter Password");
tf1=new JTextField();
pf1=new JPasswordField();
b1=new JButton("Login");
}
public void launchframe()
{
f.setLayout(null);
f.add(l1);
f.add(l2);
//f.add(l3);
f.add(tf1);
//f.add(pf1);
//f.add(b1);
l1.setBounds(600,200,100,25);
l2.setBounds(500,250,150,25);
tf1.setBounds(600,250,150,25);
f.setSize(2000,2000);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
/*
if(e.getSource()==b1)
{
staffdata sd=new staffdata();
sd.launchframe();
f.setVisible(false);
}*/
}
/*public void focusLost(FocusEvent fe)
{}
public void focusGained(FocusEvent fe1)
{}*/
public static void main(String args[])
{
adminlogin ac =new adminlogin();
ac.launchframe();
}
}
pls help me to solve this problem!!!!
urgent!!!
thanks in advance:)
Re: exception in "main" java.lang.NullPointerException
You never initialized the variable f. That's why it's throwing that exception.
Also, when comparing objects like JButtons or Strings, use .equals(Object obj) method instead of ==.
Re: exception in "main" java.lang.NullPointerException
javapenguin, the line: is valid since == on objects compares their memory locations (making sure the two objects are the exact same object), which is most likely what the OP wants to do.
The .equals() method compares whether the objects contain the same information, while == compares if they are actually the same object (meaning changing the values in one object will change values in the other).
Re: exception in "main" java.lang.NullPointerException
Sorry. I knew that == compares memory locations, but I guess that == would be ok in this case.
However, the variable f isn't initialized so it's throwing the Null Pointer Exception on the first line when you try to use the uninitialized variable to call a method.
Normally when people use it the wrong way, it's with Strings.