user3163561
user3163561

Reputation: 17

JOptionPane message showing null

we are working on a project trying to make a message in a JOptionPane show up when a button is pressed and certain conditions are met. However whenever the code is activated and a button is pressed, the JOptionPane shows up with no message. Here is the code that creates the GUI

 package BlackJack;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import javax.swing.*;

public class BlckJckUI {

public static void main(String[] args) {

    JFrame GUI = new JFrame("Blackjack Advisor");
    GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    GUI.setSize(1000,900);
    GUI.setVisible(true);
 JButton two = new JButton(Two);
    two.setSize(300, 100);
    two.setLocation(100, 200);
    two.addActionListener(new ActionListener ()
    {
        public void actionPerformed(ActionEvent e)
        {
            Arrays array = new Arrays();
            Math math = new Math();
            math.cardvalue = 2;
            array.clicktracker++;

            JOptionPane.showMessageDialog(null,array.result);
        }
    });
   GUI.add(two);

This is the code that works out the logic.

package BlackJack;

public class Math {
public int cardvalue;
public Math()
{

    Arrays array = new Arrays();

    if (array.clicktracker == 1)
    {   
        array.dealer = cardvalue;   
        array.result = "Please select the first card you have :)";


    }
    else if (array.clicktracker == 2)
    {

        array.playerhand.add(cardvalue);
        array.result = "Please select the second card you have :)";

    }
    else if (array.clicktracker >= 3)
    {
        array.playerhand.add(cardvalue);
        if (array.playerhandtotal <= 8)
        {

            // array.result = result statement
            array.result = "You should just hit until you're safe. If the dealer 6 or below,\n"
                + " the chances are that he'll bust and if not, remain low above 17.\n"
                + " As long as you can pull a 17 or higher, you should be safe. Pick \n"
                + "another card or reset.";

This is the code that creates the Array and variables associated with it.

package BlackJack;

import java.util.ArrayList;

public class Arrays{
  public String result = null;
    ArrayList<Integer> playerhand = new ArrayList<Integer>();
    public int dealer = 0;
    public int clicktracker = 0;
    public int playerhandtotal = 0;
    {
    for (int element: playerhand)
     {
         playerhandtotal = element + playerhandtotal;
     }
    System.out.println(result);
    System.out.println(dealer);
    System.out.println(clicktracker);
     }
}

Upvotes: 1

Views: 265

Answers (2)

Java Devil
Java Devil

Reputation: 10959

The problem is that you are creating two separate instances of your Arrays class. Once in the actionPerformed method and also within the constructor of your Math class.

This code you currently have:

Arrays array = new Arrays();
Math math = new Math();
math.cardvalue = 2;
array.clicktracker++;

JOptionPane.showMessageDialog(null,array.result);

will display the result of the Arrays object you created in the actionPerformed method - which is null as the result of this object is initialised to null and never set.

This has been mentioned in other answers and comments and will solve it from producing null but this approach will now always yield the same result as you are still always creating a new instance of your Arrays class in your actionPerformed method.

A better approach would be to separate the logic of the result from the constructor of your Math class into another method and create this instance of your Math class outside of the actionPerformed method. Then within your actionPerformed method call your method which will do the logic for your result.

In UI:

Math math = new Math();
two.addActionListener(new ActionListener ()
{
    public void actionPerformed(ActionEvent e)
    {
        math.cardvalue = 2;
        math.array.clicktracker++;
        math.calcResult();
        JOptionPane.showMessageDialog(null,math.array.result);
    }
});

In Math:

public class Math {
    public int cardvalue;
    public Arrays array;

    public Math()
    {
        array = new Arrays();
    }

    public void calcResult(){
        if (array.clicktracker == 1)
        {   
        //...rest of your logic
    }
}

Upvotes: 0

Obicere
Obicere

Reputation: 3019

In your Math constructor, you are changing the result of a different type of array.result than the one you are trying to display.

I would consider passing the Arrays instance into the Math constructor so that you can modify the result from there. Be sure to not reassign the instance though.


public void actionPerformed(ActionEvent e)
    {
        Arrays array = new Arrays();
        Math math = new Math(array);
        math.cardvalue = 2;
        array.clicktracker++;

        JOptionPane.showMessageDialog(null,array.result);
    }

...

public Math(Arrays array)
{

    if (array.clicktracker == 1)
    {   
    // And so on ...

Upvotes: 2

Related Questions