Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Java 2d array problem

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java 2d array problem

    I currently have a 2d array named grid which can store from 0-9 both included. Now if i try to access let's say grid[11][11], the program will give a NullPointerException. I tried this code but to no avail:
    if(grid[r][c] == null){System.out.println("Do nothing")}. I am getting this error: incomparable types: int and <nulltype>. Is there any way to produce the same result with different code?


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Java 2d array problem

    Hello jugi!
    Can you post the complete error messages and the code that produces them?
    I'm asking that because the second error (incomparable types: int and <nulltype>) says you cannot compare an int (primitive) with null (reference type).
    Therefore grid[][] is an int array. But then, there shouldn't be thrown an NullPointerException. A NPE is thrown when you are trying to access something that is null. But grid[11][11] (or any other index) can't be null. The default values of an int array is 0 - not null.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Java 2d array problem

    Please post the code.


    int[] value;
    value = new int[1];

    System.out.println(value[0]);

    That prints 0 despite not being set.

    However, if grid[r][c] = 0 and should be, then we have a problem.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java 2d array problem

    Because of the ways java handles multidimensional arrays, its possible that accessing the second dim can give a NPE. Try this:
          int[][] twoD = new int[2][];           //  Define only the first dim
          System.out.println(twoD[0][0]);    // NullPointerException
    // do the following to fix it
          twoD[0] = new int[2];                   // define the second dim for [0]
          System.out.println(twoD[0][0]);    // Ok
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java 2d array problem

    If for example I try this code: System.out.println(grid[11][11]);
    java.lang.ArrayIndexOutOfBoundsException: 11
    at Board.setPieces(Board.java:33)
    at Window.<init>(Window.java:33)
    at Main.<init>(Main.java:23)
    at Main.main(Main.java:10)
    I know of this problem, since my array is only capable of storing from 0-9.
    But when I try this code:
    if(grid[11][11] == null) {
    System.out.println("Index doesn't exist");
    }
    Java would not compile and gives me this message: incomparable types: int and <nulltype>

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java 2d array problem

    What is the statement definition for the array?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java 2d array problem

    if i understood you well the array is defined as:
    public static int[][] grid;
    public Board() {
    grid = new int[10][10];
    }
    Board is the constructor of my class.

  8. #8
    Junior Member
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java 2d array problem

    Well, to give you some background about the program. It is practically a draughts game, and the game is almost complete. The bit that is giving me trouble is how to make the player eat the enemy's piece if he has the chance. So if for example a white piece is at 5,5. I have to check if at 6,6 exists an enemy piece and at 7,7 is a free space. If these conditions are true then he must jump. The problem comes when for example a white piece is at 8,8 and the loop in which this testing occurs is testing the grid at 10,10 for an empty space, which is clearly wrong since 10,10 doesn't exist.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java 2d array problem

    You should use the array's length attribute to test if the index is out of bounds.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java 2d array problem

    can you give me a quick example please?

  11. #11
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Java 2d array problem

    Here's a loop that would go through the values in your 2D array and print them out. It uses the .length thing.

    for (int i = 0; i < grid.length; i++)
    {
    for (int j = 0; j < grid[i].length; j++)
    {
    System.out.println(grid[i][j]);

    }

    }

Similar Threads

  1. Replies: 4
    Last Post: November 14th, 2011, 10:00 PM
  2. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  3. array problem
    By aizen92 in forum Java Theory & Questions
    Replies: 4
    Last Post: December 18th, 2010, 11:06 AM
  4. Java Webservice problem (Array)
    By Gadge in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 10th, 2010, 11:06 AM
  5. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 04:30 AM