You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Dennis Putnam <de...@cisco.com> on 2012/10/31 16:51:53 UTC
CompositeConfiguration Checking Existence of Properties.
I am making my first foray into CompositeConfiguration and am struggling
with some basics. I am trying to create a method that returns a
specified property or 'null' if it does not exist:
Public String getProperty(String prop) {
return((String) config.getProperty(prop));
}
The problem is that I keep getting a NullPointerException. I know it is
not 'config' that is null so the exception is a bit confusing. In any
case I also know that the value of 'prop' is a key that does not exist.
So the question really is, how do I determine that a property does not
exist so I can return a null? When I search for checking existence of a
property I get nothing useful and the docs don't seem to indicate a
solution either. TIA.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org
Re: [configuration] Re: CompositeConfiguration Checking Existence of Properties.
Posted by Greg Thomas <gr...@gmail.com>.
>>
>> You can't cast null objects.
>
> This is not directly relevant, but yes, you can cast null to any
> object type, because any object type can be null.
I don't know why I wrote that, but of course you are quite correct. I
suspect I was getting myself muddled with
Double foo = null;
double bar = (double)foo;
which is caused by the unboxing behaviour - i.e. calling foo.doubleValue().
Greg
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org
Re: [configuration] Re: CompositeConfiguration Checking Existence of Properties.
Posted by sebb <se...@gmail.com>.
On 31 October 2012 16:09, Greg Thomas <gr...@gmail.com> wrote:
> On 31 October 2012 15:51, Dennis Putnam <de...@cisco.com> wrote:
>> I am making my first foray into CompositeConfiguration and am struggling
>> with some basics. I am trying to create a method that returns a specified
>> property or 'null' if it does not exist:
>>
>> Public String getProperty(String prop) {
>> return((String) config.getProperty(prop));
>> }
>
> You can't cast null objects.
This is not directly relevant, but yes, you can cast null to any
object type, because any object type can be null.
> Using the same API call, you can re-write the method as
>
> Public String getProperty(String prop) {
> Object val = config.getProperty(prop);
> if( val == null ) {
> return null;
> } else {
> return (String)val;
> }
> }
>
> However, that's a lesson in Java. More practically, why not simply
> call getString(prop); - that returns a String directly.
>
> Greg
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org
Re: [configuration] Re: CompositeConfiguration Checking Existence
of Properties.
Posted by Dennis Putnam <de...@cisco.com>.
Dumb, dumb dumb. I figured it out although I don't know why this
exception did not happen sooner. I suddenly started getting a new
exception. The configuration file could not be found so although
'config' was not null, internally there was no file opened to read.
On 10/31/2012 01:09 PM, Dennis Putnam wrote:
> Hi Greg,
>
> Yes. If only it had been that easy. :-(
>
> On 10/31/2012 01:02 PM, Greg Thomas wrote:
>> On 31 October 2012 16:47, Dennis Putnam<de...@cisco.com> wrote:
>>> FWIW, here is the full traceback.
>>>
>>> Exception in thread "main" java.lang.NullPointerException
>>> at
>>> org.apache.commons.configuration.CompositeConfiguration.getProperty(CompositeConfiguration.java:261)
>> ...
>> Are you sure that your "prop" String is not null?
>>
>> Greg
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail:user-unsubscribe@commons.apache.org
>> For additional commands, e-mail:user-help@commons.apache.org
>>
Re: [configuration] Re: CompositeConfiguration Checking Existence
of Properties.
Posted by Dennis Putnam <de...@cisco.com>.
Hi Greg,
Yes. If only it had been that easy. :-(
On 10/31/2012 01:02 PM, Greg Thomas wrote:
> On 31 October 2012 16:47, Dennis Putnam<de...@cisco.com> wrote:
>> FWIW, here is the full traceback.
>>
>> Exception in thread "main" java.lang.NullPointerException
>> at
>> org.apache.commons.configuration.CompositeConfiguration.getProperty(CompositeConfiguration.java:261)
> ...
> Are you sure that your "prop" String is not null?
>
> Greg
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
--
*Dennis Putnam*, Systems Administrator
Acquisitions Integration
Data Net IT at Cisco
770-236-6922
Re: [configuration] Re: CompositeConfiguration Checking Existence of Properties.
Posted by Greg Thomas <gr...@gmail.com>.
On 31 October 2012 16:47, Dennis Putnam <de...@cisco.com> wrote:
> FWIW, here is the full traceback.
>
> Exception in thread "main" java.lang.NullPointerException
> at
> org.apache.commons.configuration.CompositeConfiguration.getProperty(CompositeConfiguration.java:261)
...
Are you sure that your "prop" String is not null?
Greg
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org
Re: [configuration] Re: CompositeConfiguration Checking Existence
of Properties.
Posted by Dennis Putnam <de...@cisco.com>.
Hi Greg,
Not quite. I don't have any keys set yet so I don't know about the first
line. I am only at the beginning of this and my first step was to
determine if a property exists and if not set it. I switched back to
using 'getString' but have the same exception. FWIW, here is the full
traceback.
Exception in thread "main" java.lang.NullPointerException
at
org.apache.commons.configuration.CompositeConfiguration.getProperty(CompositeConfiguration.java:261)
at
org.apache.commons.configuration.AbstractConfiguration.resolveContainerStore(AbstractConfiguration.java:1171)
at
org.apache.commons.configuration.AbstractConfiguration.getString(AbstractConfiguration.java:1038)
at
org.apache.commons.configuration.AbstractConfiguration.getString(AbstractConfiguration.java:1021)
at KCBSConfig.getProperty(KCBSConfig.java:21)
at ImageTest.main(ImageTest.java:7)
Just to be sure, I added an "if (config==null) .." to my code and it
fails that so 'config' must contain something. If this is a Java issue
rather than misuse of CompositeConfiguration, I'm stuck.
On 10/31/2012 12:24 PM, Greg Thomas wrote:
> On 31 October 2012 16:16, Dennis Putnam<de...@cisco.com> wrote:
>> Thanks for the reply. I had 'getString' originally and got the same error
> So just to confirm,
>
> String foo = config.getString(keythatexists);
> String bar = config.getString(keythatdoesnotexist);
>
> is throwing a NullPointerException on the second line?
>
> If so, that sounds like a bug as it should only throw a
> NoSuchElementException - and that only if
> setThrowExceptionOnMissing(true) is called.
>
> Greg
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org
Re: [configuration] Re: CompositeConfiguration Checking Existence of Properties.
Posted by Greg Thomas <gr...@gmail.com>.
On 31 October 2012 16:16, Dennis Putnam <de...@cisco.com> wrote:
> Thanks for the reply. I had 'getString' originally and got the same error
So just to confirm,
String foo = config.getString(keythatexists);
String bar = config.getString(keythatdoesnotexist);
is throwing a NullPointerException on the second line?
If so, that sounds like a bug as it should only throw a
NoSuchElementException - and that only if
setThrowExceptionOnMissing(true) is called.
Greg
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org
Re: [configuration] Re: CompositeConfiguration Checking Existence
of Properties.
Posted by Dennis Putnam <de...@cisco.com>.
Thanks for the reply. I had 'getString' originally and got the same
error so I changed to 'getProperty' thinking that might fix it. Since I
am not casting when I use 'getString' don't see why I get the same
error. In any case I will use your suggestion even with 'getString'.
On 10/31/2012 12:09 PM, Greg Thomas wrote:
> On 31 October 2012 15:51, Dennis Putnam<de...@cisco.com> wrote:
>> I am making my first foray into CompositeConfiguration and am struggling
>> with some basics. I am trying to create a method that returns a specified
>> property or 'null' if it does not exist:
>>
>> Public String getProperty(String prop) {
>> return((String) config.getProperty(prop));
>> }
> You can't cast null objects. Using the same API call, you can re-write
> the method as
>
> Public String getProperty(String prop) {
> Object val = config.getProperty(prop);
> if( val == null ) {
> return null;
> } else {
> return (String)val;
> }
> }
>
> However, that's a lesson in Java. More practically, why not simply
> call getString(prop); - that returns a String directly.
>
> Greg
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
--
*Dennis Putnam*, Systems Administrator
Acquisitions Integration
Data Net IT at Cisco
770-236-6922
[configuration] Re: CompositeConfiguration Checking Existence of Properties.
Posted by Greg Thomas <gr...@gmail.com>.
On 31 October 2012 15:51, Dennis Putnam <de...@cisco.com> wrote:
> I am making my first foray into CompositeConfiguration and am struggling
> with some basics. I am trying to create a method that returns a specified
> property or 'null' if it does not exist:
>
> Public String getProperty(String prop) {
> return((String) config.getProperty(prop));
> }
You can't cast null objects. Using the same API call, you can re-write
the method as
Public String getProperty(String prop) {
Object val = config.getProperty(prop);
if( val == null ) {
return null;
} else {
return (String)val;
}
}
However, that's a lesson in Java. More practically, why not simply
call getString(prop); - that returns a String directly.
Greg
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org