Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Having trying to figure out an error that keeps popping up for line 46 and 94. H

ID: 3830584 • Letter: H

Question

Having trying to figure out an error that keeps popping up for line 46 and 94. Had no luck fixing this null issue.

java.lang.NullPointerException   

import java.util.ArrayList;
import java.util.Random;
public class ButtonMen
{
class Dice
{
  int no_of_sides;
  public Dice(int no_of_sides)
  {
   this.no_of_sides = no_of_sides;
  }
  int rollDice()
  {
   Random r = new Random();
   return r.nextInt(no_of_sides) + 1;
  }
  @Override
  public String toString()
  {
   return no_of_sides + "";
  }
}
private String player1, player2;
private ArrayList<Dice> player1Dices;
private ArrayList<Dice> player2Dices;
private int player1Points = 0, player2Points = 0;
public ButtonMen(String player1, String player2)
{
  this.player1 = player1;
  this.player2 = player2;
  player1Dices = new ArrayList<>();
  player2Dices = new ArrayList<>();
  Random r = new Random();
  for(int i=0; i<5; i++)
  {
   player1Dices.add(new Dice(r.nextInt(20) + 1));
   player2Dices.add(new Dice(r.nextInt(20) + 1));
  }
  }
  public void playGame()
  {
   while(player1Dices.size() != 0 && player2Dices.size() != 0);
  }
  {
   Dice d1 = player1Dices.remove(0);
   
Dice d2 = player2Dices.remove(0);
   boolean tie = false;
   do
   {
    int roll1 = d1.rollDice();
    int roll2 = d2.rollDice();
    System.out.println("P1 rolled " + roll1 + " on dice of sides " + d1.no_of_sides);
    System.out.println("P2 rolled " + roll2 + " on dice of sides " + d2.no_of_sides);
    if(roll1 > roll2)
   {
    System.out.println("P1 Won.");
    player1Points += d2.no_of_sides;
    player1Dices.add(d1);
    player1Dices.add(d2);
    tie = false;
   }
   else if(roll1 < roll2)
   {
    System.out.println("P2 Won.");
    player2Points += d1.no_of_sides;
    player2Dices.add(d1);
    player2Dices.add(d2);
    tie = false;
   }
   else
   {
    System.out.println("Tie. Roll again.");
    tie = true;
   }
  }
  while(tie);
  System.out.println("Points: " + player1 + ": " + player1Points + ", " + player2 + ": " + player2Points + " ");
  if(player1Points > player2Points)
  {
   System.out.println(" ************* "+ player1 +" Has won ********************");
  }
  else if(player1Points < player2Points)
  {
   System.out.println(" ************* "+ player2 +" Has won ********************");
  }
  else
  {
   System.out.println(" ************* Its a tie!! ********************");
  }
  }
  public static void main(String[] args)
  {
ButtonMen buttonMen = new ButtonMen("Player1", "Player2");
  buttonMen.playGame();
  }
}

