I would like to generate a sequence of 5 random integers from 0 to 11, and then sort this in an increasing order. I have coded as follows but the logcat shows NullPointerException
.
How could such be solved? Many thanks!!
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]);
}
}
Logcat:
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)
line 111 represents the line for above NumSeq[0] = random1.nextInt(12);
Solution:
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));
...
}