Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

java - Why am I getting a StackOverflowError when getting information from an object?

Hello I am currently trying to create a game of palace in java. I took a course in solo learn but never really learned how to deal with stack overflow errors. None of the questions I had where found in the internet. Currently on variable AllCards aceClover = new AllCards(); It says there is a StackOverFlowError and I don't know why.

package MainClasses;
import java.awt.*;
import java.io.*;
import javax.imageio.ImageIO;
import java.util.Random;
public class AllCards {
    private Image cardImg = null;// to set image
    private String cardImgURL = ""; // get image location
    private int cardOrder = 0; // Card order in a hierarchy
    public boolean ruleAffect = false;  // if rules is changed can affect
    private boolean cardPower = false; // is the card a special card?
    private int cardPowerNumber = 0; // which are the powers 1.Go again 2.Clear 3.mirror 4.Put card lower 5.Pick up deck unless have a copy
    AllCards aceClover = new AllCards(); // declared the Clover Ace
    AllCards twoClover = new AllCards();
    AllCards threeClover = new AllCards();
    AllCards fourClover = new AllCards();
    AllCards fiveClover = new AllCards();
    AllCards sixClover = new AllCards();
    AllCards sevenClover = new AllCards();
    AllCards eightClover = new AllCards();
    AllCards nineClover = new AllCards();
    AllCards tenClover = new AllCards();
    AllCards jackClover = new AllCards();
    AllCards queenClover = new AllCards();
    AllCards kingClover = new AllCards();
    public Image imageExtract;
    public String cardImgUrlExtractor;
    public int cardOrderExtract;
    public boolean ruleAffectExtract;
    public boolean cardPowerExtractor;
    public int cardPowerNumberExtractor;

