我已经创建了一个按字母顺序对随机生成的字符串数组进行排序的函数。在我的代码的第64行,我总是得到一个NullPointerException,即使我写if语句检查数组是否在某些索引处具有空值。我还确保在main()中创建随机数组,String []数组的索引不应为null。那么问题是什么?这是代码:
/**
This program takes an array of random integers or strings and sorts them with selection sort and Arrays.sort() and determines which method of sorting is faster.
*/
import java.util.Arrays;
public class SortAssess{
/**
This function takes an unsorted integer array and sorts it using the selection sort method.
Precondition: An array of int values.
Postcondition: A sorted array of int values.
@param baseArr This is the array that is to be sorted.
@return The sorted baseArr.
*/
static int[] selectionSort(int[] baseArr){
int lastPlace;
int maxPos;
for(lastPlace = baseArr.length - 1; lastPlace > 0; lastPlace--){//Decreases part of the array to be iterated by one each search cycle.
maxPos = 0;//Index of the largest element in the subarray.
for(int i = 1; i <= lastPlace; i++){
if(baseArr[maxPos] < baseArr[i])
maxPos = i;
}
int temp = baseArr[lastPlace];
baseArr[lastPlace] = baseArr[maxPos];
baseArr[maxPos] = temp;
}
return baseArr;
}
/**
This function takes an unsorted String array and sorts it using the selection sort method.
Precondition: An array of String values.
Postcondition: A sorted array of String values.
@param baseArr This is the array that is to be sorted.
@return The sorted baseArr.
*/
static String[] selectionSort(String[] baseArr){
int lastPlace;
int maxPos;
for(lastPlace = baseArr.length - 1; lastPlace > 0; lastPlace--){//Decreases part of the array to be iterated by one each search cycle.
maxPos = 0;//Index of the largest element in the subarray.
for(int i = 1; i <= lastPlace; i++){
int j = 0;
while(baseArr[i].charAt(j) == baseArr[maxPos].charAt(j)){//NullPointerException
j++;
}
if(baseArr[maxPos].charAt(j) < baseArr[i].charAt(j))
maxPos = i;
}
String temp = baseArr[lastPlace];
baseArr[lastPlace] = baseArr[maxPos];
baseArr[maxPos] = temp;
}
return baseArr;
}
public static void main(String[] args){
int[] randInts = new int[10000];
String[] randStrings = new String[10000];
String randString;//A single string to be added to randStrings, with random characters and length.
long start, end, elapsed;//start of timer, end, and difference.
//assign random values to randInts
for(int i = 0; i < 10000; i++){
randInts[i] = (int)(Math.random() * 100000);
}
//assign random values to randStrings
for(int i = 0; i < 12; i++){
randString = "";//initializes the string.
//Create string at i of random length and with each index a random character
for(int j = 0; j < ((int)(Math.random() * 40) + 5); j++){//creates a random length.
randString += String.valueOf((char)((int)(Math.random() * 26) + 65));//generates ASCII code for character, which is converted to char and added
//to string.
}
randStrings[i] = randString;
System.out.println(randString);
}
start = System.currentTimeMillis();
selectionSort(randInts);
end = System.currentTimeMillis();
elapsed = end - start;
System.out.println("Selection sort of random integers took " + elapsed + " milliseconds.");
start = System.currentTimeMillis();
Arrays.sort(randInts);
end = System.currentTimeMillis();
elapsed = end - start;
System.out.println("Built-in sort of random integers took " + elapsed + " milliseconds.");
start = System.currentTimeMillis();
selectionSort(randStrings);
end = System.currentTimeMillis();
elapsed = end - start;
System.out.println("Selection sort of random strings took " + elapsed + " milliseconds.");
start = System.currentTimeMillis();
Arrays.sort(randStrings);
end = System.currentTimeMillis();
elapsed = end - start;
System.out.println("Built-in sort of random strings took " + elapsed + " milliseconds.");
}
}
答案 0 :(得分:3)
您只是初始化其中的十二个。
//assign random values to randStrings
for(int i = 0; i < 12; i++) {
// loop to deal with string array
}
在处理数组时,应考虑使用randStrings.length
;这样,如果你随心所欲地改变数组的长度,你又不会再遇到这个问题。
//assign random values to randStrings
for(int i = 0; i < randStrings.length; i++) {
// loop to deal with string array
}
也要小心;我确实注意到StringIndexOutOfBoundsException
内的while循环中有一个selectionSort
。仔细检查你是否正在重复你的界限。
答案 1 :(得分:0)
除了@Makoto提供的答案之外,您还有randStrings
声明存储1000
字符串的声明,但您只分配12
个字符串。其余的显然指向null,因此NullPointerException
。
建议
每当你发现自己遇到这样的问题时,就会发表println
语句,以便更好地了解你所获得的情况exception
。用这种方法解决了这个问题。