mercredi 28 octobre 2020

System.TypeInitializationException: "Null reference Exception". Slot Machine will not run [duplicate]

I am creating a slot machine game for a class project and am receiving this error.

System.TypeInitializationException: 'The type initializer for 'WindowsFormsApp5.SlotMachine' threw an exception.'

NullReferenceException: Object reference not set to an instance of an object.

I am attempting to have numbers display in my textboxes to show that random numbers were generated. Any help will do. I am just a beginner. Thank you.

Form2.cs class where the exception is occurring

private void btnSpin_Click(object sender, EventArgs e)
        {
            SlotMachine.SlotReader.spinSlot();


            txtGame1.Text = SlotMachine.SlotReader.getSlotOne().ToString();
            txtGame2.Text = SlotMachine.SlotReader.getSlotTwo().ToString();
            txtGame3.Text = SlotMachine.SlotReader.getSlotThree().ToString();
        }

SlotMachine Class

public class SlotMachine
    {
        //attributes
        int moneyCollected;
        int Betplaced;
        //instaniates classes
        public static Player player = new Player();
        public static Table table = new Table();
        public static RandomNumber randomnum = new RandomNumber();
        public static SlotReader SlotReader = new SlotReader();
}

SlotReader Class

This is boilerplate code, as I am just trying to display numbers in my form to see if it works disregard reelResult.

public class SlotReader {


  public void spinSlot()
        {
 //attributes
         int[] reelResult = { 0, 0, 0, 0, 0 };
        //first random number
        int Slot1;
        //second random number
        int Slot2;
        //third random number
         int Slot3;
         int max = 3;
        //how much was bet this round
         int betNum = 0;

        //Empty Constructor
        public SlotReader() {

        }

            Slot1 = SlotMachine.randomnum.pickRandomValue();
            Slot2 = SlotMachine.randomnum.pickRandomValue();
            Slot3 = SlotMachine.randomnum.pickRandomValue();
        }
        public int getSlotOne()
        {
            return Slot1;
        }
        public int getSlotTwo()
        {
            return Slot2;
        }
        public int getSlotThree()
        {
            return Slot3;
        }
    }

Any help would be extremely appreciated. It's a class assignment. I am trying my best to learn.




Aucun commentaire:

Enregistrer un commentaire