    public AllCards() {


        aceClover.cardImgURL = "C:\Users\Jhosua\eclipse-workspace\Palace Game\bin\All Cards\tile000.png"; // clover ace url
        if(aceClover.ruleAffect == false) {
            aceClover.cardOrder = 1;
        }else {
            aceClover.cardOrder = 14;
        }

        try {

            aceClover.cardImg = ImageIO.read(new File(aceClover.cardImgURL));

        }catch(Exception e) {

            System.out.println("Could't loud the Image! Error : "+e.getMessage());

        }

        twoClover.cardImgURL = "C:\Users\Jhosua\eclipse-workspace\Palace Game\bin\All Cards\tile001.png";
        twoClover.cardOrder = 14;
        twoClover.cardPower = true;
        twoClover.cardPowerNumber = 2;
        try {
            twoClover.cardImg = ImageIO.read(new File(twoClover.cardImgURL));
        }catch(Exception e) {
            System.out.println("Could't loud the Image! Error : "+e.getMessage());
        }

        threeClover.cardImgURL = "C:\Users\Jhosua\eclipse-workspace\Palace Game\bin\All Cards\tile002.png";
        threeClover.cardOrder = 15;
        threeClover.cardPower = true;
        if(threeClover.ruleAffect == false) {
            threeClover.cardPowerNumber = 14;
        }else {
            threeClover.cardPowerNumber = 3;
        }

        try {
            threeClover.cardImg = ImageIO.read(new File(threeClover.cardImgURL));
        }catch(Exception e) {
            System.out.println("Could't loud the Image! Error : "+e.getMessage());
        }

        fourClover.cardImgURL = "C:\Users\Jhosua\eclipse-workspace\Palace Game\bin\All Cards\tile003.png";
        fourClover.cardOrder = 4;
        try {
            fourClover.cardImg = ImageIO.read(new File(fourClover.cardImgURL));
        }catch(Exception e) {
            System.out.println("Could't loud the Image! Error : "+e.getMessage());
        }

        fiveClover.cardImgURL = "C:\Users\Jhosua\eclipse-workspace\Palace Game\bin\All Cards\tile004.png";
        fiveClover.cardOrder = 5;
        try {
            fiveClover.cardImg = ImageIO.read(new File(fiveClover.cardImgURL));
        }catch(Exception e) {
            System.out.println("Could't loud the Image! Error : "+e.getMessage());
        }

        sixClover.cardImgURL = "C:\Users\Jhosua\eclipse-workspace\Palace Game\bin\All Cards\tile005.png";
        sixClover.cardOrder = 6;
        try {
            sixClover.cardImg = ImageIO.read(new File(sixClover.cardImgURL));
        }catch(Exception e) {
            System.out.println("Could't loud the Image! Error : "+e.getMessage());
        }

        sevenClover.cardImgURL = "C:\Users\Jhosua\eclipse-workspace\Palace Game\bin\All Cards\tile006.png";
        sevenClover.cardOrder = 14;
        sevenClover.cardPower = true;
        sevenClover.cardPowerNumber = 4;
        try {
            sevenClover.cardImg = ImageIO.read(new File(sevenClover.cardImgURL));
        }catch(Exception e) {
            System.out.println("Could't loud the Image! Error : "+e.getMessage());
        }

        eightClover.cardImgURL = "C:\Users\Jhosua\eclipse-workspace\Palace Game\bin\All Cards\tile007.png";
        eightClover.cardOrder = 8;
        try {
            eightClover.cardImg = ImageIO.read(new File(eightClover.cardImgURL));
        }catch(Exception e) {
            System.out.println("Could't loud the Image! Error : "+e.getMessage());
        }

        nineClover.cardImgURL = "C:\Users\Jhosua\eclipse-workspace\Palace Game\bin\All Cards\tile008.png";
        nineClover.cardOrder = 9;
        try {
            nineClover.cardImg = ImageIO.read(new File(nineClover.cardImgURL));
        }catch(Exception e) {
            System.out.println("Could't loud the Image! Error : "+e.getMessage());
        }

        tenClover.cardImgURL = "C:\Users\Jhosua\eclipse-workspace\Palace Game\bin\All Cards\tile009.png";
        tenClover.cardOrder = 15;
        tenClover.cardPower = true;
        tenClover.cardPowerNumber = 14;
        tenClover.ruleAffect = true;
        try {
            tenClover.cardImg = ImageIO.read(new File(tenClover.cardImgURL));
        }catch(Exception e) {
            System.out.println("Could't loud the Image! Error : "+e.getMessage());
        }

        jackClover.cardImgURL = "C:\Users\Jhosua\eclipse-workspace\Palace Game\bin\All Cards\tile010.png";
        jackClover.cardOrder = 11;
        try {
            jackClover.cardImg = ImageIO.read(new File(jackClover.cardImgURL));
        }catch(Exception e) {
            System.out.println("Could't loud the Image! Error : "+e.getMessage());
        }

        queenClover.cardImgURL = "C:\Users\Jhosua\eclipse-workspace\Palace Game\bin\All Cards\tile011.png";
        queenClover.cardOrder = 12;
        try {
            queenClover.cardImg = ImageIO.read(new File(queenClover.cardImgURL));
        }catch(Exception e) {
            System.out.println("Could't loud the Image! Error : "+e.getMessage());
        }

        kingClover.cardImgURL = "C:\Users\Jhosua\eclipse-workspace\Palace Game\bin\All Cards\tile012.png";
        kingClover.cardOrder = 13;
        try {
            kingClover.cardImg = ImageIO.read(new File(kingClover.cardImgURL));
        }catch(Exception e) {
            System.out.println("Could't loud the Image! Error : "+e.getMessage());
        }
    }
    public void setVariables(AllCards x) {
        imageExtract = x.cardImg;
        cardImgUrlExtractor = x.cardImgURL;
        cardOrderExtract = x.cardOrder;
        ruleAffectExtract = x.ruleAffect;
        cardPowerNumberExtractor = x.cardPowerNumber;
        cardPowerExtractor = x.cardPower;
    }
    public void randomizer() {
        Random rand = new Random();
        int rand_int1 = rand.nextInt(15-1)+1;
        System.out.print(rand_int1);
        if (rand_int1 == 1) {
            setVariables(aceClover);

        }else if (rand_int1 == 2) {
            setVariables(twoClover);
        }else if (rand_int1 == 3) {
            setVariables(threeClover);
        }else if (rand_int1 == 4) {
            setVariables(fourClover);
        }else if (rand_int1 == 5) {
            setVariables(fiveClover);   
        }else if (rand_int1 == 6) {
            setVariables(sixClover);
        }else if (rand_int1 == 7) {
            setVariables(sevenClover);
        }else if (rand_int1 == 8) {
            setVariables(eightClover);
        }else if (rand_int1 == 9) {
            setVariables(nineClover);
        }else if (rand_int1 == 10) {
            setVariables(tenClover);
        }else if (rand_int1 == 11) {
            setVariables(jackClover);
        }else if (rand_int1 == 12) {
            setVariables(queenClover);
        }else if (rand_int1 == 13) {
            setVariables(kingClover);
        }else {
            System.out.println("Couldn't return any Cards!");
        }
    }
}

