Results 1 to 2 of 2

Thread: 2D Object array

  1. #1
    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0

    Smile 2D Object array

    hi,

    i am trying to read the file

    ---bb-
    --w-w-
    --b-b-b

    i have 2 classes lets say
    class a{

    private classB[][] board = new board[6][6];
    private String file2 = "sample.txt";
    private int i=0;

    FileReader fReader = null;

    try
    {
    try
    {


    fReader = new FileReader (file2);
    Scanner in = new Scanner(fReader);
    int i=0;
    while (in.hasNextLine())
    {
    String inputLine = in.nextLine();
    String inLine = inputLine;
    for(j=0;j<9;j++)
    {
    board[i][j].state = inLine.charAt(j);
    }
    i++;// not writing the complete code i mean catch n finally

    nnow in classB{

    char state;

    but wen i try to assign the value to the array i get a null pointer exception , i tried to cheeck null pointer exception n then insert but still i have problems to assign values

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,006
    Blog Entries
    7
    Rep Power
    20

    Default Re: 2D Object array

    Your 'board' array has a bunch of references to classB objects but they don't refer to any objects (yet); in your read loop you assume that a board[ i ][ j ] refers to such an object (it doesn't). Create those objects first:

    Java Code:
    for (int row= 0; row < board.length; row++)
       for (int col= 0; col < board[row].length; col++)
          board[row][col]= new classB();
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Look if an array has an specified object
    By Josep_16 in forum New To Java
    Replies: 0
    Last Post: 10-09-2011, 11:43 AM
  2. Is an object array considered to be an object?
    By guest_user in forum New To Java
    Replies: 1
    Last Post: 07-13-2011, 06:40 AM
  3. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  4. array of Object
    By amit_m04 in forum New To Java
    Replies: 8
    Last Post: 03-09-2009, 03:50 AM
  5. Array of Object
    By haiforhussain in forum New To Java
    Replies: 1
    Last Post: 06-25-2008, 11:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •