#Java Error With Outputting Red Null to Console???

1 messages · Page 1 of 1 (latest)

abstract field
#

I am getting the error attatched above.
My code is as follows:

        int totalMoves = DDM(0, visited, pathWays, barnHay, pw);
        PrintWriter nw = new PrintWriter(new FileWriter("barnTree.out"));
        nw.println(totalMoves);
        nw.flush();
        pw.flush();
dusky perchBOT
#

<@&987246399047479336> please have a look, thanks.

dusky perchBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

abstract field
#

My totalMoves is a number
I use PrintWriter to output it to barnTree.out
nw is another PrintWriter, which already has a few print statements
I use both NW and PW to write to barnTree.out

When I do that, I get a bunch of NULL stuff

#

AGH

#

I can flush Either PW or NW, and I don't get an error, but if I flush BOTH, I get the very annoying NULL values

shrewd wasp
#

please use NIO for file operations

onyx sandal
#

what is PW?

dusky perchBOT
#

File IO in Java should be done preferably with NIO (Java 7+), revolving around the classes Files and Path; and not with the old interface File, BufferedReader, FileReader and similar.

NIO is simple to use. The path to a file is represented using the Path class:

Path path = Path.of("myFile.txt");

All file operations can be found in the Files class:

// Reading
List<String> allLines = Files.readAllLines(path);
// or as a single string
String content = Files.readString(path);
// or with a stream
try (Stream<String> stream = Files.lines(path)) {
  stream.forEach(System.out::println);
}

// Writing
Files.write(path, lines);
// or as a single string
Files.writeString(path, "hello world");
// or with extra options
Files.writeString(path, "hello world",
  StandardOpenOption.WRITE,
  StandardOpenOption.CREATE,
  StandardOpenOption.APPEND);

If you need more control over the process, you can fallback to the old interface, but prefer using the bridge methods from NIO (Files.newBufferedReader, Files.newInputStream, path.toFile() and similar) to benefit from advantages such as correct encoding and better error detection.

Here is a simple example of how to read a file line-wise with the old interface

try (BufferedReader br = Files.newBufferedReader(path)) {
  String line;
  while ((line = br.readLine()) != null) {
    System.out.println(line);
  }
}

it is way more verbose than NIO but it gives more control.

You must not forget to close file handles, even in all exceptional cases. Closing a handle manually is very hard, which is why you should always use try-with-resources for this to let Java automatically close the handle for you:

try (SomeResource resource = ...) {
  ...
} // Automatically closed here, even in exceptional cases
languid ferry