Java 尝试写入文件时出现NullPointerException
我应该将下面的合并排序算法跟踪到一个文件,但在测试合并方法时,我一直在合并方法中得到一个NullPointerException。我不知道为什么或如何修复它。我追踪的其他排序算法都没有这个问题。这项任务包含了另一项任务,因此我非常感谢您的帮助Java 尝试写入文件时出现NullPointerException,java,sorting,merge,nullpointerexception,Java,Sorting,Merge,Nullpointerexception,我应该将下面的合并排序算法跟踪到一个文件,但在测试合并方法时,我一直在合并方法中得到一个NullPointerException。我不知道为什么或如何修复它。我追踪的其他排序算法都没有这个问题。这项任务包含了另一项任务,因此我非常感谢您的帮助 import java.io.*; public class MergeSortWithTrace { private static PrintWriter writer; /** Sort the array using the merge sort
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]);
}
}
}
那是
writer.close();
看起来你没有设置
private static PrintWriter writer;
merge希望设置此项,但在sort中,您只创建一个本地项
PrintWriter writer = new PrintWriter("Merge_Sort_Trace.txt");
正在尝试将其更改为:
writer = new PrintWriter("Merge_Sort_Trace.txt");
你的问题是
writer.close();
你需要把它从排序方法中去掉。在merge方法可以在文件中写入条目之前,代码关闭文件流
我建议您使用另一种方法打开
PrintWriter
调用递归sort
方法并关闭PrintWriter您已经定义了PrintWriter
的全局引用,只需使用它创建一个writer并在最后关闭它。
哪一行抛出NPE?
writer.close();