wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Illegal argument exception: cannot add parent to itself Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Illegal argument exception: cannot add parent to itself" Watch "Illegal argument exception: cannot add parent to itself" New topic
Author

Illegal argument exception: cannot add parent to itself

M Gerber
Greenhorn

Joined: Jul 18, 2012
Posts: 29

This program should accept a donors name, phone number, charity, and abount in a GUI frame. The basic framwork is done, but compile time throws an illegal argument of cannot add parent to itself. I am sooo a greenhorn and searching the 'net has resulted in loss of hair. The code is below. P.S. How do I add code to this thread?



[Added code tags - see UseCodeTags for details]

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4460
    
    8

When you get an exception, part of the message will tell you exactly which line it occurred on. So if you want help, it's useful to tell us which line that was.

But I'd be worried about this line. Trying to add a panel to itself isn't going to work:
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 563
Please also post the error message and stack trace, copied and pasted just as it appears on your end. Part of the 'service' is to help you interpret, understand, and respond to error messages. (And having the error message should save us the usually minor trouble of running the code.)


Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
M Gerber
Greenhorn

Joined: Jul 18, 2012
Posts: 29

Thank all of you hard-working, underpaid, geniuses who contribute to the education of my 2 brain cells. Could you tell me how you putthe code in? I am confused in how you got the line numbers. Here is the stack trace and error codes.

Exception in thread "main" java.lang.IllegalArgumentException: adding container's parent to itself
at java.awt.Container.checkAddToSelf(Container.java:472)
at java.awt.Container.addImpl(Container.java:1083)
at java.awt.Container.add(Container.java:410)
at charitypledgeprogram.CharityPledgeProgramGUI.<init>(CharityPledgeProgramGUI.java:50)
at charitypledgeprogram.CharityPledgeProgramGUI.main(CharityPledgeProgramGUI.java:142)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)

I did not repost the code because of the issue in the first paragraph, and it is already there, but if it will help then I will post it again.

M Gerber
Greenhorn

Joined: Jul 18, 2012
Posts: 29

Sorry, I should have added that I am using NetBeans for the IDE...
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 563
Could you tell me how you put the code in?

The nice person who did that included a link that will take you to the instructions. It looked like: UseCodeTags (<-- click on this link).

Interpreting the error message and stack trace:

The error message or the reason an exception was thrown will be the top line:

Exception in thread "main" java.lang.IllegalArgumentException: adding container's parent to itself

The stack trace that caused the error is beneath. I prefer to start at the bottom, looking for references to my code in each line that starts with an 'at'. There will also be references to other code, usually core Java classes and methods, but they can be to any other code imported to make your code complete.

Inverting your stack trace:

at charitypledgeprogram.CharityPledgeProgramGUI.main(CharityPledgeProgramGUI.java:142)

142 is the line in your main() method that calls the CharityPledgeProgramGUI() constructor,

at charitypledgeprogram.CharityPledgeProgramGUI.<init>(CharityPledgeProgramGUI.java:50)

50 is the line that starts with add then continues on the next few lines:

Component add = charityPanel.add(charityPanel); // <-- three code lines put together, lines 49, 50 and 52

After that, the stack trace references code that you didn't write, all in the class called Container. So, the problem code must be in that line 50 (or lines 49 - 52). As pointed out earlier, those lines say to add charityPanel to charityPanel. Uhmmmm. That sounds like the error message, "adding container's parent to itself." Apparently, that's not a good thing.

So you know what's wrong, but we don't know what you were trying to do to help you fix it. What did you have in mind there?

Maybe you meant to return a JPanel from buildPanel() and then add that to the JFrame. (?)
M Gerber
Greenhorn

Joined: Jul 18, 2012
Posts: 29

You are correct in that I was trying to add the panel to the frame. I removed the first charityPanel from the charityPanel.add (charityPanel) and the file compiles with errors, but it compiles and does not throw any red error messages. The panel now builds and displays. Thanks for you help. Now all I have to do is have it print the text fields to the text area at the bottom and print to a file. More learning...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Illegal argument exception: cannot add parent to itself