[英]NullPointerException when trying to write to file
I'm supposed to trace the following merge sort algorithm to a file but I keep getting a NullPointerException in the merge method when I test it. 我应该将以下合并排序算法跟踪到文件,但是在测试它时,我在合并方法中始终收到NullPointerException异常。 I don't know why or how to fix it.
我不知道为什么或如何解决它。 None of the other sort algorithms I traced had this issue.
我跟踪的其他排序算法都没有这个问题。 This assignment builds into another so I'd really appreciate some help here.
这项任务又加入了另一个任务,因此我非常感谢您的帮助。
import java.io.*;
public class MergeSortWithTrace
{
private static PrintWriter writer;
/** Sort the array using the merge sort algorithm
pre: table contains Comparable objects
post: table is sorted
@param table The array to be sorted
*/
public static <T extends Comparable<T>> void sort(T[] table)
{
try
{
PrintWriter writer = new PrintWriter("Merge_Sort_Trace.txt");
// a table with one element is sorted already
if (table.length > 1)
{
// Split the table into halves.
int halfSize = table.length / 2;
T[] leftTable = (T[]) new Comparable[halfSize];
T[] rightTable =
(T[]) new Comparable[table.length - halfSize];
System.arraycopy(table, 0, leftTable, 0, halfSize);
System.arraycopy(table, halfSize, rightTable, 0, table.length - halfSize);
writer.print("\nSplit the array in two...");
writer.print("\nThe left array: ");
for (int i = 0; i < leftTable.length; i++)
{
writer.print(" " + leftTable[i]);
}
writer.println("\nThe right array: ");
for (int i = 0; i < rightTable.length; i++)
{
writer.print(" " + rightTable[i]);
}
// Sort the halves
sort(leftTable);
sort(rightTable);
// merge the halves
merge(table, leftTable, rightTable);
}
writer.close();
}
catch (FileNotFoundException fnfe)
{
writer.println(fnfe.getMessage());
}
}
/** merge two sequences.
pre: leftSequence and rightSequence are sorted
post: outputSequence is the merged result and is sorted.
@param outputSequence The destination
@param leftSequence The left input
@param rightSequence The right input
*/
private static <T extends Comparable<T>> void merge(T[] outputSequence,
T[] leftSequence,
T[] rightSequence)
{
int i = 0; // Index into the left input sequence
int j = 0; // Index into the right input sequence
int k = 0; // Index into the output sequence
// While there is data in both input sequences
while (i < leftSequence.length && j < rightSequence.length)
{
// Find the smaller and insert it into the output sequence
if (leftSequence[i].compareTo(rightSequence[j]) < 0)
{
outputSequence[k++] = leftSequence[i++];
}
else
{
outputSequence[k++] = rightSequence[j++];
}
}
// assert: one of the sequences has more items to copy
// copy remainings input from left sequence into the output.
while (i < leftSequence.length)
{
outputSequence[k++] = leftSequence[i++];
}
// Copy remaining input from right sequence into the output.
while (j < rightSequence.length)
{
outputSequence[k++] = rightSequence[j++];
}
writer.println("\nThe sorted sequence: ");
for (int z = 0; z < outputSequence.length; z++)
{
writer.print(" " + outputSequence[z]);
}
}
}
Exception trace 异常跟踪
Exception in thread "main" java.lang.NullPointerException
at MergeSortWithTrace.merge(MergeSortWithTrace.java:81)
at MergeSortWithTrace.sort(MergeSortWithTrace.java:39)
at MergeSortWithTrace.sort(MergeSortWithTrace.java:35)
at MergeSortWithTrace.main(MergeSortWithTrace.java:89)
which is 这是
writer.close();
It looks like you aren't setting 看来您没有设定
private static PrintWriter writer;
merge expects this to be set, but in sort, you only create a local one. merge希望对此进行设置,但在排序上,您只能创建一个本地的。
PrintWriter writer = new PrintWriter("Merge_Sort_Trace.txt");
trying changing it to: 尝试将其更改为:
writer = new PrintWriter("Merge_Sort_Trace.txt");
Your problem is 你的问题是
writer.close();
You need to take it out of sort method. 您需要使它脱离排序方法。 Your code closes file stream before merge method could write entries in the file
您的代码在合并方法可以在文件中写入条目之前关闭文件流
I suggest you have another method which opens PrintWriter
calls recursive sort
method and closes PrintWriter
我建议您使用另一种方法来打开
PrintWriter
调用递归sort
方法并关闭PrintWriter
您已经定义了PrintWriter
的全局引用,仅使用它来创建书写器并在finally
块中将其关闭。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.