[英]Why am I getting a NullPointerException with an array of Strings?
我做了一个函数,可以按字母顺序对随机生成的字符串数组进行排序。 在我的代码的第64行上,即使我写if语句检查数组在某些索引处是否具有null值,我也始终会得到NullPointerException。 我还确保在创建随机数组的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.");
}
}
您只初始化了十二个。
//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
}
在那里也要小心; 我确实在selectionSort
while循环中注意到StringIndexOutOfBoundsException
。 仔细检查您是否在边界上进行迭代。
除了由@Makoto提供的答案,你哈瓦一个声明randStrings
存储1000
字符串,但你只分配12
人。 其余的显然指向null,因此指向NullPointerException
。
一个建议
无论何时,只要发现自己遇到此类问题,都可以使用println
语句来更好地了解您遇到exception
。 用这种方法可以解决一半问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.