• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

NullPointerException - Trying to print arrays to text files

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to print three game boards to a text file. Could you please point me in the right direction? (No pun intended:) I have marked the lines of code referenced in the errors.

Error:
run:
Tic Tac Toe game board
Connect Four game board
Mastermind game board
Exception in thread "main" java.lang.NullPointerException
at games.board.Board.toString(Board.java:40)
at boardgametester.BoardGameTester.main(BoardGameTester.java:45)
C:\Users\Chris\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 1 second)

Board.java


BoardGameTester.java
 
Rancher
Posts: 3418
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Exception in thread "main" java.lang.NullPointerException
at games.board.Board.toString(Board.java:40)  


Look at line 40 and find the variable with the null value.  Then backtrack in the code to see why that variable does not have a valid value.
You might need to print the values of the variables: "r" and c to see where they refer.

Also the Arrays class's deepToString() method is useful for showing the contents of a 2 dim array for debugging:

NOTE to forum moderators:  I was unable to refer to the variable "r" without the "s.  The forum software wanted to see "are" not the "r" without quotes.
 
Christopher.T Brown
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:


Exception in thread "main" java.lang.NullPointerException
at games.board.Board.toString(Board.java:40)  


Look at line 40 and find the variable with the null value.  Then backtrack in the code to see why that variable does not have a valid value.
You might need to print the values of the variables: "r" and c to see where they refer.

Also the Arrays class's deepToString() method is useful for showing the contents of a 2 dim array for debugging:

NOTE to forum moderators:  I was unable to refer to the variable "r" without the "s.  The forum software wanted to see "are" not the "r" without quotes.



Thanks! I was referencing the "r" counter to process the c counter. I no longer experience build errors, but it is not generating the output files. I am attempting to do start a new thread in FileManager and create the output files. The FileManager code is as follows:

 
Norm Radder
Rancher
Posts: 3418
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

it is not generating the output files.  


Are the statements on line 30 and 31 being executed?

Is the problem that the files are created but they are empty or are the files not being created?
 
Christopher.T Brown
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

it is not generating the output files.  


Are the statements on line 30 and 31 being executed?

Is the problem that the files are created but they are empty or are the files not being created?



Hey Norm,

The files are not being created. I inserted a System.out.println(line) in the try block, and I did not receive any output.

Chris
 
Norm Radder
Rancher
Posts: 3418
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The files are not being created. I inserted a System.out.println(line) in the try block, and I did not receive any output.


That would say the code is not being executed.  Check the code where the methods are supposed to be called to make sure they are being called.
 
Bartender
Posts: 6140
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this piece of code
you may not be iterating through the correct number of columns if rows!=columns.

Try
 
Norm Radder
Rancher
Posts: 3418
33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From line 4:

Note: Using the length of the first row (cells[0].length() ) assumes that all the rows have the same length.

It might be better to use the code from line 39 that uses the length of the current row:
 
Christopher.T Brown
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:From line 4:

Note: Using the length of the first row (cells[0].length() ) assumes that all the rows have the same length.

It might be better to use the code from line 39 that uses the length of the current row:



I've made a tad bit of progress. The errors are gone. The text files now get created. But, the files are empty. I pass the array(Board) to the method, writeToFile, as a string. All I need it to do is print to the text file, but it looks as though the BufferedWriter is null. Any advice. Ya'lls help thus far has been invaluable. Thanks!

BoardGameTester:


FileManager:


 
Norm Radder
Rancher
Posts: 3418
33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The text files now get created. But, the files are empty.  


What is written to the console when the program executes?

There needs to be more info printed on line 32.  Add fileName and saveState to the println() statement so you can see what the code sees when it executes.

I don't see a close() so the buffer isn't being written to disk.
Also see http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
 
Christopher.T Brown
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

The text files now get created. But, the files are empty.  


What is written to the console when the program executes?

There needs to be more info printed on line 32.  Add fileName and saveState to the println() statement so you can see what the code sees when it executes.



Hey Norm. Everything is passed as it should be:

File written
ttt.txt


File written
cf.txt


File written
mm.txt


BUILD SUCCESSFUL (total time: 0 seconds)
 
Norm Radder
Rancher
Posts: 3418
33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the end of my last post.

http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
 
Christopher.T Brown
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

The text files now get created. But, the files are empty.  


What is written to the console when the program executes?

There needs to be more info printed on line 32.  Add fileName and saveState to the println() statement so you can see what the code sees when it executes.

I don't see a close() so the buffer isn't being written to disk.
Also see http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html



Norm, it worked!! Thank ya!
 
Let nothing stop you! Not even this tiny ad:
professionally read, modify and write PDF files from Java
https://products.aspose.com/pdf/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!