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 6 of 6

Thread: Need help with Exception in thread "main" java.lang.NullPointerException

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with Exception in thread "main" java.lang.NullPointerException

    Hello everybody im new to the board. I recently started tring to learn Java programing. I have a problem with my code giving me this error when I try to run it can anyone help me out.This is the exception that I am getting.

    Exception in thread "main" java.lang.NullPointerException
    at calc.Calc.main(Calc.java:57)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 1 second)


    Thanks in advance.
    package calc;
     
    /**
     *
     * @author
     */
     
       import javax.swing.*;
       import javax.swing.event.*;
       import java.awt.*;
       import java.awt.event.*;
     
    public class Calc  extends JFrame {
            public static JPanel panel;
            public static JFrame frame;
            public static JTextField input;
            public static JTextField output;
            public static JButton calculate;
            public static JButton clear;
            public static JButton quit;
     
            public Calc(){
            setLayout(new FlowLayout ());
            calculate = new JButton("Calculate");
            clear = new JButton("Clear");
            quit = new JButton("Quit");
            JFrame frame = new JFrame();
            JPanel panel = new JPanel();
            frame.add(panel);
            panel.add(output);
            frame.setVisible(true);
            panel.setVisible(true);
            quitevent quitevent = new quitevent();
            clearevent clearevent = new clearevent();
            ooop ooop = new ooop();
     
            quit.addActionListener(quitevent);
            clear.addActionListener(clearevent);
            calculate.addActionListener(ooop);
     
    }
    public class quitevent implements ActionListener {
        public void actionPerformed(ActionEvent quitevent){
     
        }
     
    }
    public class clearevent implements ActionListener{
        public void actionPerformed(ActionEvent clearevent){
    }
    }
    public class ooop implements ActionListener{
        public void actionPerformed(ActionEvent ooop){
    }}
        public static void main(String args[]){
     
           frame.add(panel);
    }       
    }
    Last edited by JGeorge337; October 26th, 2011 at 08:30 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Need help with Exception in thread "main" java.lang.NullPointerException

    For future reference, please wrap your code in the code tags (see the getting help link in my signature for more information). I have done this for you.

    Please post the full exception - it contains valuable information indicating where the exception is being thrown

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with Exception in thread "main" java.lang.NullPointerException

    Thanks copeg!!

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Need help with Exception in thread "main" java.lang.NullPointerException

    You are welcome.

    A comment about the code. First, there is no need to declare the variables of Calc static - they should be instance variables and part of the Calc class. See the following for a better description than I can provide at the moment: Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    Regarding the Exception, it indicates something is Null, or has not been instantiated (eg using the new keyword). Glance through the code, use println's, or do whatever you are more comfortable with to find out what has not been instantiated (that's all I can output at the moment, and I've said the main point - hint, hint). For what its worth, to avoid this, I often instantiate variables that do not depend on runtime values when they are declared (eg private JPanel myPanel = new JPanel() ).
    Last edited by copeg; October 27th, 2011 at 08:47 AM.

  5. #5
    Member
    Join Date
    Oct 2011
    Posts
    50
    My Mood
    Fine
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Need help with Exception in thread "main" java.lang.NullPointerException

    The exception is thrown at this line: frame.add(panel);

    Remove the JFrame and JPanel from the constructor. Basically what you do there, is to redeclare them locally, and the actual frame/panel which you intended to use, are never initialized.

    application context
    Last edited by daniel.j2ee; December 13th, 2011 at 05:04 PM.

  6. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Need help with Exception in thread "main" java.lang.NullPointerException

    Read copeg's post again and carefully. You will find answer.

Similar Threads

  1. Exception in thread "main" java.lang.NullPointerException
    By manzili in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 9th, 2011, 12:02 PM
  2. Replies: 7
    Last Post: May 30th, 2011, 09:11 AM
  3. Exception in thread "main" java.lang.NullPointerException
    By isun in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 28th, 2011, 09:22 AM
  4. Exception in thread "main" java.lang.NullPointerException
    By MryJaho in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 4th, 2011, 05:36 PM
  5. Please help! Exception in thread "main" java.lang.NullPointerException
    By Arutha2321 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 18th, 2009, 02:25 AM