• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Tic-Tac-Toe Array Help

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!




 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!!
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jacqueline Turof
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic