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. 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.
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
的全局引用,仅使用它来创建书写器并在finally
块中将其关闭。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.