Author Topic: [solved] public variables and null pointers.  (Read 2624 times)

0 Members and 1 Guest are viewing this topic.

Offline squidgetx

  • Food.
  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1881
  • Rating: +503/-17
  • rawr.
    • View Profile
[solved] public variables and null pointers.
« on: December 27, 2012, 10:56:44 pm »
Ok, so here's my code basically. Normally I'd spend a bit more time trying to figure it out but I'll be away for the next couple days so I thought I could run it by you guys first :)

Code: [Select]
public class stuff {

public static Tile[] tileReference;
//a reference array to keep tile objects reference-able by tile number (this is a calc/computer project)
public static TileSelector tileContainer; //need to declare this outside of the main class so it can be accessed by other nested static classes in 'stuff'
public static class TileSelector extends JPanel {
 public void paintComponent(Graphics g) {
  //initial drawing stuff
  for(int i =0; i<tileReference.length, i++)
   tileReference[i].drawTile(blah blah blah)
public static void main(String[] args) {
...stuff initializing contents of tileReference
tileContainer = new TileSelector();
Right at that tileReference[].drawTile method I get a NullPointerException at runtime. In the main method, the first creation of a TileSelector occurs after I've initialized the contents of tileReference.

Does anybody have any ideas on how to get around this? If anyone has suggestions on how to better organize my code, I'm also welcome to that. I need tileReference[] to be accessible by many different methods and classes, and the instance tileContainer needs to be accessible by one other class (well to be specific, the active tile, which is encapsulated in the tileSelector class needs to be accessible. I suppose I could make that another instance of the main class "stuff" but i might run into complications, as mouse events in the tileSelector need to be able to modify the active tile. I might be ablet o give it a go but I'm not sure if that would solve the problem) .
* squidgetx is pretty new to oop, so feel free to also elaborate on the philosophical and practical differences between nested static and inner classes.

edit: solved on irc, thanks runer and jacobly
« Last Edit: December 27, 2012, 11:23:21 pm by squidgetx »