Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: exception in "main" java.lang.NullPointerException

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Unhappy 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


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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 ==.

  3. The Following User Says Thank You to javapenguin For This Useful Post:

    overdriveboy (May 11th, 2012)

  4. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,706
    Thanks
    25
    Thanked 338 Times in 313 Posts

    Default Re: exception in "main" java.lang.NullPointerException

    javapenguin, the line:
    if(e.getSource()==b1)
    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).
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. The Following 2 Users Say Thank You to aussiemcgr For This Useful Post:

    javapenguin (May 11th, 2012), overdriveboy (May 11th, 2012)

  6. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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.
    Last edited by javapenguin; May 11th, 2012 at 02:37 PM.

  7. The Following User Says Thank You to javapenguin For This Useful Post:

    overdriveboy (May 11th, 2012)

Similar Threads

  1. Exception in thread "main" java.lang.NullPointerException
    By StratMaster007 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 26th, 2012, 01:07 PM
  2. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  3. Replies: 1
    Last Post: October 31st, 2011, 07:19 AM
  4. Exception in thread "main" java.lang.NullPointerException
    By isun in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 28th, 2011, 10:22 AM
  5. Replies: 16
    Last Post: August 27th, 2010, 04:30 PM

Tags for this Thread