本文介绍了为什么我的排序实现中有NullPointerException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在完成我的任务,我想为我的数组创建一个排序方法。但是,我的排序方法中出现空指针错误,我不知道为什么。这是我的代码。
I'm working on my assignment and I am suppose to create a sort method for my array. However, I'm getting a null pointer error in my sort method and I'm not sure why. This is my code below.
public Object[] sorted() {
getSetArray();
Arrays.sort(setArray);
return setArray;
}
package setadt;
import java.util.*;
public class SetADT {
private static Random rand = new Random();
private final int DEFAULT_CAPACITY = 100;
private final int NOT_FOUND = -1;
private int size;
private Object[] setArray;
public SetADT() {
size = 0;
setArray = new Object[DEFAULT_CAPACITY];
}
public SetADT(int capacity) {
size = 0;
setArray = new Object[capacity];
}
public void add(Object newElement) {
if (size() == setArray.length) {
resize();
}
if(!contains(newElement) == true){
setArray[size] = newElement;
size++;
}
//System.out.println(newElement +"\t" + size);
}
public void addAll(SetADT set) {
Object[] temp = set.getSetArray();
for (int i = 0; i < set.size(); i++) {
if (!this.containsValue(temp[i])) {
add(temp[i]);
}
}
}
public boolean contains(Object element) {
for (int i = 0; i < size; i++) {
if (setArray[i].equals(element)) {
return true;
}
}
return false;
}
public boolean containsValue(Object element) {
for (int i = 0; i < size; i++) {
if (setArray[i].toString().equals(element.toString())) {
return true;
}
}
return false;
}
public boolean equals(SetADT set) {
boolean result = false;
Object element;
if (size() == set.size()) {
result = true;
for (int i = 0; i < size(); i++) {
element = setArray[i];
if (!set.contains(element)) {
return false;
}
}
}
return result;
}
public boolean isEmpty() {
if (size == 0) {
return true;
} else {
return false;
}
}
public Object remove(Object element) {
Object result = new Integer(NOT_FOUND);
int search = NOT_FOUND;
if (!isEmpty()) {
for (int i = 0; i < size() && search == NOT_FOUND; i++) {
if (setArray[i].equals(element)) {
search = i;
}
}
if (search != NOT_FOUND) {
result = setArray[search];
setArray[search] = setArray[size - 1];
setArray[size - 1] = null;
size--;
}
}
return result;
}
public Object removeRandom() {
int element;
Object value = new Integer(NOT_FOUND);
if (!isEmpty()) {
element = rand.nextInt(size);
value = setArray[element];
setArray[element] = setArray[size - 1];
setArray[size - 1] = null;
size--;
}
return value;
}
public int size() {
return size;
}
public SetADT union(SetADT set) {
SetADT newSet = new SetADT();
newSet.addAll(this);
newSet.addAll(set);
return newSet;
}
private void resize() {
Object[] newArray = new Object[setArray.length * 2];
for (int i = 0; i < setArray.length; i++) {
newArray[i] = setArray[i];
}
setArray = newArray;
}
public Object[] getSetArray() {
return setArray;
}
public String toString() {
String result = "";
for (int i = 0; i < size(); i++) {
if ((i % 10) == 0 && i != 0) {
result += "\n";
}
result = result + setArray[i].toString() + "\t";
}
return result;
}
public SetADT intersection(SetADT set) {
SetADT newSet = new SetADT();
for(int index = 0; index < set.size; index++){
if(set.contains(set) == set.contains(setArray)){
newSet.add(set);
}
}
return newSet;
}
public SetADT difference(SetADT set) {
SetADT newSet = new SetADT();
for(int index = 0; index < set.size; index++){
if(set.contains(set) == false){
newSet.add(set);
}
}
return newSet;
}
public Object[] sorted() {
setArray = getSetArray();
Arrays.sort(setArray);
return setArray;
}
public static String getObjectArray(Object[] o) {
String s = "";
for (int i = 0; i < o.length; i++) {
s += (o[i] + " ");
}
return s;
}
}
推荐答案
NPE的原因是您在数组中存储null。 不检查null。
The cause of NPE is that you store null in your array. The implementation of ComparableTimSort does not check for null.
private static void binarySort(Object[] a, int lo, int hi, int start) {
214 assert lo <= start && start <= hi;
215 if (start == lo)
216 start++;
217 for ( ; start < hi; start++) {
218 @SuppressWarnings("unchecked")
219 Comparable<Object> pivot = (Comparable) a[start];
220
221 // Set left (and right) to the index where a[start] (pivot) belongs
222 int left = lo;
223 int right = start;
224 assert left <= right;
225 /*
226 * Invariants:
227 * pivot >= all in [lo, left).
228 * pivot < all in [right, start).
229 */
230 while (left < right) {
231 int mid = (left + right) >>> 1;
232 if (pivot.compareTo(a[mid]) < 0)
233 right = mid;
234 else
235 left = mid + 1;
236 }
237 assert left == right;
238
您必须确保您的数组中不存储空值。
You must assure that null value is not stored in your array.
要做到这一点,你必须断言方法 add
中传递的参数不为null并创建容器的副本在排序到有效大小之前。
To do that you must assert that argument passed in method add
is not null and create a copy of your container before sorting to valid size.
public Object[] sorted() {
Object[] toSort = Arrays.copyOf(getSetArray(),getSize());
Arrays.sort(toSort);
return toSort;
}
这篇关于为什么我的排序实现中有NullPointerException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!