Commons Configuration
  1. Commons Configuration
  2. CONFIGURATION-63

[configuration] ConfigurationUtils.locate throws NullPointerException if the context ClassLoader is null

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.3
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None
    • Environment:

      Operating System: Windows XP
      Platform: PC

      Description

      Hello,

      When the locate method attempts to find resource location from the context classpath
      (lines 375-384 in ConfigurationUtils.java) it fails (on line 378) if the context
      ClassLoader cannot be retrieved for the current thread.
      I suppose this bug is quite difficult to reproduce. I came accross it when I and a
      colleague of mine were testing a Java API (kept in one jar) that is called from C++
      application. In the static initializer of a Java class I used the next line to load
      settings:
      config = new PropertiesConfiguration(ConfigurationUtils.locate("properties/routing.
      properties"));
      It worked fine if the class was used from another Java class that was normally loaded
      by JVM (for example my tests passed). But when my Java classes were executed by JVM
      started from C++ application it resulted in the next exception:
      java.lang.NullPointerException
      at org.apache.commons.configuration.ConfigurationUtils.
      locate(ConfigurationUtils.java:378)
      at org.apache.commons.configuration.ConfigurationUtils.
      locate(ConfigurationUtils.java:255)
      ...
      It is obvious from the ConfigurationUtils code that loader variable got null value
      (line 377) for some reason so the next step threw the exception.
      I suppose this bug can be easily fixed (just to add an additional check if loader is
      not null).
      As a workaround I will try to call locate method with a given base parameter (but it
      needs to determine location of my jar so it is a kind of overheads though it would
      work I hope).

      Sorry for my English.

      Hope it is useful,
      Denis

        Activity

        Hide
        Emmanuel Bourg added a comment -
            • COM-2854 has been marked as a duplicate of this bug. ***
        Show
        Emmanuel Bourg added a comment - COM-2854 has been marked as a duplicate of this bug. ***
        Hide
        Emmanuel Bourg added a comment -

        Could you could try to set the context classloader with something like this and
        see if it solves your issue ?

        Thread.currentThread().setContextClassLoader(Configuration.class.getClassLoader());

        Show
        Emmanuel Bourg added a comment - Could you could try to set the context classloader with something like this and see if it solves your issue ? Thread.currentThread().setContextClassLoader(Configuration.class.getClassLoader());
        Hide
        nomus added a comment -

        (In reply to comment #2)
        > Could you could try to set the context classloader with something like this and
        > see if it solves your issue ?
        >
        > Thread.currentThread().setContextClassLoader(Configuration.class.getClassLoader());
        >

        Sorry for a long silence (work and other affairs).

        As you had suggested I added line
        Thread.currentThread().setContextClassLoader(RequestManager.class.getClassLoader());
        just before
        config = new PropertiesConfiguration(ConfigurationUtils.locate("properties/routing.
        properties"));
        And now it works all right.

        Thank you very much.

        Show
        nomus added a comment - (In reply to comment #2) > Could you could try to set the context classloader with something like this and > see if it solves your issue ? > > Thread.currentThread().setContextClassLoader(Configuration.class.getClassLoader()); > Sorry for a long silence (work and other affairs). As you had suggested I added line Thread.currentThread().setContextClassLoader(RequestManager.class.getClassLoader()); just before config = new PropertiesConfiguration(ConfigurationUtils.locate("properties/routing. properties")); And now it works all right. Thank you very much.
        Hide
        Oliver Heger added a comment -

        An additional check was added to avoid NPEs when the TCCL is null. So you don't
        need to set it manually any more.

        Show
        Oliver Heger added a comment - An additional check was added to avoid NPEs when the TCCL is null. So you don't need to set it manually any more.
        Hide
        Oliver Heger added a comment -
            • COM-2852 has been marked as a duplicate of this bug. ***
        Show
        Oliver Heger added a comment - COM-2852 has been marked as a duplicate of this bug. ***

          People

          • Assignee:
            Unassigned
            Reporter:
            nomus
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development