Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes JSF and the fly likes How to troubleshoot Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "How to troubleshoot "duplicate Id for a component" errors?" Watch "How to troubleshoot "duplicate Id for a component" errors?" New topic

How to troubleshoot "duplicate Id for a component" errors?

David Karr

Joined: Jan 06, 2008
Posts: 12
When I'm iterating through building a new page, I often get "duplicate Id for a component" while constructing pieces of the page. When I get this, I have to guess where I'm having an "id" problem. I've never actually specifically set two fields with the same "id" value, this error only really happens when the framework generates two fields that result in the same "id" value. The problem is, how the heck do you figure out what you did wrong that produces that error? I usually have to hunt around on the whole page to figure this out.

For instance, here is a page that I'm working on:
<%@ taglib uri="" prefix="a4j"%>
<%@ taglib uri="" prefix="rich"%>
<%@ taglib uri="" prefix="h"%>
<%@ taglib uri="" prefix="f"%>
<title>TabPanel Experiment</title>
<rich:tabPanel id="tabpanel" switchType="client">
<rich:tab name="obligor" label="Obligor">
<h:panelGrid columns="2" id="obligorFields">
<h:column><h:inputText id="field1"/></h:column>
<rich:tab name="property" label="Property">
<h:panelGrid columns="2" id="propertyFields">
<h:column><h:inputText id="field2"/></h:column>
<rich:tab name="ratings" label="Ratings">
<h:panelGrid columns="2" id="ratingsFields">
<h:column><h:inputText id="field3"/></h:column>

This produces "duplicate Id for a component _id0:_id1". In general, how do I figure out how to fix these errors?
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3381

Hi David,

I am not too sure whether this would help you or not. One plausible solution what i can think of is, don't let the runtime generate an id for your component. You try to always associate an id with the component.

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Abiodun Adisa
Ranch Hand

Joined: Jan 17, 2002
Posts: 495
Raghavan Muthu is right you always try to set the ID of a component when you create this. what i normally do is setting the Id to the component name+ line number where its located. but you can come up with your own style

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17417

This probably indicates that the JSF libraries you are using have bugs in them.

I recommend setting explicit IDs on your page elements as much as is practical. That way if you get a JSF error, it's easier to backtrack it to the defective component (just look for its ID).

Many XML editors will flag duplicate instances of the same user-assigned ID, so that's another plus.

An IDE is no substitute for an Intelligent Developer.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3381

That's great guys. That's how i worked in JSF initially which was 2 yrs back
I agree. Here's the link:
subject: How to troubleshoot "duplicate Id for a component" errors?
It's not a secret anymore!