嗨,我正在尝试按字母顺序获取这些水果,有人可以帮忙吗,因为当我尝试运行程序时它会关闭并说 NullPointer Exception java Lang 但它编译没有问题我认为问题出在 .compareTo 行我不太确定
import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Write a description of class rigthOrder here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class rigthOrder
{
public static void main (String args []) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int i;
String fruit [] = new String[1000];
List<String> fruitsList = Arrays.asList (fruit);
fruit[0] = "orange";
fruit[1] = "bananna";
fruit[2] = "apple";
fruit[3] = "grape";
fruit[4] = "plum";
// for(i = 0; i < fruit.length; i++)
{
// fruit [i] = fruit [i].toUpperCase();
}
Bubble_sort(fruit);
for(i = 0; i < fruit.length; i++)
{
System.out.println(fruit [i]);
}
}
public static void Bubble_sort(String [] fruit)
{
int i, j, size = fruit.length;
String temp;
for(i = 0; i < size-1; i++)
{
for(j = i + 1; j < size; j++)
{
if (fruit[j].compareTo (fruit[i]) < 0)
{
temp = fruit[i];
fruit[i] = fruit [j];
fruit[j] = temp;
}
}
}
}
}