• Post Reply Bookmark Topic Watch Topic
  • New Topic

Advanced programming logic  RSS feed

 
Lee High
Greenhorn
Posts: 22
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello This is my first time to the ranch. Looking for some help with my project.
What this program is to do is write three txt files that are game boards I created in the first
leg of this project. I've followed the instructions and think my program is correct. but when I compile,
it gives me the following error. Exception in thread "main" java.lang.NullPointerException
at boardgametester.BoardGameTester.main(BoardGameTester.java:34)
C:\Users\Dave&Rosa_Hample\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds). Could anybody help me resolve this problem. Many Thanks,






 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I can see all sorts of problems with that code. You are declaring fields and local variables with the same name. You have three fields declared as type Object; they are bound to be a different type, so ahy declare them as Object? You have lots of things marked static: why? Why are the three kinds of board instances of the same class? They don't look at all similar in real life.

Line 34 has a Board instance which has already been instantiated. Is CROSS a field of Mark? Is Mark an enum? How does Mark work? If it is an enum, then CROSS can't be null. Please show more details about Mark. Maybe CROSS returns something else which is null, or maybe your line numbers have been changed, or maybe something different.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This problem isnt related to IO and Streams so I shall move this to the Beginners Forum
 
Lee High
Greenhorn
Posts: 22
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your feedback. My program worked great as it shows three separate game boards one for connect4 with a cell set  to red, a tic tact toe with a cell set to cross, and last a mastermind with a cell set to green.
Now in the second leg of this project I have to place these game board displays into a three separate text file(ttt.txt,c4.txt, and mm.txt). I am not sure if I need to remove some of my previous code, but I have added the below code and that's what's giving me my errors. I also added the games.utilities package and placed a FileManager class into it to help with this project, but does seam to be working correctly.
To answer your one question the marks are enum. As for the static, it was the only way I could get my program to work in the first project. I agree I don't think that I need to declare these fields as Objects. I'm really new at this and it is quite confusing. Your responses are greatly appreciated.

 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but I have added the below code and that's what's giving me my errors.

Please TellTheDetails (← click).
We don't know know what errors you are seeing unless you tell us and the best way to do that is to copy and paste the error message and stack trace into your post.
 
Lee High
Greenhorn
Posts: 22
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry The error was in my first post but here is it below. I am trying to work through the error and it is diffidently the code in my last posting that causing me the problem.


the error I'm getting

 
Lee High
Greenhorn
Posts: 22
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured out the problem and now my code works without errors. The only problem and I know it's my misunderstanding of it, is I have created three txt files for each game board but the files are empty and they should display the three game boards. Not sure how to pull it from my code and have it convert to my txt files? Here is my updated code. Thanks again!



game.utilities package

 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your problem is because you are not flushing/closing the file after writing to it.
you should always close resources after you have finished using them and the easiest way is to use the try-with-resources statement - see: https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html.

BTW Thank you for trying to use code tags but you are not using them correctly. I have fixed your previous posts for you but please read UseCodeTags (← click) on how to use them properly.
 
Lee High
Greenhorn
Posts: 22
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again for your help. I read through the link you posted and did what I thought was the correct code. But when I check my text files there still empty. Here is the code I put in.

 
Lee High
Greenhorn
Posts: 22
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please disregard my last post. I got the program to work perfectly. Mybe there's hope for me yet. Thanks again for all your help.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done, good to hear you've got your code working.
You still aren't using code tags correctly though (I've fixed your post again), please read the link I provided on how to use them properly.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!