f somebody could nudge me in the right direction that would be great.
Or tell me if I have to redo a chunk or something.
This is a program for rolling dice. I’m trying to make each individual ‘roll’ display in the eDieField.
here is the class:
import java.util.Random;
public class dice
{
private int times;
private int roll;
private int side;
public int[] each;
Random roller = new Random();
public void setTimes(int rolls)
{
times = rolls;
}
public void setSides(int die)
{
side = die;
}
public int getRoll()
{
int[] each = new int[times];
int total = 0;
int c = 0;
int i = 0;
while (c < times)
{
c = c + 1;
roll = roller.nextInt(side);
roll = roll + 1;
each[i] = roll;
total = total + roll;
System.out.print(each[i] + " ");
i = i + 1;
}
return total;
}
public int getEach()
{
return each[/*each number in the array*/];
}
}
here is the GUIWindow:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GUIWindow extends JFrame
{
private dice dices = new dice();
private JLabel dice = new JLabel("# of Dice");
private JLabel sides = new JLabel("# of Sides");
private JLabel total = new JLabel("Total:");
private JTextField diceField = new JTextField("");
private JTextField sideField = new JTextField("");
private JTextField totField = new JTextField("");
private JButton button = new JButton ("Roll!");
private JTextField eDieField = new JTextField("");
private JLabel empt = new JLabel("Here is each roll");
// Constructor
public GUIWindow()
{
JPanel dataPanel = new JPanel(new GridLayout(2, 2, 12, 6));
dataPanel.add(dice);
dataPanel.add(sides);
dataPanel.add(diceField);
dataPanel.add(sideField);
JPanel rPanel = new JPanel(new GridLayout(1, 3, 12, 6));
rPanel.add(button);
rPanel.add(total);
rPanel.add(totField);
JPanel eDiePan = new JPanel(new GridLayout(2, 1, 12, 6));
eDiePan.add(empt);
eDiePan.add(eDieField);
Container container = getContentPane();
container.add(dataPanel, BorderLayout.WEST);
container.add(rPanel, BorderLayout.EAST);
container.add(eDiePan, BorderLayout.SOUTH);
button.addActionListener(new dieListener());
}
// >>>>>>> The controller <<<<<<<<
private class dieListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try
{
String input = diceField.getText();
int die = Integer.parseInt(input);
dices.setTimes(die);
String inputa = sideField.getText();
int side = Integer.parseInt(inputa);
dices.setSides(side);
int tot = dices.getRoll();
totField.setText("" + tot);
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(GUIWindow.this,
"Sorry,\nyou can do that with dice.",
"Dice Fail",
JOptionPane.ERROR_MESSAGE);
}
int eachd = dices.getEach();
eDieField.setText("" + eachd);
}
}
}
and the main:
import javax.swing.*;
public class diceRoller
{
public static void main(String[] args)
{
GUIWindow theGUI = new GUIWindow();
theGUI.setTitle("Dice Roller");
theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
theGUI.pack();
theGUI.setVisible(true);
}
}
I hope this is not bad conduct or anything, I just have no idea what to do. (And my stupid textbook is useless) Say you enter 4 dice with 4 sides and the numbers that would be on if you rolled them by hand were 2, 4, 3, 4. added that would be 13. 13 goes in ‘totField’ and 2 4 3 4 go in ‘eDieField’. I cant get that to work. I keep getting nullPointerException and I don’t know how to keep the array each[] so I can get the numbers for eDieField (or something else like a list or whatever).
There are a few problems with your approach, but the one that is causing your main problem is:
you created a local variable called “each”, but you never actually instantiate the one in the dice class. Once the function leaves its scope, your local “each” is lost, causing your null pointer error. You think you’re setting the class variable when you did not.
Get rid of the local definition of the each variable (the int[] portion).
The next part is your getEach function. I see what you’re trying to do but that’s not what the code will do. Try something like this (though you may want to change the name to something like getRollList()):
and change the GUIWindow class dieListener actionEvent to:
(yes, i’ll leave it to you to figure out the last comma, heh, you could just use space instead of a comma). I was able to get your code to work with these changes.
Something else you should change. The general convention for naming a java class is to make the first character of your class name a capital letter. The “dice” class should be changed to “Dice”.