This is actually part of a much bigger program, but I cut it down to the problematic area to make it easier to read.
import javax.swing.JButton;
public class test{
static GameButton[] grid;
static class GameButton extends JButton{
int value = 0;
}
public static void main(String[] args){
grid = new GameButton[1];
if(grid[0].value == 0){
System.out.println("It's working");
}
}
Exception in thread "main" java.lang.NullPointerException
at test.main(test.java:11) <===== thats line 11
The problem is for sure the array.... It doesn't actually allocate "int value = 0;" into memory for each of the array elements, which means "value" is still null in memory, not 0..
How can I fix this problem? Also, this is one of the weirdest ones I've come across, so if someone can explain to me what actually happens when I do "grid = new GameButton[1];" with respect to that "int value = 0;" in the GameButton class, maybe I can get some kind of understanding.... Why is value = null and not 0?
Switching from creating a new array with 1 elements:
"grid = new GameButton[1];"
into a new GameButton constructor:
"grid = new GameButton();"
seems to work but why doesn't creating an array with 1 element call the GameButton constructor for each element automatically? It's too confusing.....