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

Thread: Newbie Problem With Null Pointer Exception

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    My Mood
    Inspired
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Newbie Problem With Null Pointer Exception

    Trying to build my first calculator from scratch and everything was going smooth but I am now getting hit with a handler coming up as a null pointer exception.

    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JButton;
    import javax.swing.ImageIcon;
    import javax.swing.Icon;
    import javax.swing.JTextArea;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Scanner;
     
    public class tuna extends JFrame {
    	private JTextArea Screen;
    	private JButton Num0;
    	private JButton Num1;
    	private JButton Num2;
    	private JButton Num3;
    	private JButton Num4;
    	private JButton Num5;
    	private JButton Num6;
    	private JButton Num7;
    	private JButton Num8;
    	private JButton Num9;
    	private ImageIcon INum0;
    	private ImageIcon INum1;
    	private ImageIcon INum2;
    	private ImageIcon INum3;
    	private ImageIcon INum4;
    	private ImageIcon INum5;
    	private ImageIcon INum6;
    	private ImageIcon INum7;
    	private ImageIcon INum8;
    	private ImageIcon INum9;
    	private int count =0;
    	double Fnum;
    	double Snum;
    	boolean[] op = {false,false,false,false};  // not in use yet
    	double Ans;
    	// this is initializing all of the variables
     
    	public tuna(){
    		super("RaycasterJr's Calculator");
    		setLayout(new FlowLayout());
    		Fnum = 0;
    		Snum = 0;
    		Screen = new JTextArea(10,10);
    		Screen.setEditable(false);
    		add(Screen);
    		JButton Nums[] = {Num0, Num1, Num2, Num3, Num4, Num5, Num6, Num7, Num8, Num9};
    		Icon[] Numicons = new Icon[10];
    		for(int count=0; count<=9;count++){ 
    			Numicons[count] = new ImageIcon(getClass().getResource(count +"Num.png"));
    			/// Setup the names of the images to co-ordinate with the count+"Num" system. This will allow for limited code.
    		}
    		for (int count = 0; count>=9; count++){
    				Nums[count] = new JButton(Numicons[count]);
    				add(Nums[count]);
    				/// This will create all of the buttons with their own images
    		}
     
     
    		[COLOR="#FF0000"][COLOR="#FFFF00"]handlerNum H = new handlerNum();
    		for (int count = 0; count<=9;count++){
    			Nums[count].addActionListener(H);
    			//This is where the error occurs
    			//this is the error code below:
    			//Exception in thread "main" java.lang.NullPointerException
    				//at tuna.<init>(tuna.java:66)
    				//at apples.main(apples.java:5)[/COLOR]
    [/COLOR]
    		}
    	}
    	public class handlerNum implements ActionListener{
    		public void actionPerformed(ActionEvent Event){
    			if (Event.getSource() == Num0){
    				JOptionPane.showMessageDialog(null,"Testing"+Event.getSource());
    			/// if a operator is click set that operators boolean to true and set the rest to false - this solves the multi-op problem
    			}
    		}
    	}
    }


    Any help would be appreciated as I'm loving java and trying the gui from scratch but... hit a wall.
    Again, thank you for any help.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Newbie Problem With Null Pointer Exception

    Quote Originally Posted by RaycasterJr View Post
    a null pointer exception
    Always post the full text of the exception on the forum with your code and question.
    Did you do a search on "Java null pointer exception"? Do you understand what that means? What trouble are you having in finding and fixing it?

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    My Mood
    Inspired
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Newbie Problem With Null Pointer Exception

    This is the error I get when I run the program.

    Exception in thread "main" java.lang.NullPointerException
    at tuna.<init>(tuna.java:64)
    at apples.main(apples.java:5)

    I think that it means that at line 64 the program is having trouble seeing what type one of my variables is.

    Nums[count].addActionListener(H);

    Above is line 64. So that would mean that it either can't check the type for H which is my action listener or the type of Nums[].
    I have declared both of these variables and was wondering why I get this Null Pointer Exception and how I can fix this.
    Thanks in advance, your help is appreciated.
    Last edited by RaycasterJr; November 21st, 2013 at 07:06 AM. Reason: Clarification

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Newbie Problem With Null Pointer Exception

    Variables have to be declared and initialized.

    Declared:

    MyActionClass hListener; // variable names begin with lowercase letters, and single-letter names are lousy

    Initialized:

    hListener = new MyActionClass();

    Declared and initialized:

    MyActionClass hListener = new MyActionClass();

    The array Nums (should be named "nums") must also have its elements initialized to something other than null or zero.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    RaycasterJr (November 21st, 2013)

  6. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    My Mood
    Inspired
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Newbie Problem With Null Pointer Exception

    Thanks for the help - This concept allowed me to fix the problem with the arrays. Problem Solved - Mission Success!

Similar Threads

  1. Null Pointer Exception. How to fix this problem?
    By carbLoading2012 in forum Exceptions
    Replies: 2
    Last Post: February 4th, 2013, 03:06 AM
  2. Re: null pointer exception problem
    By tbarbone in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 2nd, 2011, 08:56 PM
  3. null pointer exception problem
    By vandrop in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 2nd, 2011, 01:45 PM
  4. [SOLVED] Fixed Null Pointer Exception but still have another problem
    By javapenguin in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 29th, 2010, 10:16 PM
  5. Null Pointer Exception
    By MysticDeath in forum Exceptions
    Replies: 2
    Last Post: October 24th, 2009, 01:49 PM