PicoContainer

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: javascript, sql, xhtml, actionscript, none, html, xml, java
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: javascript, sql, xhtml, actionscript, none, html, xml, java
public boolean isStarted() {
            // add this guardInstRef() line
            guardInstRef();
            return instanceReference.get().started;
        }

Activity

Hide
Paul Hammant added a comment -

thx Mark.

Show
Paul Hammant added a comment - thx Mark.

People

Vote (0)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: