Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

null pointer while acessing graphics object  RSS feed

 
Titus Abraham
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

i have a form which is having a panel within and after panel is initalized i try getting teh graphics object to draw some shapes on teh panel but when i access the object i get null pointer error.



Any clues any one.. I am clueless on why this is happening.,

Best regards,
Titus
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Usually, you should be overriding the paintComponent to play with the graphics.
Here is a small example
 
Titus Abraham
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well,

I need to paint at run time . i am making a card game so cards needs to be drawn on the panel at the run time. So how will i proceed. and Also why the null poinyer? i am doing a valid operation which puzzles me all the more.

Best regards,
Titus
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this
Graphics g = cardPanel.getGraphics();
is really, really bad.

1) the component MUST be visible, otherwise it's null (refer api docs)
2) any time you minimize/maximize, or open another app. on top, the graphics is gone.

test, and you'll find out.
 
Titus Abraham
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well,

Can you kindly tell me of some other way I can draw images and shapes on to the Panel at run time? like on click of a button or geting an event i would like to add another card (image)

Best regards,
Titus
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Can you kindly tell me of some other way I can draw images and shapes on to the Panel at run time?

Maneesh summed it up perfectly
"Usually, you should be overriding the paintComponent to play with the graphics. "

more reading here

http://java.sun.com/docs/books/tutorial/uiswing/painting/index.html

 
Titus Abraham
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Maneesh and Micheal.. I have got it working.. thank you for helping me to rectify the fundamental flaw. reminds me to read a bit of documentation before i do stuff.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!