aspose file tools*
The moose likes Beginning Java and the fly likes Tic-Tac-Toe Array Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Tic-Tac-Toe Array Help" Watch "Tic-Tac-Toe Array Help" New topic
Author

Tic-Tac-Toe Array Help

Jacqueline Turof
Greenhorn

Joined: Apr 18, 2013
Posts: 12
I'm working on creating a program where I can run two different games. I've just started and I'm stuck on just creating a tic-tac-toe board.
When I test to make sure that the board is set up properly it comes back as null. I'm obviously not setting my values for my array properly and was hoping someone had some advice. Thanks in advance!




Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
I added code tags, which you should always use, and also broke some of those long lines, and it looks a lot better. You still have too many blank lines, and you shouldn’t use the tab key for indenting.

Start by designing one game, and you can add other games later. Start designing from the centre and work out towards the periphery, but you code the other way: periphery first. Start by designing boards, maybe, and how you put circles and ×s on it.
Also search this forum; somebody else has a similar problem right now.

Also check that you are not declaring the same thing as a field and as a local variable. Why is your board using ints? You only ever use O/× so why ints?
Jacqueline Turof
Greenhorn

Joined: Apr 18, 2013
Posts: 12
So I've made some changes to my code and I am throwing a NullPointerException and can't figure out what I'm doing wrong.

For my code part of the requirements are that I create a board by making a multidimensional array and that I create a method with the header public String currentBoard() that returns the state of the board as a multi line String. Here is my code so far.



I have a checking class with a main method so that I can make sure things are working as they should.


Any one have any ideas why I'm getting the error NullPointerException? Any help is appreciated!!!
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jacqueline Turof wrote:Any one have any ideas why I'm getting the error NullPointerException?


Because you're doing something equivalent to this:


If you want more help, you'll have to indicate clearly which line is causing the exception.

However, NPEs are among the easiest to diagnose and fix. The stack trace tells you exactly which line it's on, and even what the sequence of method calls is that got you there. Look at that line, see which references you're dereferencing, then work backwards to find which one is null. If you can't figure out which one by looking at the code, then add a bunch of print statements right before the offending line to see what's null.

Any help is appreciated!!!


Please don't shout.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

a line like this:
String [][] board= new String [3][3];

creates an array, but it has nothing in it.

Also..remember that java doesn't have multi-dimensional arrays. it has arrays that hold objects - and those objects can be arrays.

So, after this line, you have an array that can hold three things (which have to be arrays of String), but those things don't exist yet. So you can't call a method on them.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jacqueline Turof
Greenhorn

Joined: Apr 18, 2013
Posts: 12
fred rosenberger wrote:a line like this:
String [][] board= new String [3][3];

creates an array, but it has nothing in it.

Also..remember that java doesn't have multi-dimensional arrays. it has arrays that hold objects - and those objects can be arrays.

So, after this line, you have an array that can hold three things (which have to be arrays of String), but those things don't exist yet. So you can't call a method on them.


I thought I was filling the the array with the board [i][j]= "-"; statement? Thanks for the responses, they've been very informative.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16



Do you actually get to the "board [i][j]= "-"; " line? I would think (and this is a guess) that you get the NPE on line 4 here. you are trying to get the length of an array that doesn't exist.>
Jacqueline Turof
Greenhorn

Joined: Apr 18, 2013
Posts: 12
fred rosenberger wrote:

Do you actually get to the "board [i][j]= "-"; " line? I would think (and this is a guess) that you get the NPE on line 4 here. you are trying to get the length of an array that doesn't exist.>


Thank you for all your help. I was finally able to get it working.
Jacqueline Turof
Greenhorn

Joined: Apr 18, 2013
Posts: 12
So, I'm back. I've been able to create a board object, but now I'm having a new problem. I'm trying to write a play() method using a board object. I am unable to set up a loop that checks for a win. I've added println statements in the program trying to check the loop and see how its actually working, but I still can't tell what is going on. The reason the loops are set up differently for an X horizontal win was for diagnostic purposes. None of the those testing 1,2,3 print ln statements printed to the console and the loops never ended. If anyone has any advice or insight it would be greatly appreciated. Thanks in advance.

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

generally, you don't need to do this:


you can just do



if your code isn't entering loops you think it should, put in more system.outprintln statements. Print what "win" is just before your if-test
 
Consider Paul's rocket mass heater.
 
subject: Tic-Tac-Toe Array Help