Question: I am developing a game on Android that involves a grid of objects. Right now I am using an array to store the objects, like this:

```
public class ObjectBuffer {

  private final Object[] buffer;

  private final int playfieldWidth;

  ObjectBuffer(int playfieldWidth, int playfieldHeight) {
    super();
    this.buffer = new Object[playfieldWidth + (playfieldHeight * playfieldWidth)];
    this.playfieldWidth = playfieldWidth;
  }

  public static int getX(int id) {
    return id % playfieldWidth();
  }

  public static int getY(int id) {
    return id / playfieldWidth();
  }

  public Object get(int id) {
    return this.buffer[id];
  }

  public Object get(int x, int y) {
    return this.buffer[x + (y * this.playfieldWidth)];
  }
}

```

But, my questions is, is this the best way to accomplish this? I chose an array (as opposed to a list, ArrayList, or some other container) because once the width and height of the playfield are set, they do not change. The objects are there for state, and once they are instantiated and added to the array, they may be updated, but are not rearranged.

My question is similar to [Storing objects for locating by x,y coordinates](https://stackoverflow.com/questions/132319/storing-objects-for-locating-by-x-y-coordinates), but different enough that I wanted to ask my question separately. My requirements are different from his, and I don't think that a spatial index would be warranted in this situation. Please correct me if I'm wrong.

I should also mention, the array's dimensions will usually be 10x10 (100 elements), but may need to be able to accommodate a modest increase; but will not need to handle more than about a 15x15 grid (225 elements). These dimensions will always be known when generating the grid. After the grid has been generated, the dimensions will not change.

Answer: 
You're calling `type.getClass()` but at that point `type` is *guaranteed* to be null. No other code will have had a chance to set it yet, so how did you expect it to work? You say you're confused about why it's null - but you haven't shown anything which would make it non-null.

I suspect you want something like:

```
public abstract class ExcelReader<T>{
     protected final Class<T> type;
     protected GenericResolver resolver;

     public ExcelReader(Class<T> type){
        this.type = type;
        resolver = ResolverFactory.createResolver(type); 
     }
}

```

You'd want to introduce the same sort of parameter on the subclasses too.
I don't think you are trying to use the generics for the thing they were intended to use. Generics are a kind of "syntactic sugar" usable **in compile-time only** as an additional level of code validation. What you seem to want to achieve - correct me if I am wrong - is to provide the generic type to your POIReader class and then be able (in runtime) to access this type's class object. Is this correct?

The way you could use the generics in your example - but I am not sure this will be what you want to achieve - is for something like this:

```
public abstract class ExcelReader<T>{
     protected T value;
     protected GenericResolver resolver;

     public ExcelReader(T value) {
        super();
        resolver=ResolverFactory.createResolver(type.getClass()); 
     } 
}

public class POIReader<T> extends ExcelReader<T>{
    public POIReader(T value) {
        super(value);
    }
}

```

and then:

```
MessageDTO yourObject = ...;
ExcelReader<MessageDTO> reader=new POIReader<MessageDTO>(yourObject);

```

This way, thanks to using generics the compiler will not allow you to pass anything that is not assignable to a `MessageDTO` class as the argument to `POIReader`'s constructor - but you still must assign **something** to that value field in order to be able to call the `getClass()` method. This is because, due to type erasure of generics (an unfortunate backwards-compatibility thing), generics you provide are **not** available in runtime, only in compile-time.