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

Thread: null pointer exception error for a method calling a graphics obj help please

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default null pointer exception error for a method calling a graphics obj help please

    I am trying to get a paddleball game working and as i create my paintComponent i have it call a method from a different class and its giving me this error

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at pong.panelc.paintComponent(panelc.java:48)
    I pasted the code below minus movement and listener methods and its giving me the same errors.


    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.ActionListener;
     
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.Timer;
     
     
     
     
     
    public class pongTest extends JPanel{
     
     
    	int displayH;
    	int displayW;
    	int paddlex;
    	panelc display;
    	ballc ball;
    	paddlec paddle;
     
     
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		pongTest obj = new pongTest();
    	}
     
    	pongTest()
    	{
     
     
    		display = new panelc();
    		displayH = display.getHeight();
    		displayW = display.getWidth();
    		paddle = new paddlec(40,300);
    		ball = new ballc(20);
     
    	}
    	class panelc extends JPanel
    	{
    	int height;
    	int width;
     
    	JFrame frameD;
    	pongTest control;
     
     
    	panelc()
    	{
     
    		width = getWidth();
    		height = getHeight();
     
    		frameD = new JFrame();
    		frameD.setSize(640,480);
    		frameD.setResizable(false);
    		frameD.getContentPane().add(this);
    		frameD.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frameD.setVisible(true);
     
     
     
    	}
     
    	public void paintComponent(Graphics g) 
    	{
    		super.paintComponent(g);
    		setBackground(Color.BLACK);
    		g.fillRect(0, 0, frameD.getWidth(), frameD.getHeight());
    		control.draw(g);
    	}
    	}
    	class paddlec extends JPanel //paddle class
    	{
     
    		int Length;
    		int width;
    		int positionX;
    		int positionY;
    		int paddleCenter;
     
     
    		paddlec(int length, int positionx)
    		{
     
    			width = 5;
    			Length = length;
    			positionX = positionx;
    			positionY = 30;
    			paddleCenter = (positionx + (length / 2));
     
    		}
     
    		public void paintComponent(Graphics g)
    		{
     
    			g.fillRect(positionX,positionY,Length,width);
    			g.setColor(Color.ORANGE);
     
    		}
    	}
     
    	class ballc extends JPanel  //ball class
    	{
     
    		int size;   // size of ball
    		int positionX;  // x position of ball
    		int positionY;  // y position
    		int movementX;  // movement speed in x direction
    		int movementY;  // in y direction
    		int pheight;  // display panel height
    		int pwidth;  // display panel width
     
     
     
    		ballc(int sizex) // constructor setting size position and display size and movement speed
    		{
     
    			size = sizex;
    			positionX = 0;
    			positionY = 0;
    			movementX = (size / 4);
    			movementY = (size / 4);
     
     
    		}
     
    		public void paintComponent(Graphics g)
    		{
     
    			g.setColor(Color.BLUE);
    			g.fillOval(positionX, positionY, size, size);
     
    		}
    	}
    	public void draw(Graphics g)
    	{	
    		if ((paddle != null) && (ball != null))
    		{
    		paddle.repaint();
    		ball.repaint();
    		}
     
    	}
    }


    I can get the black background to show sometimes but not reliably and never with the ball or paddle.
    any help is appreciated thanks in advance.
    Last edited by jorys22; December 2nd, 2013 at 04:57 PM. Reason: Add a SSCCE


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: null pointer exception error for a method calling a graphics obj help please

    This really isn't enough code for us to debug. Can you post an SSCCE showing what you're doing?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    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: null pointer exception error for a method calling a graphics obj help please

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at pong.panelc.paintComponent(panelc.java:48)
    What variable on line 48 is null when that line is executed?
    If you don't understand my answer, don't ignore it, ask a question.

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

    jorys22 (December 2nd, 2013)

  5. #4
    Junior Member
    Join Date
    Nov 2013
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: null pointer exception error for a method calling a graphics obj help please

    here is the full stackTrace
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at test.pongTest$panelc.paintComponent(pongTest.java: 74)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JLayeredPane.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paintToOffscreen(Unknown Source)
    at javax.swing.BufferStrategyPaintManager.paint(Unkno wn Source)
    at javax.swing.RepaintManager.paint(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at java.awt.GraphicsCallback$PaintCallback.run(Unknow n Source)
    at sun.awt.SunGraphicsCallback.runOneComponent(Unknow n Source)
    at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
    at java.awt.Container.paint(Unknown Source)
    at java.awt.Window.paint(Unknown Source)
    at javax.swing.RepaintManager$3.run(Unknown Source)
    at javax.swing.RepaintManager$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unkno wn Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unkno wn Source)
    at javax.swing.RepaintManager.prePaintDirtyRegions(Un known Source)
    at javax.swing.RepaintManager.access$1100(Unknown Source)
    at javax.swing.RepaintManager$ProcessingRunnable.run( Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

  6. #5
    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: null pointer exception error for a method calling a graphics obj help please

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at test.pongTest$panelc.paintComponent(pongTest.java: 74)
    What variable on line 74 is null when that line is executed?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #6
    Junior Member
    Join Date
    Nov 2013
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: null pointer exception error for a method calling a graphics obj help please

    oh its the g im guessing but i dont' know how to assign a value to a graphics variable.

    --- Update ---

    this is where the null pointer is highlighting
    control.draw(g);

    and this is the method

    public void draw(Graphics g)
    {
    if ((paddle != null) && (ball != null))
    {
    paddle.repaint();
    ball.repaint();
    }

    }

  8. #7
    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: null pointer exception error for a method calling a graphics obj help please

    What is the value of control when that line is executed? Add a println() just before line 74 and print out the value of control and g.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #8
    Junior Member
    Join Date
    Nov 2013
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: null pointer exception error for a method calling a graphics obj help please

    it is null. when i initialize it though its constructor creates a class of panelc which in turn creates a pongTest obj and so it fails. in my instructions my pongTest constructor has to create a ball a paddle and a panel and the panels paintComponent has to send its graphics obj to the draw method of pongTest.

    I have tried every way i can think of to get this to compile with out having the never ending loop but if it compiles it doesn't draw the ball or paddle or it doesn't compile at all.

  10. #9
    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: null pointer exception error for a method calling a graphics obj help please

    Are you talking about the variable: control? Have you defined it in more than one place so that the local variable shadows the class level variable?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #10
    Junior Member
    Join Date
    Nov 2013
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: null pointer exception error for a method calling a graphics obj help please

    yes the variable control is what im refering too. when i try to create it as a main class lvl variable it didn't even compile.

    --- Update ---

    Im sorry i am really struggling with this program im very new to programming and this one is kicking my butt im completely lost and don't even know what to search for for help. I really appreciate you taking the time to try and help.

  12. #11
    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: null pointer exception error for a method calling a graphics obj help please

    it didn't even compile
    If you want help with the errors, you will have to post the code and the full text of the error messages.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Null Pointer Exception error
    By Keitho55 in forum Exceptions
    Replies: 3
    Last Post: November 18th, 2011, 03:29 AM
  2. NULL POINTER EXCEPTION error
    By beefwithrice in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 28th, 2011, 06:26 AM
  3. JComboBox null pointer Exception error
    By F_Arches in forum AWT / Java Swing
    Replies: 2
    Last Post: November 29th, 2009, 02:32 PM
  4. Null Pointer Exception
    By MysticDeath in forum Exceptions
    Replies: 2
    Last Post: October 24th, 2009, 01:49 PM
  5. Getting Null Pointer Exception in runtime
    By RoadRunner in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2009, 01:21 PM