Results 1 to 9 of 9
  1. #1
    haas117 is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default NullPointerException in basic GUI

    I have no idea what to do here someone please help
    This is the error message
    Java Code:
     Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
            at ColorPanel1.paintComponent(ColorPanel1.java:22)
            at javax.swing.JComponent.paint(JComponent.java:1029)
            at javax.swing.JComponent.paintChildren(JComponent.java:862)
            at javax.swing.JComponent.paint(JComponent.java:1038)
            at javax.swing.JComponent.paintChildren(JComponent.java:862)
            at javax.swing.JComponent.paint(JComponent.java:1038)
            at javax.swing.JLayeredPane.paint(JLayeredPane.java:567)
            at javax.swing.JComponent.paintChildren(JComponent.java:862)
            at javax.swing.JComponent.paintToOffscreen(JComponent.java:5131)
            at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManag
    er.java:278)
            at javax.swing.RepaintManager.paint(RepaintManager.java:1224)
            at javax.swing.JComponent.paint(JComponent.java:1015)
            at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
    
            at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:
    60)
            at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97
    )
            at java.awt.Container.paint(Container.java:1780)
            at java.awt.Window.paint(Window.java:3375)
            at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
    
            at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:713)
    
            at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:6
    93)
            at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(System
    EventQueueUtilities.java:125)
            at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
            at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
    ad.java:269)
            at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
    java:184)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
    ad.java:174)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
    Press any key to continue...
    This is my Gui window main class
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class readgame {
    
        public static void main(String[] args) {
            JFrame GUI = new JFrame();
            GUI.setTitle("Austin's Game");
            GUI.setSize(300, 200);
            GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            ColorPanel1 level1 = new ColorPanel1();
            Container pane = GUI.getContentPane();
            pane.add(level1);
    
            GUI.setVisible(true);
        }
    }
    This is the Color panel 1 class
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class ColorPanel1 extends JPanel {
    
    	private Circle c1, c2;
    	private Circle selectedCircle;
    	private int x, y;
    
        public void ColorPanel1() {
        	setBackground(Color.white);
        	c1 = new Circle (200, 100, 25, Color.red);
        	c2 = new Circle (100, 100, 50, Color.blue);
        	selectedCircle = null;
        	addMouseListener(new PanelListener());
        	addMouseMotionListener(new PanelMotionListener());
        }
    
        public void paintComponent(Graphics g){
        	super.paintComponent(g);
        	c1.draw(g);
        	c2.draw(g);
        }
    
        private class PanelListener extends MouseAdapter{
        	public void mousePressed(MouseEvent e){
        		x = e.getX();
        		y = e.getY();
        		if (c1.containsPoint(x, y))
        			selectedCircle = c1;
        		else if (c2.containsPoint(x, y))
        			selectedCircle = c2;
        	}
        	public void mouseReleased (MouseEvent e){
        		x = e.getX();
        		y = e.getY();
        		selectedCircle = null;
        	}
    }
    private class PanelMotionListener extends MouseMotionAdapter{
    
    	public void mouseDragged(MouseEvent e){
    		int newX = e.getX();
    		int newY = e.getY();
    		int dx = newX - x;
    		int dy = newY - y;
    		if(selectedCircle != null)
    			selectedCircle.move(dx, dy);
    		x = newX;
    		y = newY;
    		repaint();
    
    	}
    	}
    }
    This is the Circle class
    Java Code:
    import java.awt.*;
    
    public class Circle {
    	private int centerX, centerY, radius;
    	private Color color;
    
        public Circle(int x, int y, int r, Color c){
        	centerX = x;
        	centerY = y;
        	radius = r;
        	color = c;
        }
        public void draw (Graphics g){
        	Color oldColor = g.getColor();
        	g.setColor(color);
        	g.drawOval(centerX - radius, centerY - radius,
        				radius * 2, radius * 2);
        				g.setColor(oldColor);
        }
        public boolean containsPoint(int x, int y){
        	int xSquared = (x - centerX) * (x - centerX);
        	int ySquared = (y - centerY) * (y - centerY);
        	int radiusSquared = radius * radius;
        	return xSquared + ySquared - radiusSquared <= 0;
    
        }
        public void move(int xAmount, int yAmount){
        	centerX = centerX + xAmount;
        	centerY = centerY + yAmount;
        }
    }
    Please Help:confused:

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    This method is never called:

    Java Code:
       public void ColorPanel1() {
          setBackground(Color.white);
          c1 = new Circle(200, 100, 25, Color.red);
          c2 = new Circle(100, 100, 50, Color.blue);
          selectedCircle = null;
          addMouseListener(new PanelListener());
          addMouseMotionListener(new PanelMotionListener());
       }

    and so c1 and c2 are never initialized. Why is it never called? Because it has a void return type and constructors should have no return type:

    Java Code:
       public ColorPanel1() {
    
          //.....
    
       }
    Luck and HTH!

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Oh, and welcome to the forum, and special kudos for using code tags in your first post in this forum. Most unusual and refreshing to see.

  4. #4
    haas117 is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    ah thanks thats it.
    im only halfway into a year of comm science so ya thanks

    also the c1.draw works but c1.fill does not
    im pretty sure its because my text book is old but i have no idea what the correct code is to make the circle appear solid ...if anyones know, or if fubarable knows that would be great

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by haas117 View Post
    ah thanks thats it.
    im only halfway into a year of comm science so ya thanks
    You're welcome.


    also the c1.draw works but c1.fill does not
    im pretty sure its because my text book is old but i have no idea what the correct code is to make the circle appear solid ...if anyones know, or if fubarable knows that would be great
    You call fill very similar to how you call draw. Please show us how you attempt to call it, and we may be able to better help you.

  6. #6
    haas117 is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    Java Code:
        public void paintComponent(Graphics g){
        	super.paintComponent(g);
        	c1.draw(g);
        	c2.draw(g);
        }
    i tried
    c1.fill(g);
    but it didnt work

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    fill is called like draw, but it is called on a Graphic object, i.e, Graphic#fillOval(....).

    Wouldn't you would first need to give the Circle class a fill method for it to work, using a call similar to above, if you are going to try to call it off of a Circle object?

  8. #8
    haas117 is offline Member
    Join Date
    Jan 2011
    Posts
    4
    Rep Power
    0

    Default

    Ah ya thats pretty much it but i already had the method it was
    Java Code:
       public void draw (Graphics g){
        	Color oldColor = g.getColor();
        	g.setColor(color);
        	g.drawOval(centerX - radius, centerY - radius,
        				radius * 2, radius * 2);
        				g.setColor(oldColor);
        }
    but on the last line it should be g.fillOval(...)

    Thanks for answering my stupid questions

    I appreciate all the help:D

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Basic GUI help
    By vahshir in forum New To Java
    Replies: 5
    Last Post: 10-11-2010, 01:33 AM
  2. Need really basic help!
    By anthonyalaan in forum New To Java
    Replies: 9
    Last Post: 09-12-2010, 01:43 PM
  3. Basic Help
    By robjames in forum New To Java
    Replies: 3
    Last Post: 02-09-2009, 02:58 AM
  4. Need some really basic help
    By Mayur in forum New To Java
    Replies: 6
    Last Post: 01-24-2009, 06:00 AM
  5. help with basic example
    By fred in forum New To Java
    Replies: 1
    Last Post: 07-20-2007, 05:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •