我应该将以下合并排序算法跟踪到文件,但是当我测试它时,我在merge方法中不断收到NullPointerException。我不知道为什么或如何解决它。我追踪的其他排序算法都没有这个问题。这个任务构建到另一个,所以我非常感谢这里的一些帮助。
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 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)
是
writer.close();
答案 0 :(得分:1)
看起来你没有设置
private static PrintWriter writer;
merge期望设置它,但在排序中,您只创建一个本地的。
PrintWriter writer = new PrintWriter("Merge_Sort_Trace.txt");
尝试将其更改为:
writer = new PrintWriter("Merge_Sort_Trace.txt");
答案 1 :(得分:0)
你的问题是
writer.close();
您需要将其从排序方法中取出。您的代码在合并方法可以在文件中写入条目之前关闭文件流
我建议您使用其他方法打开PrintWriter
调用递归sort
方法并关闭PrintWriter
答案 2 :(得分:0)
您已经定义了PrintWriter
的全局引用,只需使用它来创建一个编写器并在finally
块中关闭它。