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();
}
}
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 ********************
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.