By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,806 Members | 1,363 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,806 IT Pros & Developers. It's quick & easy.

java.lang.nullpointerexception error in array

P: 5
with this code I get a java.lang.NullPointerException:

Expand|Select|Wrap|Line Numbers
  1. public class TicTacToe
  2. {
  3.     private int count=0;
  4.     private String letter;
  5.     private int[][]myArray;
  6.  public TicTacToe()
  7.     {
  8.  
  9.        int[][] myArray = new int[3][3];
  10.        for(int i=0;i>3;i++){
  11.            for(int j=0;j>3;j++){
  12.            myArray[i][j]=0;
  13.         }
  14.         }
  15.  
  16.     }
  17.   public void nextMove(int s1,int s2){
  18.         myArray[s1][s2]=1;//error is here
  19.         count++;
  20.  
  21.         if(count==1||count==3||count==5||count==7||count==9){
  22.             letter="O";
  23.  
  24.         }
  25.         if(count==2||count==4||count==6||count==8){
  26.             letter="X";
  27.  
  28.         }
  29.  
  30.        System.out.println("button"+s1+","+s2+":"+letter);
  31.  
  32.  
  33.     }
  34. }
Apr 3 '12 #1

✓ answered by Frinavale

In your constructor code for-loop, on line 10 you have:
Expand|Select|Wrap|Line Numbers
  1. 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

Share this Question
Share on Google+
4 Replies


Frinavale
Expert Mod 5K+
P: 9,731
In your constructor code for-loop, on line 10 you have:
Expand|Select|Wrap|Line Numbers
  1. 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
Apr 3 '12 #2

P: 21
You need to declare array properly....
in your nextMove method it is not initialized properly... check it.

Expand|Select|Wrap|Line Numbers
  1.     public class TicTacToe
  2.     {
  3.         private int count=0;
  4.         private String letter;
  5.         private int[][]myArray;
  6. private  int[][] myArray = new int[3][3];
  7. //declare array here so that it will be visible in your //whole class
  8.  
  9.      public TicTacToe()
  10.         {
  11.  
  12.            int[][] myArray = new int[3][3];//remove it
  13.            for(int i=0;i>3;i++){
  14.                for(int j=0;j>3;j++){
  15.                myArray[i][j]=0;
  16.             }
  17.             }
  18.  
  19.         }
  20.       public void nextMove(int s1,int s2){
  21.             myArray[s1][s2]=1;//error is here
  22.             count++;
  23.  
  24.             if(count==1||count==3||count==5||count==7||count==9){
  25.                 letter="O";
  26.  
  27.             }
  28.             if(count==2||count==4||count==6||count==8){
  29.                 letter="X";
  30.  
  31.             }
  32.  
  33.            System.out.println("button"+s1+","+s2+":"+letter);
  34.  
  35.  
  36.         }
  37.     }
  38.  
  39.  
Apr 3 '12 #3

P: 5
thank you very much for your best answer!!
Apr 4 '12 #4

P: 5
thank you frinavale!!
Apr 4 '12 #5

Post your reply

Sign in to post your reply or Sign up for a free account.