How to generate a random index into my ArrayList?
So I have a class Cup which is part of a game for class. The public int select() method has to return a move in c. I need to generate a random index into c, and I'm told to do this by generating a random number from zero up to and not including the size of the ArrayList. Here's what I have:
import java.util.ArrayList;
import java.util.Random;
public class Cup
ArrayList<Integer> c = new ArrayList<Integer>();
private Random r;
public Cup()
c.add(1);
c.add(2);
c.add(3);
Random r = new Random();
public int count()
return c.size();
public int select()
int index = r.nextInt(c.size());
return c.get(index);
public void remove(int m)
c.remove(m);
When I compile this in the game I'm using, it compiles correctly but tells me there's a Null Pointer exception on the line where r.nextInt(c.size()) is. Just very confusing because I feel like this should be right. Thank you!!!
java arraylist random nullpointerexception
add a comment |
So I have a class Cup which is part of a game for class. The public int select() method has to return a move in c. I need to generate a random index into c, and I'm told to do this by generating a random number from zero up to and not including the size of the ArrayList. Here's what I have:
import java.util.ArrayList;
import java.util.Random;
public class Cup
ArrayList<Integer> c = new ArrayList<Integer>();
private Random r;
public Cup()
c.add(1);
c.add(2);
c.add(3);
Random r = new Random();
public int count()
return c.size();
public int select()
int index = r.nextInt(c.size());
return c.get(index);
public void remove(int m)
c.remove(m);
When I compile this in the game I'm using, it compiles correctly but tells me there's a Null Pointer exception on the line where r.nextInt(c.size()) is. Just very confusing because I feel like this should be right. Thank you!!!
java arraylist random nullpointerexception
2
This line in the constructorRandom r = new Random();
is a local variabler
and thus the instance variabler
is never initialized, retaining its defaultnull
value.
– James K Polk
Nov 11 '18 at 23:48
add a comment |
So I have a class Cup which is part of a game for class. The public int select() method has to return a move in c. I need to generate a random index into c, and I'm told to do this by generating a random number from zero up to and not including the size of the ArrayList. Here's what I have:
import java.util.ArrayList;
import java.util.Random;
public class Cup
ArrayList<Integer> c = new ArrayList<Integer>();
private Random r;
public Cup()
c.add(1);
c.add(2);
c.add(3);
Random r = new Random();
public int count()
return c.size();
public int select()
int index = r.nextInt(c.size());
return c.get(index);
public void remove(int m)
c.remove(m);
When I compile this in the game I'm using, it compiles correctly but tells me there's a Null Pointer exception on the line where r.nextInt(c.size()) is. Just very confusing because I feel like this should be right. Thank you!!!
java arraylist random nullpointerexception
So I have a class Cup which is part of a game for class. The public int select() method has to return a move in c. I need to generate a random index into c, and I'm told to do this by generating a random number from zero up to and not including the size of the ArrayList. Here's what I have:
import java.util.ArrayList;
import java.util.Random;
public class Cup
ArrayList<Integer> c = new ArrayList<Integer>();
private Random r;
public Cup()
c.add(1);
c.add(2);
c.add(3);
Random r = new Random();
public int count()
return c.size();
public int select()
int index = r.nextInt(c.size());
return c.get(index);
public void remove(int m)
c.remove(m);
When I compile this in the game I'm using, it compiles correctly but tells me there's a Null Pointer exception on the line where r.nextInt(c.size()) is. Just very confusing because I feel like this should be right. Thank you!!!
java arraylist random nullpointerexception
java arraylist random nullpointerexception
edited Nov 12 '18 at 4:17
jhenrique
467312
467312
asked Nov 11 '18 at 23:39
Bug Scramble
265
265
2
This line in the constructorRandom r = new Random();
is a local variabler
and thus the instance variabler
is never initialized, retaining its defaultnull
value.
– James K Polk
Nov 11 '18 at 23:48
add a comment |
2
This line in the constructorRandom r = new Random();
is a local variabler
and thus the instance variabler
is never initialized, retaining its defaultnull
value.
– James K Polk
Nov 11 '18 at 23:48
2
2
This line in the constructor
Random r = new Random();
is a local variable r
and thus the instance variable r
is never initialized, retaining its default null
value.– James K Polk
Nov 11 '18 at 23:48
This line in the constructor
Random r = new Random();
is a local variable r
and thus the instance variable r
is never initialized, retaining its default null
value.– James K Polk
Nov 11 '18 at 23:48
add a comment |
1 Answer
1
active
oldest
votes
In your constructor you dont need Random r
as you already have a private Random r;
The rest seems to be working. Take care about your remove(int m)
method so the user doesn't pass a value greater than the ArrayList's size, to avoid an IndexOutOfBoundsException.
import java.util.ArrayList;
import java.util.Random;
public class Cup
ArrayList<Integer> c = new ArrayList<Integer>();
private Random r;
public Cup()
c.add(1);
c.add(2);
c.add(3);
//here you should use your r attribute
r = new Random();
public int count()
return c.size();
public int select()
int index = r.nextInt(c.size());
return c.get(index);
public void remove(int m)
c.remove(m);
Got it, that was tripping me up. Thanks so much
– Bug Scramble
Nov 12 '18 at 7:02
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254350%2fhow-to-generate-a-random-index-into-my-arraylist%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
In your constructor you dont need Random r
as you already have a private Random r;
The rest seems to be working. Take care about your remove(int m)
method so the user doesn't pass a value greater than the ArrayList's size, to avoid an IndexOutOfBoundsException.
import java.util.ArrayList;
import java.util.Random;
public class Cup
ArrayList<Integer> c = new ArrayList<Integer>();
private Random r;
public Cup()
c.add(1);
c.add(2);
c.add(3);
//here you should use your r attribute
r = new Random();
public int count()
return c.size();
public int select()
int index = r.nextInt(c.size());
return c.get(index);
public void remove(int m)
c.remove(m);
Got it, that was tripping me up. Thanks so much
– Bug Scramble
Nov 12 '18 at 7:02
add a comment |
In your constructor you dont need Random r
as you already have a private Random r;
The rest seems to be working. Take care about your remove(int m)
method so the user doesn't pass a value greater than the ArrayList's size, to avoid an IndexOutOfBoundsException.
import java.util.ArrayList;
import java.util.Random;
public class Cup
ArrayList<Integer> c = new ArrayList<Integer>();
private Random r;
public Cup()
c.add(1);
c.add(2);
c.add(3);
//here you should use your r attribute
r = new Random();
public int count()
return c.size();
public int select()
int index = r.nextInt(c.size());
return c.get(index);
public void remove(int m)
c.remove(m);
Got it, that was tripping me up. Thanks so much
– Bug Scramble
Nov 12 '18 at 7:02
add a comment |
In your constructor you dont need Random r
as you already have a private Random r;
The rest seems to be working. Take care about your remove(int m)
method so the user doesn't pass a value greater than the ArrayList's size, to avoid an IndexOutOfBoundsException.
import java.util.ArrayList;
import java.util.Random;
public class Cup
ArrayList<Integer> c = new ArrayList<Integer>();
private Random r;
public Cup()
c.add(1);
c.add(2);
c.add(3);
//here you should use your r attribute
r = new Random();
public int count()
return c.size();
public int select()
int index = r.nextInt(c.size());
return c.get(index);
public void remove(int m)
c.remove(m);
In your constructor you dont need Random r
as you already have a private Random r;
The rest seems to be working. Take care about your remove(int m)
method so the user doesn't pass a value greater than the ArrayList's size, to avoid an IndexOutOfBoundsException.
import java.util.ArrayList;
import java.util.Random;
public class Cup
ArrayList<Integer> c = new ArrayList<Integer>();
private Random r;
public Cup()
c.add(1);
c.add(2);
c.add(3);
//here you should use your r attribute
r = new Random();
public int count()
return c.size();
public int select()
int index = r.nextInt(c.size());
return c.get(index);
public void remove(int m)
c.remove(m);
edited Nov 12 '18 at 0:40
answered Nov 12 '18 at 0:29
jhenrique
467312
467312
Got it, that was tripping me up. Thanks so much
– Bug Scramble
Nov 12 '18 at 7:02
add a comment |
Got it, that was tripping me up. Thanks so much
– Bug Scramble
Nov 12 '18 at 7:02
Got it, that was tripping me up. Thanks so much
– Bug Scramble
Nov 12 '18 at 7:02
Got it, that was tripping me up. Thanks so much
– Bug Scramble
Nov 12 '18 at 7:02
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53254350%2fhow-to-generate-a-random-index-into-my-arraylist%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
This line in the constructor
Random r = new Random();
is a local variabler
and thus the instance variabler
is never initialized, retaining its defaultnull
value.– James K Polk
Nov 11 '18 at 23:48