Results 1 to 8 of 8
  1. #1
    Daslee's Avatar
    Daslee is offline Member
    Join Date
    Mar 2012
    Location
    Plunge, Lithuania
    Posts
    36
    Rep Power
    0

    Default NullPointerException on graphics drawing from hashmap

    Hello. I need help with graphics drawing, because i got NullPointerException, but its fake.. I have this on my code top:
    Java Code:
    public static DrawPane dPane = new DrawPane();
    And this is DrawPane:
    Java Code:
    class DrawPane extends JPanel {
    	private static final long serialVersionUID = 1L;
    	public HashMap<Integer, Rectangle> players = new HashMap<Integer, Rectangle>();
    	public int updateId;
    	
    	public void paintComponent(Graphics g){
    		Graphics2D g2 = (Graphics2D)g;
    	    RenderingHints rh = new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    	    g2.setRenderingHints(rh);
    	    g2.clearRect(0, 0, 750, 500);
    	    g2.setColor(Color.GRAY);
    	    g2.fillRect((int)players.get(updateId).getX(), (int)players.get(updateId).getY(), (int)players.get(updateId).getWidth(), (int)players.get(updateId).getHeight());
    	}
    }
    As you can see DrawPane have players hashmap. So in my main class when i adding something to players hashmap and changing updateId, then i do repaint(); so it says that g2.fillRect(); is NullPointerException, but players.get(updateId) is not Null, i checked it. Also there is code how im adding player to players hashmap:
    Java Code:
    dPane.updateId = 19;
    					dPane.players.put(19, new Rectangle(0, 0, 15, 15));
    					dPane.repaint();
    So thats all what i can show you and try to get help.
    But what is the most funny part.. That, when i adding rectangle to dPane.players and checking if its not null, it says that its not null. But when drawing it says it's nullpointerexception, but... It anyway draws that square

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: NullPointerException on graphics drawing from hashmap

    Could be any number of things.
    But I suspect you ought to stick some debugging in the paintComponent method, writing out the updateId and the contents of players.

    Oh, and you need to give us the full stack trace of the exception you're getting.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Daslee's Avatar
    Daslee is offline Member
    Join Date
    Mar 2012
    Location
    Plunge, Lithuania
    Posts
    36
    Rep Power
    0

    Default Re: NullPointerException on graphics drawing from hashmap

    If i print updateId and Rectangle which is drawing, so i got this:
    Java Code:
    19
    java.awt.Rectangle[x=0,y=0,width=15,height=15]
    And full stack trace:
    Java Code:
    0
    null
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at DrawPane.paintComponent(NakovChatClientGUI.java:155)
    	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.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
    	at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
    	at javax.swing.RepaintManager.paint(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
    	at sun.awt.SunGraphicsCallback.runOneComponent(Unknown 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.paintDirtyRegions(Unknown Source)
    	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    	at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
    	at javax.swing.RepaintManager.access$700(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$000(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.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)
    0
    null
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at DrawPane.paintComponent(NakovChatClientGUI.java:155)
    	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.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
    	at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
    	at javax.swing.RepaintManager.paint(Unknown Source)
    	at javax.swing.JComponent.paint(Unknown Source)
    	at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
    	at sun.awt.SunGraphicsCallback.runOneComponent(Unknown 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.paintDirtyRegions(Unknown Source)
    	at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    	at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
    	at javax.swing.RepaintManager.access$700(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$000(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.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)

  4. #4
    Daslee's Avatar
    Daslee is offline Member
    Join Date
    Mar 2012
    Location
    Plunge, Lithuania
    Posts
    36
    Rep Power
    0

    Default Re: NullPointerException on graphics drawing from hashmap

    Omg i just now see that he wants to draw rectangle with id 0 and that rectangle is null, but who told him to draw 0?
    I never set updateId to 0 and told to repaint();

    Maybe it could cause this: frame.setContentPane(dPane); because in DrawPane is public int updateId, so it default is 0, and maybe frame.setContentPane(dPane); or public static DrawPane dPane = new DrawPane(); called repaint() method?
    Last edited by Daslee; 03-19-2012 at 12:12 PM.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: NullPointerException on graphics drawing from hashmap

    You presumably add the DrawPane to a JFrame (or Applet, or another Panel).
    Swing will attempt to draw it, which involves calling paintComponent().
    So before you even set any of those values Swing will (understandably) try and draw the pane.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Daslee's Avatar
    Daslee is offline Member
    Join Date
    Mar 2012
    Location
    Plunge, Lithuania
    Posts
    36
    Rep Power
    0

    Default Re: NullPointerException on graphics drawing from hashmap

    So i said it in my last post "frame.setContentPane(dPane);". But how i can fix that if it's possible?

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: NullPointerException on graphics drawing from hashmap

    Possibly do a check in paintComponent?
    I don't know how you want this to work, so exactly what the check should be I couldn't say.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    Daslee's Avatar
    Daslee is offline Member
    Join Date
    Mar 2012
    Location
    Plunge, Lithuania
    Posts
    36
    Rep Power
    0

    Default Re: NullPointerException on graphics drawing from hashmap

    Thanks, fixed it! :P

Similar Threads

  1. Replies: 1
    Last Post: 10-26-2011, 07:03 PM
  2. NullPointerException on Graphics.drawImage
    By Jaeela in forum New To Java
    Replies: 5
    Last Post: 10-05-2011, 02:36 AM
  3. Drawing a graphics onto another Graphics ?
    By Ziden in forum Java Applets
    Replies: 0
    Last Post: 01-08-2011, 07:30 PM
  4. Drawing simple graphics in NetBeans
    By arifin in forum AWT / Swing
    Replies: 1
    Last Post: 11-12-2010, 10:01 AM
  5. Drawing graphics primitives
    By diegofsza in forum Java 2D
    Replies: 7
    Last Post: 09-24-2009, 11:10 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
  •