49 51 52 58 61 62 63 66 70 71 72 73 75 78 Dice di layer 1Dices. remove Dice d2 player2D remove 2Dices e(0) boolean tie false int roll dl.rollDice(); int rol 12 d2.rollDice() System. out.println("P1 rolled roll1 on dice of sides d1.no of sides) System out.println("P2 rolled roll on dice of sides d2.no of sides) if(ro 111 rol12) System.out.println("P1 Won."); player1Points d2.no of sides; player1Dices. add(d1); player1Dices. add(d2); tie false else if(roll1 rol12) System.out.println("P2 Won. player2Points d1.no of sides; player2Dices add(d1); player Dices .add(d2); tie false else system out.println("Tie. Roll again."); tie true; while (tie) system. out.println ("Points player1 player 1Points player2 player2Points if (player1Points player2Points) System.out.println("InVn' player1 Has won else if player1Points player2points) system out. printin ("InAn player2 else Its a tie!! stem out.println(" args public static void main(String[] new ButtonMer ("Player1 Player2 buttonMen utton Men buttonMen playGame

Explanation / Answer

Hi, I have fixed the issue.

import java.util.ArrayList;

import java.util.Random;

public class ButtonMen

{

   class Dice

   {

       int no_of_sides;

       public Dice(int no_of_sides)

       {

           this.no_of_sides = no_of_sides;

       }

       int rollDice()

       {

           Random r = new Random();

           return r.nextInt(no_of_sides) + 1;

       }

       @Override

       public String toString()

       {

           return no_of_sides + "";

       }

   }

   private String player1, player2;

   private ArrayList<Dice> player1Dices;

   private ArrayList<Dice> player2Dices;

   private int player1Points = 0, player2Points = 0;

   public ButtonMen(String player1, String player2)

   {

       this.player1 = player1;

       this.player2 = player2;

       player1Dices = new ArrayList<>();

       player2Dices = new ArrayList<>();

       Random r = new Random();

       for(int i=0; i<5; i++)

       {

           player1Dices.add(new Dice(r.nextInt(20) + 1));

           player2Dices.add(new Dice(r.nextInt(20) + 1));

       }

   }

   public void playGame()

   {

       while(player1Dices.size() != 0 && player2Dices.size() != 0)

       {

           Dice d1 = player1Dices.remove(0);

           Dice d2 = player2Dices.remove(0);

           boolean tie = false;

           do

           {

               int roll1 = d1.rollDice();

               int roll2 = d2.rollDice();

               System.out.println("P1 rolled " + roll1 + " on dice of sides " + d1.no_of_sides);

               System.out.println("P2 rolled " + roll2 + " on dice of sides " + d2.no_of_sides);

               if(roll1 > roll2)

               {

                   System.out.println("P1 Won.");

                   player1Points += d2.no_of_sides;

                   player1Dices.add(d1);

                   player1Dices.add(d2);

                   tie = false;

               }

               else if(roll1 < roll2)

               {

                   System.out.println("P2 Won.");

                   player2Points += d1.no_of_sides;

                   player2Dices.add(d1);

                   player2Dices.add(d2);

                   tie = false;

               }

               else

               {

                   System.out.println("Tie. Roll again.");

                   tie = true;

               }

           }while(tie);

           System.out.println("Points: " + player1 + ": " + player1Points + ", " + player2 + ": " + player2Points + " ");

           if(player1Points > player2Points)

           {

               System.out.println(" ************* "+ player1 +" Has won ********************");

           }

           else if(player1Points < player2Points)

           {

               System.out.println(" ************* "+ player2 +" Has won ********************");

           }

           else

           {

               System.out.println(" ************* Its a tie!! ********************");

           }

       }

   }

   public static void main(String[] args)

   {

       ButtonMen buttonMen = new ButtonMen("Player1", "Player2");

       buttonMen.playGame();

   }

}

/*

Sample run:

P1 rolled 4 on dice of sides 13

P2 rolled 1 on dice of sides 1

P1 Won.

Points: Player1: 1, Player2: 0

************* Player1 Has won ********************

P1 rolled 4 on dice of sides 15

P2 rolled 4 on dice of sides 13

Tie. Roll again.

P1 rolled 11 on dice of sides 15

P2 rolled 13 on dice of sides 13

P2 Won.

Points: Player1: 1, Player2: 15

************* Player2 Has won ********************

P1 rolled 6 on dice of sides 8

P2 rolled 6 on dice of sides 7

Tie. Roll again.

P1 rolled 3 on dice of sides 8

P2 rolled 2 on dice of sides 7

P1 Won.

Points: Player1: 8, Player2: 15

************* Player2 Has won ********************

P1 rolled 14 on dice of sides 18

P2 rolled 5 on dice of sides 19

P1 Won.

Points: Player1: 27, Player2: 15

************* Player1 Has won ********************

P1 rolled 10 on dice of sides 17

P2 rolled 14 on dice of sides 18

P2 Won.

Points: Player1: 27, Player2: 32

************* Player2 Has won ********************

P1 rolled 2 on dice of sides 13

P2 rolled 5 on dice of sides 15

P2 Won.

Points: Player1: 27, Player2: 45

************* Player2 Has won ********************

P1 rolled 1 on dice of sides 1

P2 rolled 5 on dice of sides 13

P2 Won.

Points: Player1: 27, Player2: 46

************* Player2 Has won ********************

P1 rolled 1 on dice of sides 8

P2 rolled 9 on dice of sides 17

P2 Won.

Points: Player1: 27, Player2: 54

************* Player2 Has won ********************

P1 rolled 7 on dice of sides 7

P2 rolled 13 on dice of sides 18

P2 Won.

Points: Player1: 27, Player2: 61

************* Player2 Has won ********************

P1 rolled 2 on dice of sides 18

P2 rolled 13 on dice of sides 13

P2 Won.

Points: Player1: 27, Player2: 79

************* Player2 Has won ********************

P1 rolled 1 on dice of sides 19

P2 rolled 1 on dice of sides 15

Tie. Roll again.

P1 rolled 17 on dice of sides 19

P2 rolled 12 on dice of sides 15

P1 Won.

Points: Player1: 42, Player2: 79

************* Player2 Has won ********************

P1 rolled 6 on dice of sides 19

P2 rolled 1 on dice of sides 1

P1 Won.

Points: Player1: 43, Player2: 79

************* Player2 Has won ********************

P1 rolled 5 on dice of sides 15

P2 rolled 3 on dice of sides 13

P1 Won.

Points: Player1: 56, Player2: 79

************* Player2 Has won ********************

P1 rolled 8 on dice of sides 19

P2 rolled 5 on dice of sides 8

P1 Won.

Points: Player1: 64, Player2: 79

************* Player2 Has won ********************

P1 rolled 1 on dice of sides 1

P2 rolled 7 on dice of sides 17

P2 Won.

Points: Player1: 64, Player2: 80

************* Player2 Has won ********************

P1 rolled 4 on dice of sides 15

P2 rolled 7 on dice of sides 7

P2 Won.

Points: Player1: 64, Player2: 95

************* Player2 Has won ********************

P1 rolled 5 on dice of sides 13

P2 rolled 7 on dice of sides 18

P2 Won.

Points: Player1: 64, Player2: 108

************* Player2 Has won ********************

P1 rolled 12 on dice of sides 19

P2 rolled 5 on dice of sides 18

P1 Won.

Points: Player1: 82, Player2: 108

************* Player2 Has won ********************

P1 rolled 4 on dice of sides 8

P2 rolled 6 on dice of sides 13

P2 Won.

Points: Player1: 82, Player2: 116

************* Player2 Has won ********************

P1 rolled 7 on dice of sides 19

P2 rolled 1 on dice of sides 1

P1 Won.

Points: Player1: 83, Player2: 116

************* Player2 Has won ********************

P1 rolled 8 on dice of sides 18

P2 rolled 8 on dice of sides 17

Tie. Roll again.

P1 rolled 16 on dice of sides 18

P2 rolled 12 on dice of sides 17

P1 Won.

Points: Player1: 100, Player2: 116

************* Player2 Has won ********************

P1 rolled 2 on dice of sides 19

P2 rolled 3 on dice of sides 15

P2 Won.

Points: Player1: 100, Player2: 135

************* Player2 Has won ********************

P1 rolled 1 on dice of sides 1

P2 rolled 5 on dice of sides 7

P2 Won.

Points: Player1: 100, Player2: 136

************* Player2 Has won ********************

P1 rolled 6 on dice of sides 18

P2 rolled 10 on dice of sides 13

P2 Won.

Points: Player1: 100, Player2: 154

************* Player2 Has won ********************

P1 rolled 13 on dice of sides 17

P2 rolled 4 on dice of sides 18

P1 Won.

Points: Player1: 118, Player2: 154

************* Player2 Has won ********************

P1 rolled 2 on dice of sides 17

P2 rolled 5 on dice of sides 8

P2 Won.

Points: Player1: 118, Player2: 171

************* Player2 Has won ********************

P1 rolled 10 on dice of sides 18

P2 rolled 2 on dice of sides 13

P1 Won.

Points: Player1: 131, Player2: 171

************* Player2 Has won ********************

P1 rolled 17 on dice of sides 18

P2 rolled 5 on dice of sides 19

P1 Won.

Points: Player1: 150, Player2: 171

************* Player2 Has won ********************

P1 rolled 5 on dice of sides 13

P2 rolled 15 on dice of sides 15

P2 Won.

Points: Player1: 150, Player2: 184

************* Player2 Has won ********************

P1 rolled 1 on dice of sides 18

P2 rolled 1 on dice of sides 1

Tie. Roll again.

P1 rolled 10 on dice of sides 18

P2 rolled 1 on dice of sides 1

P1 Won.

Points: Player1: 151, Player2: 184

************* Player2 Has won ********************

P1 rolled 9 on dice of sides 19

P2 rolled 5 on dice of sides 7

P1 Won.

Points: Player1: 158, Player2: 184

************* Player2 Has won ********************

P1 rolled 13 on dice of sides 18

P2 rolled 9 on dice of sides 18

P1 Won.

Points: Player1: 176, Player2: 184

************* Player2 Has won ********************

P1 rolled 1 on dice of sides 1

P2 rolled 6 on dice of sides 13

P2 Won.

Points: Player1: 176, Player2: 185

************* Player2 Has won ********************

P1 rolled 13 on dice of sides 19

P2 rolled 8 on dice of sides 17

P1 Won.

Points: Player1: 193, Player2: 185

************* Player1 Has won ********************

P1 rolled 2 on dice of sides 7

P2 rolled 1 on dice of sides 8

P1 Won.

Points: Player1: 201, Player2: 185

************* Player1 Has won ********************

P1 rolled 13 on dice of sides 18

P2 rolled 12 on dice of sides 13

P1 Won.

Points: Player1: 214, Player2: 185

************* Player1 Has won ********************

P1 rolled 13 on dice of sides 18

P2 rolled 5 on dice of sides 15

P1 Won.

Points: Player1: 229, Player2: 185

************* Player1 Has won ********************

P1 rolled 11 on dice of sides 19

P2 rolled 1 on dice of sides 1

P1 Won.

Points: Player1: 230, Player2: 185

************* Player1 Has won ********************

P1 rolled 6 on dice of sides 17

P2 rolled 5 on dice of sides 13

P1 Won.

Points: Player1: 243, Player2: 185

************* Player1 Has won ********************

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote