Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Problem with throwing an exception...

  1. #1
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Problem with throwing an exception...

    Hi, I'm having a problem where I get an error at a throws declaration.

    This is the error:

    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:

    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.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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
    Last edited by javapenguin; January 22nd, 2011 at 09:48 PM.

  4. #4
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default 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.

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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?

  6. The Following User Says Thank You to javapenguin For This Useful Post:

    The_Mexican (January 23rd, 2011)

  7. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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?

  8. The Following User Says Thank You to javapenguin For This Useful Post:

    The_Mexican (January 23rd, 2011)

  9. #7
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Re: Problem with throwing an exception...

    Quote Originally Posted by javapenguin View Post
    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:

    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

  10. #8
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Problem with throwing an exception...

    Could you post the full code, it might make things clearer.

  11. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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

  12. The Following User Says Thank You to copeg For This Useful Post:

    The_Mexican (January 23rd, 2011)

  13. #10
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default 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!

Similar Threads

  1. [SOLVED] Fixed Null Pointer Exception but still have another problem
    By javapenguin in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 29th, 2010, 10:16 PM
  2. Exception-Based Problem
    By Hax007 in forum Exceptions
    Replies: 1
    Last Post: December 10th, 2009, 03:53 AM
  3. throwing and returning
    By chronoz13 in forum Exceptions
    Replies: 6
    Last Post: October 25th, 2009, 12:00 AM
  4. Throwing arrays as objects
    By Audemars in forum Collections and Generics
    Replies: 1
    Last Post: September 23rd, 2009, 06:29 PM
  5. ymsg exception problem
    By maruf10 in forum Java Networking
    Replies: 0
    Last Post: September 19th, 2009, 01:05 PM