• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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: 79151
377
  • 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: 3324
32
  • 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: 5465
212
  • 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: 15484
363
  • 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:

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic