Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Plugin to modify page orders of a space. Null pointer error on homepage.

Matías Acosta February 12, 2015

Hello,

I'm  trying to develop a plugin that creates a spaces with a certain page tree in a specific order. For the last task I add a listener in the plugin for the CreateSpaceEvent.  I managed to create the space, but when the listener detects the event and executes the java code it results in a  java.lang.NullPointerException when it gets to this line:

log.warn("WARN: The homepage type is: " + homepage.getType());

 

The  full method I am testing is:

@EventListener
	public void onSpaceCreateEvent(SpaceCreateEvent event){
			Space mySpace = event.getSpace();
		    Page homepage = mySpace.getHomePage();
			log.warn("WARN: The name is: " + mySpace.getName());
			log.warn("WARN: The key is: " + mySpace.getKey());
			log.warn("WARN: The homepage type is: " + homepage.getType()); 		
	}

 

The lines logging the space data execute successfully, but the one corresponding to the homepage throws the null pointer exception.

So, why could getHomePage return  null?

Isn't the homepage created and assigned to the space when the SpaceCreateEvent is detected?

 

Thank you in advance.

 

 

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
Midori
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 13, 2015

You inspect the mySpace object in the debugger.

If the homePage property is not initialized (NULL), then fetch the space again by its ID from spaceManager.

0 votes
Matías Acosta February 13, 2015

Thanks for your answer.

 

I tried now using spaceManager but I'm getting a java.lang.NullPointerException too:

Caused by: java.lang.NullPointerException
	at com.atlassian.confluence.plugins.hello_blueprint.MyBlueprintListener.onSpaceCreateEvent(MyBlueprintListener.java:57)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at com.atlassian.event.internal.SingleParameterMethodListenerInvoker.invoke(SingleParameterMethodListenerInvoker.java:36)
	... 268 more

Line 57 is 

String x = this.spaceManager.getSpace(mySpace.getKey()).getName();

 

Apparently the spaceManager is NULL. Could it be the dependency injection is not working? Here is a snippet of my class, I wonder if the code is allright :

public class MyBlueprintListener {
	private static final Logger log = LoggerFactory.getLogger(MyBlueprintListener.class);
	private SpaceManager spaceManager;
     
	public void setSpaceManager(SpaceManager spaceManager) {
		this.spaceManager = spaceManager;
	}
	

	public MyBlueprintListener(EventPublisher eventPublisher) {
		eventPublisher.register(this); //demonstration only
	}
	@EventListener
	public void onSpaceCreateEvent(SpaceCreateEvent event){
			log.warn("WARN: Created a SPACE.");

			Space mySpace = event.getSpace();
		    Page homepage = mySpace.getHomePage();
			log.warn("WARN: The name is: " + mySpace.getName());
			log.warn("WARN: The key is: " + mySpace.getKey());
			
			String x = this.spaceManager.getSpace(mySpace.getKey()).getName();
			log.warn("WARN: This space name is: " + x); 
	}

}

Thanks in advance.

Matías Acosta February 25, 2015

Solved by injecting the spaceManager in the constructor.

TAGS
AUG Leaders

Atlassian Community Events