我想生成一个从0到11的5个随机整数序列,然后按递增顺序对其排序。我已经编码如下,但logcat显示NullPointerException
。
这样的问题怎么解决?非常感谢!啊!
public class Memory extends Activity
{
Integer[] NumSeq;
private Random random1;
@Override
public void onCreate(Bundle savedInstanceState)
{...
random1 = new Random();
setQup();
}
private void setQup()
{
NumSeq[0] = random1.nextInt(12);
NumSeq[1] = random1.nextInt(12);
while (NumSeq[1] == NumSeq[0]) {NumSeq[1] = random1.nextInt(12);}
NumSeq[2] = random1.nextInt(12);
while ((NumSeq[2] == NumSeq[1]) || (NumSeq[2] ==NumSeq[0])) {NumSeq[2] = random1.nextInt(12);}
...
Arrays.sort(NumSeq, new Comparator<Integer>()
{
@Override
public int compare(Integer x, Integer y)
{
return x - y;
}
});
Button11.setText(""+NumSeq[0]);
Button12.setText(""+NumSeq[1]);
Button13.setText(""+NumSeq[2]);
}
}
日志:
12-12 00:01:17.875: E/AndroidRuntime(18703): FATAL EXCEPTION: main
12-12 00:01:17.875: E/AndroidRuntime(18703): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pearappx.logic.run/com.pearappx.logic.run.Memory}: java.lang.NullPointerException
12-12 00:01:17.875: E/AndroidRuntime(18703): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
12-12 00:01:17.875: E/AndroidRuntime(18703): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
12-12 00:01:17.875: E/AndroidRuntime(18703): at android.app.ActivityThread.access$600(ActivityThread.java:127)
12-12 00:01:17.875: E/AndroidRuntime(18703): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
12-12 00:01:17.875: E/AndroidRuntime(18703): at android.os.Handler.dispatchMessage(Handler.java:99)
12-12 00:01:17.875: E/AndroidRuntime(18703): at android.os.Looper.loop(Looper.java:137)
12-12 00:01:17.875: E/AndroidRuntime(18703): at android.app.ActivityThread.main(ActivityThread.java:4511)
12-12 00:01:17.875: E/AndroidRuntime(18703): at java.lang.reflect.Method.invokeNative(Native Method)
12-12 00:01:17.875: E/AndroidRuntime(18703): at java.lang.reflect.Method.invoke(Method.java:511)
12-12 00:01:17.875: E/AndroidRuntime(18703): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
12-12 00:01:17.875: E/AndroidRuntime(18703): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
12-12 00:01:17.875: E/AndroidRuntime(18703): at dalvik.system.NativeStart.main(Native Method)
12-12 00:01:17.875: E/AndroidRuntime(18703): Caused by: java.lang.NullPointerException
12-12 00:01:17.875: E/AndroidRuntime(18703): at com.abc.logic.run.Memory.setQup(Memory.java:111)
12-12 00:01:17.875: E/AndroidRuntime(18703): at com.abc.logic.run.Memory.onCreate(Memory.java:66)
12-12 00:01:17.875: E/AndroidRuntime(18703): at android.app.Activity.performCreate(Activity.java:4470)
12-12 00:01:17.875: E/AndroidRuntime(18703): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
12-12 00:01:17.875: E/AndroidRuntime(18703): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
第111行表示上述
NumSeq[0] = random1.nextInt(12);
解决方案:
private List<Integer> NumS;
private void setQup()
{
// set random number from 0 to 11
int num0 = random1.nextInt(12);
int num1 = random1.nextInt(12);
while (num1 ==num0) {num1 = random1.nextInt(12);}
int num2 = random1.nextInt(12);
while ((num2 ==num0)||(num2 ==num1)) {num2 = random1.nextInt(12);}
int num3 = random1.nextInt(12);
while ((num3 ==num0)||(num3 ==num1)||(num3 ==num2)) {num3 = random1.nextInt(12);}
NumS = new ArrayList<Integer>();
NumS.clear();
NumS.add(num0);
NumS.add(num1);
NumS.add(num2);
NumS.add(num3);
Collections.sort(NumS);
Button11.setText(""+NumS.get(0));
...
}
最佳答案
您忘记初始化Integer[] NumSeq
,因此将其初始化为:
Integer[] NumSeq = new Integer[12];
要避免静态大小,请使用
ArrayList<Integer>
而不是Integer Array
您可以使用
Collections.sort
Collections.sort(unsortedList); // in ascending Order
关于java - 按升序对整数排序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13823813/