Michał Szydłowski
Michał Szydłowski

Reputation: 3409

Android - Null pointer exception in a constructor variable

I'm working on a custom adapter for a grid view in an android application. It is defined as follows:

public class BoardAdapter  extends BaseAdapter {

public static final int EMPTY = 0;
public static final int RED = 1;
public static final int BLACK = 2;
public static final int RED_PROMOTED = 3;
public static final int BLACK_PROMOTED = 4;


Context context;
int[][] board = null;


public int[] pieces = new int [64];
{
        for(int i=0;i<8;i++)
        {
            for(int j=0;j<8;j++)
            {
    //null pointer exception here           
                if(board[i][j] == RED)
                {
                    pieces[8*i+j] = R.drawable.red_piece;
                }
                else if(board[i][j] == BLACK)
                {
                    pieces[8*i+j] = R.drawable.black_piece;
                }
                else pieces[8*i+j] = 0;
            }
        }
};


public BoardAdapter (Context ctx, int[][] board)
{
    this.context = ctx;
    this.board = board;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return pieces.length;
}

@Override
public Object getItem(int pos) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public long getItemId(int pos) {
    // TODO Auto-generated method stub
    return pieces[pos];
}

@Override
public View getView(int pos, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ImageView imageView = new ImageView(context);
    imageView.setImageResource(pieces[pos]);
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
    return imageView;       
}

}

I create the object in the onCreate method of the activity:

    protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game);  
    board = (GridView) findViewById (R.id.board);
    game.printBoard();
    //null pointer exception here
    board.setAdapter(new BoardAdapter(this, game.getBoard()));

}

When I print the board, the log displays the correct values. So I am sure, that I pass an initialized board to the BoardAdapter constructor. I have no idea, why at the object creation it throws a null pointer exception when referring to this array's element...

Upvotes: 0

Views: 576

Answers (2)

ppeterka
ppeterka

Reputation: 20726

In the order of processing

...
int[][] board = null; // this.board value assigned as null

public int[] pieces = new int [64]; //defining value, doesn't matter now

//WARNING this is an instance initializer block! Gets to run before the code of the constructor...
{ 
    for(int i=0;i<8;i++)
    {
        for(int j=0;j<8;j++)
        {
//null pointer exception here           
            if(board[i][j] == RED) //access stuff that does not exist...
            {
                pieces[8*i+j] = R.drawable.red_piece;
            }
            else if(board[i][j] == BLACK)
            {
                pieces[8*i+j] = R.drawable.black_piece;
            }
            else pieces[8*i+j] = 0;
        }
    }
};

...

Somewhere in the future the constuctor would be called

  ...
  this.board = board; //board is assigned a value
  ...

EDIT

I want to assign the values of a 2 dimensional array board into a one-dimensional array pieces, so that I can get the appropriate images for the grid elements

Then you should create a method to accomplish that, and not a static initializer block (assuming the voard is always 8 by 8, and is not null):

public int[] getPieces()
{ 
    int[] pieces = new int[64];
    for(int i=0;i<8;i++)
    {
        for(int j=0;j<8;j++)
        {
            if(board[i][j] == RED)
            {
                pieces[8*i+j] = R.drawable.red_piece;
            }
            else if(board[i][j] == BLACK)
            {
                pieces[8*i+j] = R.drawable.black_piece;
            }
            else pieces[8*i+j] = 0;
        }
    }
    return pieces;
};

And call this on BoardAdapter instance whenever you'd like.

Upvotes: 1

Suresh Atta
Suresh Atta

Reputation: 121998

Since the you wrote a initializer block and using board which is null.

public int[] pieces = new int [64];

//you are started a block here.MIND that the board not yet initialized.

{
        for(int i=0;i<8;i++)
        {
            for(int j=0;j<8;j++)
            {
    //null pointer exception here           
                if(board[i][j] == RED)
                {
                    pieces[8*i+j] = R.drawable.red_piece;
                }
                else if(board[i][j] == BLACK)
                {
                    pieces[8*i+j] = R.drawable.black_piece;
                }
                else pieces[8*i+j] = 0;
            }
        }
};

What you do is create a method called intializer and do there

public int[] pieces = new int [64];
private void intialize(){

{
        for(int i=0;i<8;i++)
        {
            for(int j=0;j<8;j++)
            {
    //null pointer exception here           
                if(board[i][j] == RED)
                {
                    pieces[8*i+j] = R.drawable.red_piece;
                }
                else if(board[i][j] == BLACK)
                {
                    pieces[8*i+j] = R.drawable.black_piece;
                }
                else pieces[8*i+j] = 0;
            }
        }
}

}

and now call that method in constructor.

public BoardAdapter (Context ctx, int[][] board)
{
    this.context = ctx;
    this.board = board;
    intialize();
}

Upvotes: 3

Related Questions