Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Sudoku grid shading

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a sudoku 9x9 grid and i am trying to shade the 3x3 cells in the top right, left, center, bottom left and right
I have tried to do it by accessing the points in the 2d array which was fine for position

subPanels[0][0].setBackground(Color.lightGray);

but if i try it for the other positions it shows errors.

subPanels[0][1].setBackground(Color.lightGray);
This throws the error?

Could someone tell me why it is doing this or assist in shading the other cells.

 
Saloon Keeper
Posts: 10420
223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Andrew. Maybe you can tell us the error?
 
Andrew Evans
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stephan

The Error is

Exception in thread "main" java.lang.NullPointerException
at Gui.<init>(Gui.java:78)
 
Sheriff
Posts: 21775
103
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what is line 78 of file Gui.java?
 
Andrew Evans
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the full code for the Gui

This line works on its own - subPanels[0][0].setBackground(Color.lightGray);
but if i add -subPanels[0][1].setBackground(Color.lightGray);

that is when error happens

 
Stephan van Hulst
Saloon Keeper
Posts: 10420
223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is because you are trying to dereference an array element before you created it. Think about it, when you call subPanels[0][1] in the first iteration of your nested loops, this element doesn't exist yet.

You should replace the two setBackground lines with the following:
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Andrew, If thing this is because you try to access the element number 1 of your array, but just the number 0 have been instantiated.

But do you really need those two lines?

- Your double loop should go inside every boxes.
- This operation will be repeated over and over doing exactly the same thing every time.

Edit: oups, too late.
 
Andrew Evans
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried adding the if statement instead of the original lines but still nothing happens
the whole grid remains with a white background
 
Andrew Evans
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My mistake i have moved the position of the if statement and it now works

Thanks for the assistance
 
Stephan van Hulst
Saloon Keeper
Posts: 10420
223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because you're overriding the background color for each text field near the end of your nested loop.

[edit]

You figured it out :P
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!