为什么从对象获取信息时会出现StackOverflow Error?

Why am I getting a StackOverflowError when getting information from an object?(为什么从对象获取信息时会出现StackOverflow Error?)

本文介绍了为什么从对象获取信息时会出现StackOverflow Error?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我目前正在尝试用Java创建一个宫殿游戏。我参加了一门单人学习课程,但从未真正学会如何处理堆栈溢出错误。我的问题都没有在互联网上找到。当前处于可变状态 AllCards aceClover = new AllCards(); 它显示存在StackOverFlowError,我不知道原因。

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!");
        }
    }
}

我在这个类中尝试做的是创建一个带有某些变量的对象。然后所有这些变量都被赋值,但一旦它们赋值,我想要一个函数,它获取一个随机整数来选择某个对象,将信息发送到另一个类,然后将其显示在屏幕上。

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);
    }   
}

这是我用来从对象中提取信息的函数。我不能100%确定它是否工作,但从以前的版本来看,它显示得很好。

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

    }

}

这是我用来创建JFrame的类,以备需要时使用。

推荐答案

尝试创建AllCards的新实例时,将在构造函数中创建AllCards的另一个实例,该构造函数将调用相同的构造函数,并再次尝试创建AllCards的另一个实例,依此类推。因此,指向StackOverFlowError.

的无限循环

建议的设计更改

  1. 将"一张牌"和"所有牌"的概念分为两类,例如Card和当前的AllCards
  2. 将所有单卡属性移至Card类。然后,它将是一个仅包含字段、getter和setter的简单类。
  3. AllCards继续为每张卡设置字段,但作为Card个实例,而不是以前的AllCards个实例。

下面是新的Card类(您将需要生成并使用getter和setter。我已将这些字段更改为具有默认访问权限并使用它们。这不是推荐的方式。)

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() {}

}

和更改后的AllCards类。

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!");
        }
    }
}

这篇关于为什么从对象获取信息时会出现StackOverflow Error?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:为什么从对象获取信息时会出现StackOverflow Error?

基础教程推荐