Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3962580
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:06:48+00:00 2026-05-20T03:06:48+00:00

f somebody could nudge me in the right direction that would be great. Or

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T03:06:49+00:00Added an answer on May 20, 2026 at 3:06 am

    There are a few problems with your approach, but the one that is causing your main problem is:

    public int getRoll()
      { 
        int[] each = new int[times];
    

    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()):

      public String getEach()
       {
         StringBuffer sb = new StringBuffer();
    
         for (int i : each) {
           sb.append(i).append(",");
         }
    
         return sb.toString();
       }
    

    and change the GUIWindow class dieListener actionEvent to:

    String eachd = dices.getEach();
    

    (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”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would really appreciate if somebody could tell me why compilation of this program:
Simple question that would be fantastic if somebody could answer. I have a simple
Would appreciate if somebody could help me with something like this: NOTE!: I Do
How can I produce a URL which somebody could open and that would immediately
I am a beginner in jQuery. It would be great if somebody could help
That's an odd title. I would greatly appreciate it if somebody could clarify what
I would appreciate if somebody could help me with this (and explaining what's going
I wonder if somebody could point me in the right direction. We're currently looking
I have the following problem/doubt that I hope somebody could help me with. Let's
I would be really grateful if somebody could help me out with this.. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.