473,397 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

java.lang.nullpointerexception error in array

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

4 2339
Frinavale
9,735 Expert Mod 8TB
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
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
thank you very much for your best answer!!
Apr 4 '12 #4
thank you frinavale!!
Apr 4 '12 #5

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

Similar topics

12
nomad
by: nomad | last post by:
Hi everyone; My Class has ended and I was not able to solve this problem in time, and I would still like to solve it. I got these error code. Exception in thread "main"...
3
sammyboy78
by: sammyboy78 | last post by:
Hello again, This time I'm creating a program that creates an array of CD objects and then displays the information. I've created a CD class, a CDInventory class and then a CDInventoryDisplay to...
0
by: gezkk | last post by:
hi, i m using http proxy service to get proxy and host to applet, its working fine with jdk1.5 but getting java.lang.NullPointerException in jdk1.6 And the following is mseeage contains java...
2
by: chokcheese | last post by:
I'm having trouble with a java application. When I try and run the program it shows a java.lang.NullPointerException in the "tic.getClient().getClientTicketList().add(tic);" line (it's in bold). I...
1
by: ksrashmi | last post by:
HI I am getting following exception when i use mxml tags in jsp <%@ taglib uri="FlexTagLib" prefix="mm" %> can any one tell why i am getting this exception type Exception report message...
2
by: lilyumestar | last post by:
This project is due by Tuesday and I haven't even gotten half of it done. Can anyone please help me with this Exception error? I've been trying to figure it out for several hours Error Message ...
3
by: ohadr | last post by:
hi, i get Exception in thread "main" java.lang.NullPointerException when i run my application. the exact error is: "Exception in thread "main" java.lang.NullPointerException at...
3
by: makweatan | last post by:
Hello guys... Data is not updated into table. I got this error : java.lang.NullPointerException 00000030 SystemErr R java.lang.NullPointerException 00000030 SystemErr R at...
0
mickey0
by: mickey0 | last post by:
Hello, I have my class file 'MyClass'; I would like compile it from my application; I wrote it but I got a java.lang.NullPointerException Any reasons for this? thanks. JavaCompiler compiler =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.