I'm programming a code that writes an array of integers to a file. However, it keeps giving me a NullPointerException. Which I think is in one of two methods that I have, I know it's somewhere in there, but I can't spot it. Here's the code.
public static void WriteToFile (int[] n)throws IOException{
try{
BufferedWriter bw=new BufferedWriter(new FileWriter("C:\\sortfile2.txt"));
for(int i=0;i<=25;i++){
bw.write(sortArray(n)+" ");
}
bw.flush();
bw.close();
}
catch(IOException e){
System.out.println("Could not write to file");
}
}
This is the first method that the error could be located in. The second is here:
public static int sortArray(int[] a){
int sort=0;
for(int i=0;i<a.length;i++){
int max=IndexOfMaxInRange(a,i,a.length-1);
sort=SwapElement(a,max,max++);
a[i]=sort;
}
return sort;
}
Where IndexOfMaxInRange
is a method that finds the highest integer of an array, and where SwapElement
is a method that implements IndexOfMaxInRange
, and swaps the highest number in the array, with the lowest.