Problem with throwing an exception...
Hi, I'm having a problem where I get an error at a throws declaration.
This is the error:
Code java:
Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at sun.awt.SunToolkit.isInstanceOf(Unknown Source)
at sun.awt.SunToolkit.isInstanceOf(Unknown Source)
at sun.awt.SunToolkit.isInstanceOf(Unknown Source)
at sun.awt.SunToolkit.isInstanceOf(Unknown Source)
at sun.awt.SunToolkit.isInstanceOf(Unknown Source)
at sun.awt.SunToolkit.isInstanceOf(Unknown Source)
at sun.awt.SunToolkit.isInstanceOf(Unknown Source)
at javax.swing.LookAndFeel.installProperty(Unknown Source)
at javax.swing.plaf.basic.BasicPanelUI.installDefaults(Unknown Source)
at javax.swing.plaf.basic.BasicPanelUI.installUI(Unknown Source)
at javax.swing.JComponent.setUI(Unknown Source)
at javax.swing.JPanel.setUI(Unknown Source)
at javax.swing.JPanel.updateUI(Unknown Source)
at javax.swing.JPanel.<init>(Unknown Source)
at javax.swing.JPanel.<init>(Unknown Source)
at javax.swing.JPanel.<init>(Unknown Source)
at layout.GameClient2.<init>(GameClient2.java:60)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
at layout.GameClient2.<init>(GameClient2.java:109)
Here is the relevant code:
Code java:
public GameClient2() throws Exception{ //Line 60
URL myurl = this.getClass().getResource("/noteicon.png");
Toolkit tk = this.getToolkit();
Image img = tk.getImage(myurl);
frame.setIconImage(img);
}
...
GameClient2 s = new GameClient2(); //Line 109
I can't put line 109 in the throws exception because then it wouldn't be visible for when I use it later in the program.
Re: Problem with throwing an exception...
You're calling something repeatedly I think.
Well...actually it appears it's throwing Exception till it runs out of memory or something.
Why I don't know.
Re: Problem with throwing an exception...
Say, you're not creating a GameClient2 object inside the GameClient2 class are you?
I did that once, caused a StackOverFlowError.
I made an object of the "big" class inside one of its inner classes and it caused that error.
I'm assuming if you create an object of that class, which if it's a GUI or Window or of that type, will call itself inside the object since it's an object of that class, which will again call itself within that object innerly, and so on until the compiler or whatever can't handle it anymore.
Which is in effect
callClassObject;
callClassObject.classObject;
callClassObject.classObject.classObject;
callClassObject.classObject.classObject.classObjec t;
etc
Re: Problem with throwing an exception...
I'm not sure what you mean by the "big" class, but I think I must be doing that. However, there's no other way I can do it since I need this to be visible to places later in the program.
Re: Problem with throwing an exception...
What I meant I had an inner class and was, to make a change to the main class that the inner class was inside, I made an object of the main class inside the inner class, but it basically would've called the JFrame within itself over and over till it ran out of space since it had an instance of itself inside of itself...in essence a sort of infinite loop but not a loop per se.
Why wouldn't it be visible?
Re: Problem with throwing an exception...
Maybe you can't throw Exception with a throws clause, but could you have the method that you're creating this
GameClient2 object throw the Exception instead of putting the GameClientObject itself in a throws clause?
Re: Problem with throwing an exception...
Quote:
Originally Posted by
javapenguin
Maybe you can't throw Exception with a throws clause, but could you have the method that you're creating this
GameClient2 object throw the Exception instead of putting the GameClientObject itself in a throws clause?
I can't do that, this is what it's like:
Code java:
public class GameClient2 extends JPanel implements ActionListener {
public GameClient2() throws Exception{ //Line 60
URL myurl = this.getClass().getResource("/noteicon.png");
Toolkit tk = this.getToolkit();
Image img = tk.getImage(myurl);
frame.setIconImage(img);
}
...
GameClient2 s = new GameClient2(); //Line 109
public static void createAndShowGame() {
...
public void actionPerformed(ActionEvent e) {
s.start();
}
}
}
So it needs to be visible all the way to the s.start(); but it seems the only way to do that is to put it before the other methods but that causes an error :(
Re: Problem with throwing an exception...
Could you post the full code, it might make things clearer.
Re: Problem with throwing an exception...
Javapenguin, I suggest you stop posting messages and advice that contain words or phrases like "I think" or "maybe" - read your recent private message.
The_Mexican, the code you posted isn't complete or compilable. However, it looks as though you are repeatedly creating a GameClient2 in the class GameClient2, which creates a recursive nightmare and stack overflow
Re: Problem with throwing an exception...
Yes I see now, thanks to both of you. What I had to do is simply move the statement into the next method then surround pretty much the entire program in a try/catch block. There is probably another way you're suppose to do this but whatever, this works. Thanks!