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

Thread: NullPointerException problem

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Location
    Middle Georgia
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default NullPointerException problem

    I just recently learned how to incorporate time into my programs. Specifically using the Timer and TimerTask classes to make a specific action occur after a certain amount of times, and repeat every amount of time. I first tried it, and just made a simple program that counted to 10 then exited. That worked fine. Now my goal is to create an applet that draws red circles on the screen repetitively. Here is my code:
    import java.util.Timer;
    import java.util.TimerTask;
    import java.util.Date;
    import javax.swing.*;
    import java.awt.*;
     
    public class TimerTest extends JApplet
    {
    	public static Graphics pa;
    	public void paint(Graphics page)
    	{
    		pa = page;
    		pa.setColor(Color.red);
    		pa.fillOval(100,100,50,50);
    		Timer timer = new Timer();
    		timer.schedule(new CountDown(), 1000);
    	}
     
    	private static class CountDown extends TimerTask
    	{
    		public static Graphics pa;
    		public void paint(Graphics page)
    		{
    			pa = page;
    			pa.setColor(Color.red);
    			pa.fillOval(100,100,50,50);
    		}
    		public void run()
    		{
    			pa.fillOval(200,200,50,50);
    		}
    	}
    }

    This generates a NullPointerException error at runtime. I have done some research on this exception and discovered that this occurs when I have a variable that points to nothing. I can not see what is causing this error. Note: I know that thus far this code will only draw 2 total circles. I plan to adjust the code later, for now I just want to make sure that at least one circle (aside from the first one) will appear.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,707
    Thanks
    25
    Thanked 338 Times in 313 Posts

    Default Re: NullPointerException problem

    More likely than not, variables are getting hidden in your example. When you extend another class, the public variables and methods from the parent class are inherited, I believe the variable being static doesn't change that habit. So in your example, you should be able to do away with the public static Graphics pa; variable in the CountDown class and referencing a pa variable will still be fine.
    Where exactly are you getting the null pointer exception? Also, why is your Graphics pa object static?
    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/

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,372
    Thanks
    184
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: NullPointerException problem

    Below are some references for how to paint using Java2D:
    Learning Java 2D, Part 1
    Trail: 2D Graphics (The Java™ Tutorials)
    I point you to these because you are painting in a very strange manner.
    1) use a javax.swing.Timer, not a java.util.Timer. The reasons for this revolve around the single threaded Swing model.
    2) If you are using Swing (eg Components that begin with the letter J), override paintComponent rather than paint, and call the super method regardlenss of which method you override
    3) Don't keep references to a Graphics object. You have no way of controlling what happens to these during the course of your program.
    4) You never know when paint (or paintComponent) will be called, and you are constructing a Timer every time paint is called. If you truly wish this to occur every second, then start a repeating timer to fire every second, and call repaint() rather than painting directly to a Graphics object (which may or may not still reference the current Graphics context)

    This may not cover everything, but I do recommend considering the above links and each of the points above.

  4. #4
    Junior Member
    Join Date
    Feb 2012
    Location
    Middle Georgia
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException problem

    The exception occurs here:
    public void run()
    		{
    			pa.fillOval(200,200,50,50);		
                    }
    I made Graphics pa object static to get rid of an "unable to reference a nonstatic variable to a static method" error. I doubt that I was actually supposed to do that, but it got me around the error so. :/

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,707
    Thanks
    25
    Thanked 338 Times in 313 Posts

    Default Re: NullPointerException problem

    I assume it complained about that because CountDown is a static class. Forget my previous comment, I misread your code and thought CountDown was extending TimerTest (it is actually extending TimerTask).

    I assume you get this error because the run() method is called before the paint() method. Try calling repaint() before the pa.fillOval() call. You should also include the @Override tag whenever you are overriding an inherited method. I don't believe it will make the program do anything extra, but it makes it more readable at the very least.
    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/

  6. #6
    Junior Member
    Join Date
    Feb 2012
    Location
    Middle Georgia
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException problem

    Okay, I changed the
    pa.fillOval(100,100,50,50);
    to just repaint(). This got rid of my NullPointException error but now, the initial circle is drawn and nothing else happens.
    This is what my code looks like now:
    import java.util.Timer;
    import java.util.TimerTask;
    import javax.swing.*;
    import java.awt.*;
     
    public class TimerTest extends JApplet
    {
    	public static Graphics pa;
    	public void paint(Graphics page)
    	{
    		pa = page;
    		pa.setColor(Color.red);
    		pa.fillOval(100,100,50,50);
    		Timer timer = new Timer();
    		timer.schedule(new CountDown(), 1000);
    	}
     
    	private class CountDown extends TimerTask
    	{
    		public Graphics pa;
    		public int x, y, width, height;
    		public void paintComponent (Graphics page)
    		{
    			x = 100;
    			y = 100;
    			width = 50;
    			height = 50;
    			pa = page;
    			pa.setColor(Color.red);
    			pa.fillOval(x,y,width,height);
    		}
    		public void run()
    		{
    			x+=50;
    			y+=50;
    			repaint();
    		}
    	}
    }

  7. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,707
    Thanks
    25
    Thanked 338 Times in 313 Posts

    Default Re: NullPointerException problem

    call pa.fillOval(100,100,50,50) after the repaint() call in your run method.

    Also, word of advice, you most likely want to call super.paintComponent(page); as the first thing in your paintComponent() method.
    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/

Similar Threads

  1. Problem with java.lang.NullPointerException
    By more_dread in forum Exceptions
    Replies: 11
    Last Post: November 21st, 2011, 06:42 AM
  2. Problem with NullPointerException?
    By scooty199 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: November 6th, 2010, 06:13 PM
  3. NullPointerException
    By bbr201 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 29th, 2010, 08:06 PM
  4. problem in java.lang.NullPointerException
    By jianghuzai in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 28th, 2010, 11:24 AM
  5. NullPointerException:null problem
    By derky in forum Exceptions
    Replies: 8
    Last Post: September 18th, 2009, 04:06 PM