commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oliver Heger <oliver.he...@oliver-heger.de>
Subject Re: [Commons Configuration] How SubsetConfiguration works ?
Date Sat, 06 Sep 2008 14:51:07 GMT
Pierre8rou schrieb:
> Hello,
> 
> 
> With this code :
> ---------------
> 
> package com.test.configuration;
> 
> import org.apache.commons.configuration.BaseConfiguration;
> import org.apache.commons.configuration.SubsetConfiguration;
> 
> public class testCommonsConfig001 {
> 
> 	public static void main(String[] args) {
> 
> 		BaseConfiguration config = new BaseConfiguration();
> 
> 		config.addProperty("prefix1.car01", 11.1);
> 		config.addProperty("prefix1.car02", 22.2);
> 		config.addProperty("prefix1.car03", 33.3);
> 
> 		config.addProperty("prefix2.car01", 11.1);
> 		config.addProperty("prefix2.car02", 22.2);
> 		config.addProperty("prefix2.car03", 33.3);
> 
> 		System.out.println(config.getDouble("prefix1.car01"));
> 		System.out.println(config.getDouble("prefix1.car02"));
> 		System.out.println(config.getDouble("prefix1.car03"));
> 
> 		SubsetConfiguration subsetPre01 = new SubsetConfiguration(config,
> 				"prefix1");
> 
> 		System.out.println(subsetPre01.getDouble("car01"));
> 
> 	}
> }
> 
> 
> I got this console message :
> ---------------------------
> 
> Exception in thread "main" java.util.NoSuchElementException: 'car01' doesn't
> map to an existing object
> 	at
> org.apache.commons.configuration.AbstractConfiguration.getDouble(AbstractConfiguration.java:734)
> 	at
> com.test.configuration.testCommonsConfig001.main(testCommonsConfig001.java:30)
> 
> 
> Why car01 does not exist ?
> 
> Thanks,
> 
> Pierre8r
> 

Instead of instantiating a SubsetConfiguration directly you should use 
the subset() method of the Configuration interface. So change your code from

SubsetConfiguration subsetPre01 = new SubsetConfiguration(config, 
"prefix1");

to

Configuration subsetPre01 = config.subset("prefix1");

This should work.

Oliver

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message