• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sudoku grid shading

 
Andrew Evans
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.

 
Stephan van Hulst
Bartender
Posts: 6583
84
  • 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)
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java 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
Bartender
Posts: 6583
84
  • 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:
 
Loic Beylot
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
Bartender
Posts: 6583
84
  • 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!