java.lang.nullpointerexception error in array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zahra abaszade
    New Member
    • Apr 2012
    • 5

    java.lang.nullpointerexception error in array

    with this code I get a java.lang.NullP ointerException :

    Code:
    public class TicTacToe
    {
        private int count=0;
        private String letter;
        private int[][]myArray;
     public TicTacToe()
        {
            
           int[][] myArray = new int[3][3];
           for(int i=0;i>3;i++){
               for(int j=0;j>3;j++){
               myArray[i][j]=0;
            }
            }
            
        }
      public void nextMove(int s1,int s2){
            myArray[s1][s2]=1;//error is here
            count++;
           
            if(count==1||count==3||count==5||count==7||count==9){
                letter="O";
                
            }
            if(count==2||count==4||count==6||count==8){
                letter="X";
                
            }
            
           System.out.println("button"+s1+","+s2+":"+letter);
        
           
        }
    }
    Last edited by Meetee; Apr 3 '12, 06:17 AM. Reason: code tags added
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    In your constructor code for-loop, on line 10 you have:
    Code:
    for(int i=0;i>3;i++)
    This won't work...i will never be greater than 3.
    You have similar code on line 11...

    Change the check for i greater than 3 to i less than 3.

    Edit: the reason you're getting the error is because your array isn't be initialized properly due to the problem in your loops.

    -Frinny
    Last edited by Frinavale; Apr 4 '12, 01:10 PM.

    Comment

    • neeraj0708
      New Member
      • Feb 2012
      • 21

      #3
      You need to declare array properly....
      in your nextMove method it is not initialized properly... check it.

      Code:
          public class TicTacToe
          {
              private int count=0;
              private String letter;
              private int[][]myArray;
      private  int[][] myArray = new int[3][3];
      //declare array here so that it will be visible in your //whole class
      
           public TicTacToe()
              {
           
                 int[][] myArray = new int[3][3];//remove it
                 for(int i=0;i>3;i++){
                     for(int j=0;j>3;j++){
                     myArray[i][j]=0;
                  }
                  }
           
              }
            public void nextMove(int s1,int s2){
                  myArray[s1][s2]=1;//error is here
                  count++;
           
                  if(count==1||count==3||count==5||count==7||count==9){
                      letter="O";
           
                  }
                  if(count==2||count==4||count==6||count==8){
                      letter="X";
           
                  }
           
                 System.out.println("button"+s1+","+s2+":"+letter);
           
           
              }
          }

      Comment

      • zahra abaszade
        New Member
        • Apr 2012
        • 5

        #4
        thank you very much for your best answer!!

        Comment

        • zahra abaszade
          New Member
          • Apr 2012
          • 5

          #5
          thank you frinavale!!

          Comment

          Working...