This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

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: 12165
258
  • 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: 21974
106
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: 12165
258
  • 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: 12165
258
  • 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
 
There's a city wid manhunt for this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic