本文介紹了為什么從對象獲取信息時會出現(xiàn)StackOverflow Error?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
你好,我目前正在嘗試用Java創(chuàng)建一個宮殿游戲。我參加了一門單人學(xué)習(xí)課程,但從未真正學(xué)會如何處理堆棧溢出錯誤。我的問題都沒有在互聯(lián)網(wǎng)上找到。當(dāng)前處于可變狀態(tài)
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!");
}
}
}
我在這個類中嘗試做的是創(chuàng)建一個帶有某些變量的對象。然后所有這些變量都被賦值,但一旦它們賦值,我想要一個函數(shù),它獲取一個隨機整數(shù)來選擇某個對象,將信息發(fā)送到另一個類,然后將其顯示在屏幕上。
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);
}
}
這是我用來從對象中提取信息的函數(shù)。我不能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());
}
}
這是我用來創(chuàng)建JFrame的類,以備需要時使用。
推薦答案
嘗試創(chuàng)建AllCards
的新實例時,將在構(gòu)造函數(shù)中創(chuàng)建AllCards
的另一個實例,該構(gòu)造函數(shù)將調(diào)用相同的構(gòu)造函數(shù),并再次嘗試創(chuàng)建AllCards
的另一個實例,依此類推。因此,指向StackOverFlowError
.
的無限循環(huán)
建議的設(shè)計更改
-
將”一張牌”和”所有牌”的概念分為兩類,例如
Card
和當(dāng)前的AllCards
。將所有單卡屬性移至
Card
類。然后,它將是一個僅包含字段、getter和setter的簡單類。讓
AllCards
繼續(xù)為每張卡設(shè)置字段,但作為Card
個實例,而不是以前的AllCards
個實例。
下面是新的Card
類(您將需要生成并使用getter和setter。我已將這些字段更改為具有默認(rèn)訪問權(quán)限并使用它們。這不是推薦的方式。)
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!");
}
}
}
這篇關(guān)于為什么從對象獲取信息時會出現(xiàn)StackOverflow Error?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,