What I am trying to do in this class is make an object with certain variables. Then all of those variables are assigned a value,but once they do I want a function that gets a random integer to select a certain object to send information to another class which then displays it on screen.

package MainClasses;
import java.awt.*;
import java.io.*;
import java.util.Random;

import javax.imageio.ImageIO;

public class DrawBoard extends Component{
    AllCards accessor = new AllCards();
    public void paint(Graphics g) { // function to draw onto the window
    Graphics2D g1 = (Graphics2D)g; // the component being used to access and write to the window
    g1.clearRect(0, 0, getWidth(), getHeight()); // clears rectangle every frame
    g1.setBackground(Color.green); // sets background color
    accessor.randomizer();
    g1.drawImage(accessor.imageExtract, 100, 100, null);
    }   
}

This is the function I am using to draw out information from the object. I am not 100% sure it works but from previous builds it was displaying just fine.

package MainClasses;
import java.awt.Color;

import javax.swing.*;
public class Main extends JFrame{

    public static void main(String[] args) {
        JFrame f = new JFrame("Palace");
        f.setSize(1920,1080);
        f.setResizable(false);
        f.setVisible(true);
        f.setDefaultCloseOperation(EXIT_ON_CLOSE);
        f.add(new DrawBoard());

    }

}

This is the class I am using to create the JFrame just in case if it is needed.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

When a new instance of AllCards is attempted to be created, another instance of AllCards is being created in the constructor, which in its turn will call the same constructor and another attempt to create another instance of AllCards will be made, and so on. Hence, the infinite loop towards StackOverFlowError.

Suggested design change

  1. Separate the ideas of "one card" and "all cards" into two separate classes, say Card and the current AllCards.
  2. Move all single card attributes to Card class. It will be then a simple class with just fields and getters and setters.
  3. Let AllCards continue to have fields for each card, but as Card instances instead of the earlier AllCards instances.

Here is the new Card class (You will need to generated getters and setters and use them. I have changed the fields to have default access and used them. That's not the recommended way.)

public class Card {
    Image cardImg = null;// to set image
    String cardImgURL = ""; // get image location
    int cardOrder = 0; // Card order in a hierarchy
    boolean ruleAffect = false;  // if rules is changed can affect
    boolean cardPower = false; // is the card a special card?
    int cardPowerNumber = 0; // which are the powers 1.Go again 2.Clear 3.mirror 4.Put card lower 5.Pick up deck unless have a copy
    public Image imageExtract;
    public String cardImgUrlExtractor;
    public int cardOrderExtract;
    public boolean ruleAffectExtract;
    public boolean cardPowerExtractor;
    public int cardPowerNumberExtractor;

    public Card() {}

}

And the changed AllCards class.

public class AllCards {
    private Image cardImg = null;// to set image
    private String cardImgURL = ""; // get image location
    private int cardOrder = 0; // Card order in a hierarchy
    public boolean ruleAffect = false;  // if rules is changed can affect
    private boolean cardPower = false; // is the card a special card?
    private int cardPowerNumber = 0; // which are the powers 1.Go again 2.Clear 3.mirror 4.Put card lower 5.Pick up deck unless have a copy
    Card aceClover = new Card(); // declared the Clover Ace
    Card twoClover = new Card();
    Card threeClover = new Card();
    Card fourClover = new Card();
    Card fiveClover = new Card();
    Card sixClover = new Card();
    Card sevenClover = new Card();
    Card eightClover = new Card();
    Card nineClover = new Card();
    Card tenClover = new Card();
    Card jackClover = new Card();
    Card queenClover = new Card();
    Card kingClover = new Card();
    public Image imageExtract;
    public String cardImgUrlExtractor;
    public int cardOrderExtract;
    public boolean ruleAffectExtract;
    public boolean cardPowerExtractor;
    public int cardPowerNumberExtractor;

    public AllCards() {



        aceClover.cardImgURL = "C:\Users\Jhosua\eclipse-workspace\Palace Game\bin\All Cards\tile000.png"; // clover ace url
        if(aceClover.ruleAffect == false) {
            aceClover.cardOrder = 1;
        }else {
            aceClover.cardOrder = 14;
        }

        try {

            aceClover.cardImg = ImageIO.read(new File(aceClover.cardImgURL));

        }catch(Exception e) {

            System.out.println("Could't loud the Image! Error : "+e.getMessage());

        }

        twoClover.cardImgURL = "C:\Users\Jhosua\eclipse-workspace\Palace Game\bin\All Cards\tile001.png";
        twoClover.cardOrder = 14;
        twoClover.cardPower = true;
        twoClover.cardPowerNumber = 2;
        try {
            twoClover.cardImg = ImageIO.read(new File(twoClover.cardImgURL));
        }catch(Exception e) {
            System.out.println("Could't loud the Image! Error : "+e.getMessage());
        }

        threeClover.cardImgURL = "C:\Users\Jhosua\eclipse-workspace\Palace Game\bin\All Cards\tile002.png";
        threeClover.cardOrder = 15;
        threeClover.cardPower = true;
        if(threeClover.ruleAffect == false) {
            threeClover.cardPowerNumber = 14;
        }else {
            threeClover.cardPowerNumber = 3;
        }

        try {
            threeClover.cardImg = ImageIO.read(new File(threeClover.cardImgURL));
        }catch(Exception e) {
            System.out.println("Could't loud the Image! Error : "+e.getMessage());
        }

        fourClover.cardImgURL = "C:\Users\Jhosua\eclipse-workspace\Palace Game\bin\All Cards\tile003.png";
        fourClover.cardOrder = 4;
        try {
            fourClover.cardImg = ImageIO.read(new File(fourClover.cardImgURL));
        }catch(Exception e) {
            System.out.println("Could't loud the Image! Error : "+e.getMessage());
        }

        fiveClover.cardImgURL = "C:\Users\Jhosua\eclipse-workspace\Palace Game\bin\All Cards\tile004.png";
        fiveClover.cardOrder = 5;
        try {
            fiveClover.cardImg = ImageIO.read(new File(fiveClover.cardImgURL));
        }catch(Exception e) {
            System.out.println("Could't loud the Image! Error : "+e.getMessage());
        }

        sixClover.cardImgURL = "C:\Users\Jhosua\eclipse-workspace\Palace Game\bin\All Cards\tile005.png";
        sixClover.cardOrder = 6;
        try {
            sixClover.cardImg = ImageIO.read(new File(sixClover.cardImgURL));
        }catch(Exception e) {
            System.out.println("Could't loud the Image! Error : "+e.getMessage());
        }

        sevenClover.cardImgURL = "C:\Users\Jhosua\eclipse-workspace\Palace Game\bin\All Cards\tile006.png";
        sevenClover.cardOrder = 14;
        sevenClover.cardPower = true;
        sevenClover.cardPowerNumber = 4;
        try {
            sevenClover.cardImg = ImageIO.read(new File(sevenClover.cardImgURL));
        }catch(Exception e) {
            System.out.println("Could't loud the Image! Error : "+e.getMessage());
        }

        eightClover.cardImgURL = "C:\Users\Jhosua\eclipse-workspace\Palace Game\bin\All Cards\tile007.png";
        eightClover.cardOrder = 8;
        try {
            eightClover.cardImg = ImageIO.read(new File(eightClover.cardImgURL));
        }catch(Exception e) {
            System.out.println("Could't loud the Image! Error : "+e.getMessage());
        }

        nineClover.cardImgURL = "C:\Users\Jhosua\eclipse-workspace\Palace Game\bin\All Cards\tile008.png";
        nineClover.cardOrder = 9;
        try {
            nineClover.cardImg = ImageIO.read(new File(nineClover.cardImgURL));
        }catch(Exception e) {
            System.out.println("Could't loud the Image! Error : "+e.getMessage());
        }

        tenClover.cardImgURL = "C:\Users\Jhosua\eclipse-workspace\Palace Game\bin\All Cards\tile009.png";
        tenClover.cardOrder = 15;
        tenClover.cardPower = true;
        tenClover.cardPowerNumber = 14;
        tenClover.ruleAffect = true;
        try {
            tenClover.cardImg = ImageIO.read(new File(tenClover.cardImgURL));
        }catch(Exception e) {
            System.out.println("Could't loud the Image! Error : "+e.getMessage());
        }

        jackClover.cardImgURL = "C:\Users\Jhosua\eclipse-workspace\Palace Game\bin\All Cards\tile010.png";
        jackClover.cardOrder = 11;
        try {
            jackClover.cardImg = ImageIO.read(new File(jackClover.cardImgURL));
        }catch(Exception e) {
            System.out.println("Could't loud the Image! Error : "+e.getMessage());
        }

        queenClover.cardImgURL = "C:\Users\Jhosua\eclipse-workspace\Palace Game\bin\All Cards\tile011.png";
        queenClover.cardOrder = 12;
        try {
            queenClover.cardImg = ImageIO.read(new File(queenClover.cardImgURL));
        }catch(Exception e) {
            System.out.println("Could't loud the Image! Error : "+e.getMessage());
        }

        kingClover.cardImgURL = "C:\Users\Jhosua\eclipse-workspace\Palace Game\bin\All Cards\tile012.png";
        kingClover.cardOrder = 13;
        try {
            kingClover.cardImg = ImageIO.read(new File(kingClover.cardImgURL));
        }catch(Exception e) {
            System.out.println("Could't loud the Image! Error : "+e.getMessage());
        }

    }
    public void setVariables(Card x) {
        imageExtract = x.cardImg;
        cardImgUrlExtractor = x.cardImgURL;
        cardOrderExtract = x.cardOrder;
        ruleAffectExtract = x.ruleAffect;
        cardPowerNumberExtractor = x.cardPowerNumber;
        cardPowerExtractor = x.cardPower;
    }
    public void randomizer() {
        Random rand = new Random();
        int rand_int1 = rand.nextInt(15-1)+1;
        System.out.print(rand_int1);
        if (rand_int1 == 1) {
            setVariables(aceClover);

        }else if (rand_int1 == 2) {
            setVariables(twoClover);
        }else if (rand_int1 == 3) {
            setVariables(threeClover);
        }else if (rand_int1 == 4) {
            setVariables(fourClover);
        }else if (rand_int1 == 5) {
            setVariables(fiveClover);   
        }else if (rand_int1 == 6) {
            setVariables(sixClover);
        }else if (rand_int1 == 7) {
            setVariables(sevenClover);
        }else if (rand_int1 == 8) {
            setVariables(eightClover);
        }else if (rand_int1 == 9) {
            setVariables(nineClover);
        }else if (rand_int1 == 10) {
            setVariables(tenClover);
        }else if (rand_int1 == 11) {
            setVariables(jackClover);
        }else if (rand_int1 == 12) {
            setVariables(queenClover);
        }else if (rand_int1 == 13) {
            setVariables(kingClover);
        }else {
            System.out.println("Couldn't return any Cards!");
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...