How to generate a random index into my ArrayList?










-1














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!!!










share|improve this question



















  • 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















-1














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!!!










share|improve this question



















  • 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













-1












-1








-1







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!!!










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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












  • 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







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












1 Answer
1






active

oldest

votes


















1














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);







share|improve this answer






















  • Got it, that was tripping me up. Thanks so much
    – Bug Scramble
    Nov 12 '18 at 7:02










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
);



);













draft saved

draft discarded


















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









1














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);







share|improve this answer






















  • Got it, that was tripping me up. Thanks so much
    – Bug Scramble
    Nov 12 '18 at 7:02















1














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);







share|improve this answer






















  • Got it, that was tripping me up. Thanks so much
    – Bug Scramble
    Nov 12 '18 at 7:02













1












1








1






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);







share|improve this answer














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);








share|improve this answer














share|improve this answer



share|improve this answer








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
















  • 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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20