Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8997291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:52:12+00:00 2026-06-15T23:52:12+00:00

I would like to generate a sequence of 5 random integers from 0 to

  • 0

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));
               ...
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:52:12+00:00Added an answer on June 15, 2026 at 11:52 pm

    you forget to initialize Integer[] NumSeq so initialize it as:

    Integer[] NumSeq = new Integer[12];
    

    and to avoid static size use ArrayList<Integer> instead of Integer Array

    you can sort ArrayList easy using Collections.sort

    Collections.sort(unsortedList); // in ascending Order
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is not homework. I would like to generate a random integer sequence (50
I am creating a function that I would like to generate random strings from
Using Groovy, I'd like to generate a random sequence of characters from a given
I would like a function that can generate a pseudo-random sequence of values, but
I would like to generate a patch with all commits from a local branch.
I would like to generate the mysql-password hash from js. I know the method
I would like to generate 16 char length hex decimal value as sequence. My
I would like to use an IEnumerable to generate a sequence of values --
Possible Duplicate: How Random is System.Guid.NewGuid()? Based on this question I would like to
I would like to generate the following output, using a single row from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.