我正在尝试解决父亲提出的问题,但是由于某种原因而得到了NullPointerException 它是一个程序,用于创建Priority排序器,在其中输入名称和优先级,并自动为您排序所有内容。
我尝试将get / set方法与公共变量交换,但它不起作用!
public class PrioritySorter
{
Scanner s;
private String[] names;
private int[] priorities;
private Connecter[] connecters;
public PrioritySorter()
{
s = new Scanner(System.in);
names = new String[10];
priorities = new int[10];
connecters = new Connecter[10];
System.out.println("Enter 10 names");
for (int i = 0; i < 10; i++)
{
names[i] = s.nextLine();
connecters[i].setName(names[i]);
}
System.out.println("Enter priorities");
for (int i = 0; i < 10; i++)
{
System.out.print("Priority of " + names[i] + ": ");
priorities[i] = s.nextInt();
connecters[i].setConnectedPriority(priorities[i]);
}
sort();
print();
}
public void sort()
{
Arrays.sort(priorities);
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if(priorities[i] ==
connecters[j].getConnectedPriority())
{
if(names[i] ==
connecters[j].getName())
{
String temp = names[j];
names[j] = names[i];
names[i] = temp;
}
}
}
}
}
public void print()
{
for (int i = 0; i < 10; i++)
{
System.out.println("Name: " + names[i] + " Priority:
" + priorities[i]);
}
}
public static void main(String[] args)
{
new PrioritySorter();
}
}
public class Connecter
{
private String name = "Nujsf";
private int connectedPriority = 10000;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getConnectedPriority()
{
return connectedPriority;
}
public void setConnectedPriority(int connectedPriority)
{
this.connectedPriority = connectedPriority;
}
}