PicoContainer
  1. PicoContainer
  2. PICO-353

Cached.isStarted throws NullPointerException when not started

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.8
    • Fix Version/s: 2.9
    • Component/s: PicoContainer (Java)
    • Labels:
      None
    • Testcase included:
      yes
    • Patch Submitted:
      Yes
    • Number of attachments :
      0

      Description

      The following code throws a NullPointerException

      Unable to find source-code formatter for language: test. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml
      	public static class TestStartable implements org.picocontainer.Startable {
      		@Override
      		public void start() {
      			// empty
      		}
      
      		@Override
      		public void stop() {
      			// empty
      		}
      	}
      	
      	@Test
      	public void testIsStartedShouldNotThrowOnNonStartedComponent() {
      		DefaultPicoContainer cont = new DefaultPicoContainer();
      		cont.as(CACHE).addComponent(TestStartable.class);
      		ComponentAdapter<?> adapter = cont.getComponentAdapter(TestStartable.class);
      		Cached cached = adapter.findAdapterOfType(Cached.class);
      
                     // this line throws - instead of returning false
      		assertFalse(cached.isStarted());
      	}
      

      This is caused by the instance not being cached yet (instanceReference.get() == null), and hence the .started member cannot be accessed.

      Suggest to fix by changing RealComponentLifecycle<T>.isStarted() to (this is the 'patch')

      Unable to find source-code formatter for language: fixed realcomponentlifecycle. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml
              public boolean isStarted() {
                  // add this guardInstRef() line
                  guardInstRef();
                  return instanceReference.get().started;
              }
      

        People

        • Assignee:
          Paul Hammant
          Reporter:
          Mark J. Sinke
        • Votes:
          0 Vote for this issue
          Watchers:
          1 Start watching this issue

          Dates

          • Created:
            Updated:
            Resolved: