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

Thread: Exception in Main NullPointerException?

  1. #1
    Junior Member lil_misfitss's Avatar
    Join Date
    Aug 2013
    Location
    USA!!!!!
    Posts
    24
    My Mood
    Confused
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Unhappy Exception in Main NullPointerException?

    Hello!
    I'm trying to make a program where the ball moves to the right of the screen but my program compiles correctly. It's when I run it is when this pops up:
    Exception in thread "main" java.lang.NullPoinerException
    at java.awt.Container.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at javax.swing.JFrame.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at FirstGame.<init><FirstGame.java:18>
    at FirstGame.main<FirstGame.java:27>

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import java.util.*;
    import java.util.concurrent.*;
    //Start
    class FirstGame extends JFrame
    {
    	private PaintSurface canvas;
     
    	public FirstGame() 
    	{
    		JFrame frame = new JFrame();
    		frame.setSize(800, 600);
    		frame.setResizable(false);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.add(canvas,BorderLayout.CENTER);
     
    		ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(3);
    		executor.scheduleAtFixedRate(new paintSpriteThread(frame), 0L, 20L, TimeUnit.MILLISECONDS);
     
    	}
     
    	public static void main(String[] args)
    	{
    		new FirstGame();
    	}
    }
    class paintSpriteThread implements Runnable
    {
     
    	JFrame c;
     
    	public paintSpriteThread(JFrame c)
    	{
    		this.c = c;
    	}
     
    	public void run()
    	{
    		c.repaint();
    	}
    }
     
    class PaintSurface extends JComponent
    {
    	int x = 400;
     
    	int y = 300;
     
    	int d = 20;
     
    	public void paint(Graphics g)
    	{
    		Graphics2D g2 = (Graphics2D)g;
    		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
    		Shape ball = new Ellipse2D.Float(x,y,d,d);
     
    		x+=1;
     
    		g2.setColor(Color.RED);
    		g2.fill(ball);
    	}
    }

    Welp, with every error I will learn something new!
    Thank you to all who reply with helpfullness!
    lil_misfit


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Exception in Main NullPointerException?

    Exception in thread "main" java.lang.NullPoinerException
    at java.awt.Container.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at javax.swing.JFrame.addImpl(Unknown Source)
    at java.awt.Container.add(Unknown Source)
    at FirstGame.<init><FirstGame.java:18>
    There is a null value in the add() statement called on line 18.
    If you don't understand my answer, don't ignore it, ask a question.

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

    lil_misfitss (June 22nd, 2014)

  4. #3
    Junior Member lil_misfitss's Avatar
    Join Date
    Aug 2013
    Location
    USA!!!!!
    Posts
    24
    My Mood
    Confused
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Exception in Main NullPointerException?

    Wow, it was that simple thanks!

    lil_misfit

Similar Threads

  1. Replies: 1
    Last Post: May 25th, 2014, 05:36 AM
  2. Exception in my code:Exception in thread "main" java.lang.NullPointerException
    By vickychawla.chawla in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 25th, 2014, 05:30 AM
  3. I get the error Exception in thread "main" java.lang.NullPointerException?
    By jeremy28 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: October 3rd, 2013, 11:13 PM
  4. Exception in thread "main" java.lang.NullPointerException problem.......
    By Adam802 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 20th, 2012, 02:23 AM
  5. exception in thread main java.lang.Nullpointerexception
    By westandeast in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 6th, 2011, 09:08 AM