这个程序是一个小型的联系人经理。我在调用sortContacts方法时遇到问题。每次调用方法时,它都会给我一个nullpointerexception。我试图让方法从A-Z排序联系人,但由于某种原因,数组似乎是空的?它们在loadContacts中加载的文本文件中包含条目,并且在调用displayContacts时打印完全正确(带索引号)。
我尝试过使用ArrayLists无济于事。它似乎只是比较数组中的字符串将永远不会工作?
Exception in thread "main" java.lang.NullPointerException
at Contacts.sortContacts(Contacts.java:179)
at Contacts.menu(Contacts.java:47)
at Contacts.main(Contacts.java:25)
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Contacts {
//global variables
public static int counter = 0;
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static int x;
static String[] name = new String[101];
static String[] number = new String[101];
public static void main(String[] args) throws IOException {//main method, sends user to the menu method
menu();
}
//The menu method displays the option menu and compares user input to a menu option
public static void menu() throws IOException{
int choice = -1;
while (choice != 7){
System.out.println("Main Menu");
System.out.println("1. View all Contacts\n2. Add Entry\n3. Modify Entry\n4. Delete Entry\n5. Save\n6. Load\n7. Sort Conacts\n8. Exit");
choice = Integer.valueOf(in.readLine()).intValue();
switch(choice){
case 1: displayContacts();
break;
case 2: addContact();
break;
case 3: modifyContact();
break;
case 4: deleteContact();
break;
case 5: saveContact();
break;
case 6: loadContact();
break;
case 7: sortContacts(name);
break;
default:System.exit(0);//terminates the program (quit)
}
}
}
private static void displayContacts() throws IOException {
System.out.println(counter);
for(int i = 0; i < counter; i++){
if(name != null) {//this loops prevents the program from printing out empty contact slots
System.out.println("Contact " + i + ": " + name[i] + " | " + number[i]);
}
else{
System.out.print("");
}
}
}
public static void addContact() throws IOException {
//check the counter variable
int print;
BufferedReader scan = new BufferedReader(new FileReader("contacts.txt"));
print = Integer.valueOf(scan.readLine()).intValue();
print = counter;
if (counter < 101){
//prompt the user for information
System.out.println("Enter the Contact's name:");
name[counter] = in.readLine();
System.out.println("Enter the Contact's number");
number[counter] = in.readLine();
//increment the counter
counter++;
} else {
System.out.println("Sorry the array is full!");
}
scan.close();
}
public static void modifyContact() throws IOException{
boolean found = false;
int flag = 0;
String modCon;
System.out.println("Enter the name of the contact you wish to modify:");
modCon = in.readLine();
for (int i = 0; i < counter; i++){
if (modCon.compareTo(name[i]) == 0){
flag = i;
found = true;
}
}
if (found == false){
System.out.println("Item not found");
}
else{
System.out.println("Contact found!");
System.out.println("Enter new name: ");
name[flag] = in.readLine();
System.out.println("Enter new number: ");
number[flag] = in.readLine();
}
}
private static void deleteContact() throws IOException {
boolean found = false;
int flag = -1;
String delCon;
System.out.println("Enter the name of the contact you wish to delete:");
delCon = in.readLine();
for(int i = 0; i < counter; i++){
if(delCon.compareTo(name[i]) == 0) {
flag = i;
found = true;
}
}
if(found == false){
System.out.println("Item not found");
}
else{
if (flag == counter - 1){
name[flag] = "";
number[flag] = "";
counter--;
} else{
for (int i = flag; i < counter; i++){
name[i] = name[i + 1];
number[i] = number[i + 1];
}
}
}
counter = flag + 1;
}
public static void saveContact() throws IOException{
//Create File output
PrintWriter output;
output = new PrintWriter(new FileWriter("contacts.txt"));
//Output a header to the file, in this case it is the number of criminals
output.println(counter);
//loop through our array outputting to a file
for(int i = 0; i < counter; i++){
System.out.println("Writing Contact to file");
output.println(name[i]);
output.println(number[i]);
}
output.close();
}
public static void loadContact() throws IOException {
int print;
BufferedReader in = new BufferedReader(new FileReader("contacts.txt"));
print = Integer.valueOf(in.readLine()).intValue();
counter = print; // tells the counter where to continue from (if loading a file)
System.out.println("Loading " + print + " Contacts");
for (int i = 0; i < print; i++){
//read in the data
name[i] = in.readLine();
number[i] = in.readLine();
}
in.close();
}
public static void sortContacts(String[] a) throws IOException{
//counters i and j
int i = 0;
int j = 0;
//"smallest" flag"
int smallest = 0;
//temporary holder for swapping
String temp;
//Selection Sort
for (i = 0; i < 100; i++){
smallest = i;
for (j = i; j < 10; j++){
//compare the current smallest with the current array element
if (name[j].compareTo(name[smallest]) < 0){
smallest = j;
}
//swap the smallest
temp = name[i];
name[i] = name[smallest];
name[smallest] = temp;
}
}
}
}
答案 0 :(得分:1)
阅读本文以了解您的例外含义:What is a NullPointerException, and how do I fix it?
在您的情况下,如果我在菜单中选择案例7,则在您调用name
之前未初始化sortContacts
,因此默认情况下每个索引都等于null
,因为它是一个字符串阵列。因此,在sortContacts
尝试name[j].compareTo(name[smallest]
之类的内容时,您会得到空指针异常,因为name[j]
等于null
。
要解决此问题,您需要确保使用文件中的值填充数组,然后选择调用sortContacts
。