• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Draw on a panel

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. I'm trying draw on a panel inside a frame. But all the time I get 2Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException" in the repaintComponent.
This is all the exception error.



and keeps repeating all the time.

the code is the below



I'm trying to implement the langton's ant problem on java. Inside ant.render(g) is jus the follow code


My idea is on repaintcomponent paint the world an the ant positions. But I can't get this work. Any solution? thanks!
 
Marshal
Posts: 75629
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And which line does the Exception occur on?
 
Rancher
Posts: 3262
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Try changing the order of the above statements. You make the frame visible before you create your Ant class, so the Ant variable will be null when the frame tries to paint itself.
 
Bartender
Posts: 4903
185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, that's not the problem.

The problem is not uncommon on this site. I've written on this before, but I must say:
this version is very complex and subtle.

First of all: Main extends JPanel, but nevertheless in the 'initG' method another
Main panel is created. However, this panel is not subject to 'initG' and 'init',
so Ant ant gets uninitialized.

The extra complication is that in 'main' all looks okay. a new Main g is created,
and that g is neatly subjected to initG and init. But that is not the panel
that gets added to the frame....

Here is a corrected listing that works. Since I do not have the Cell and Ant classes,
I dropped the Cell and turned Ant into a String. I tried not to change too much,
but OP's construct and mine are still ehh... not 'top notch'.

Greetz,
Piet


 
Saloon Keeper
Posts: 13824
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, this is extremely iffy, what with separation of concerns. This is what a Swing frame should look like:

 
This will take every ounce of my mental strength! All for a tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic