Issue Details (XML | Word | Printable)

Key: PICO-353
Type: Bug Bug
Status: Closed Closed
Resolution: Fixed
Priority: Major Major
Assignee: Paul Hammant
Reporter: Mark J. Sinke
Votes: 0
Watchers: 1
Operations

If you were logged in you would be able to see more operations.
PicoContainer

Cached.isStarted throws NullPointerException when not started

Created: 12/May/09 02:28 AM   Updated: 12/May/09 06:59 AM   Resolved: 12/May/09 06:59 AM
Component/s: PicoContainer (Java)
Affects Version/s: 2.8
Fix Version/s: 2.9

Time Tracking:
Not Specified

Testcase included: yes
Patch Submitted: Yes


 Description  « Hide

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;
        }


Paul Hammant added a comment - 12/May/09 06:56 AM

thx Mark.