Null pointer when calling selectionSort?
I'm writing a program that offers the user two options:
1. Create a file
2. Search a file
When creating a file, I prompt the user for a filename and then have the user enter strings of names until the user enters a sentinel value("end").Before writing to the file, I'm trying to sort the strings using a selectionSort algorithm, I'm trying to write out the algorithm to learn the workings behind it instead of using a java library.
When searching the File, I prompt the user for the filename and the search name. Then I use a binary search to determine whether or not the item is in the file.
I've been working on this for a couple weeks and I've re-written the selection sort a couple dif ways to continue to run into the nullpointer exception when i call SelectionSort in the main program.
Below is the code I've written. I'm not looking for anyone to do my homework, i've just had a lot of trouble with this and i'm at my wits end.Can anyone help? Or at least point me in the right direction?
package assignment1;
import java.io.*;
public class Assignment1 {
public static void main(String[] names)throws IOException {
BufferedReader keyboard = null;
String userChoice;
String inputFile = null;
String studentData;
String searchFile = null;
String searchName;
String stringIn;
PrintWriter outputFile;
FileWriter fWriter = null;
BufferedReader fReader = null;
int first;
int last;
int mid;
int midValue;
int i;
int number;
// creates keyboard as a buffered input stream
keyboard = new BufferedReader(new InputStreamReader(System.in));
//prompts user to choose 1 or 2 to make a corresponding choice
System.out.println("Please Enter: ");
System.out.println("1 to Create a File: ");
System.out.println("2 to Search a File: ");
userChoice = keyboard.readLine();//user enters 1 or 2
// converts a String into an int value
number = Integer.parseInt(userChoice);
fReader = new BufferedReader(new FileReader("studentData.txt"));
if (number == 1) {
studentData = keyboard.readLine();
System.out.println("Please Enter the File Name to Create: ");
fWriter = new FileWriter("studentData.txt");
names = new String[200];
while (studentData != "end") {//keep looping till sentinel
studentData = keyboard.readLine();
if (studentData.equals("end")) break;//break and call sort
System.out.println("Enter a name and press Enter. " +
"Type 'end' and press Enter when done: ");
//loop for putting the names into the array
for(i=0; i<names.length; i++) ;
}
//call selectionSort() to order the array
selectionSort(names);
// Now output to a file.
fWriter = new FileWriter("studentData.txt");
//File file = new File("studentData.txt");
} else if (number == 2) {
System.out.println("Please Enter a File Name to search: ");
searchFile = keyboard.readLine();
inputFile = ("studentData.txt");
}if (searchFile == "studentData.txt") {
// Input from a file. See input file streams.
fReader = new BufferedReader(new FileReader("studentData.txt"));
System.out.println("Please enter a Name to search for: ");
searchName = keyboard.readLine();
//enter binary search code
first = 0;
last = 199;
while (first >< last)
{
mid = (first + last)/2; // Compute mid point.
if (searchName.compareTo(names[mid]) < 0) {
last = mid; // repeat search in bottom half.
}else if (searchName.compareTo(names[mid]) > 0) {
first = mid + 1; // Repeat search in top half.
}else {
// Found it.
System.out.println("The Name IS in the file.");
}
}// did not find it.
System.out.println("The Name IS NOT in the file.");
} else //if userChoice != 1 or 2, re-prompt then start over
System.out.println("Please Enter 1 or 2 or correctly " +
"enter an existing file!!");
// fWriter = new FileWriter("studentdata.txt");
//outputFile = new PrintWriter(fWriter);//output
}
public static void selectionSort(String[] names) {
//use compareTo!!!!
int smallIndex;
int pass, j = 1, n = names.length;
String temp;
for (pass = 0; pass < n-1; pass++)
{
//Code for Do/While Loop
do {
//scan the sublist starting at index pass
smallIndex = pass;
//jtraverses sublist names[pass+1] to names[n-1]
for (j = pass+1; j < n; j++)
//if smaller string found, smallIndex=that position
if (names[j].compareTo(names[smallIndex]) < 0)
smallIndex = j;
temp = names[pass];//swap
names[pass] =names[smallIndex];
names[smallIndex] = temp;
} while (j <= names.length);
//File file = new File("studentData.txt");
}
}
}

