Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Sirius » NullPointerException when I create session using aird file
NullPointerException when I create session using aird file [message #1847337] Sat, 23 October 2021 03:43 Go to next message
Ts Bao is currently offline Ts BaoFriend
Messages: 7
Registered: August 2021
Junior Member
hi,I use the following code
64 URI airduri = URI.createPlatformResourceURI(airdfile.getFullPath().toString(), true);
65 Session siriussession =SessionManager.INSTANCE.getSession(airduri , new NullProgressMonitor());
66 siriussession.save(new NullProgressMonitor());
67 siriussession.open(new NullProgressMonitor());

But I encountered such an Exception
Caused by: java.lang.NullPointerException
at org.eclipse.sirius.common.tools.api.resource.ResourceSetFactory.createFactory(ResourceSetFactory.java:77)
	at org.eclipse.sirius.business.internal.session.SessionFactoryImpl.prepareEditingDomain(SessionFactoryImpl.java:113)
	at org.eclipse.sirius.business.internal.session.SessionFactoryImpl.createSession(SessionFactoryImpl.java:82)
	at org.eclipse.sirius.business.internal.session.SessionManagerImpl.getSession(SessionManagerImpl.java:271)
	at utils.GenreateImages.set(GenreateImages.java:65)

The code in line 65 prompts an Exception. How can I modify it?
Re: NullPointerException when I create session using aird file [message #1847476 is a reply to message #1847337] Thu, 28 October 2021 13:06 Go to previous messageGo to next message
Glenn Plouhinec is currently offline Glenn PlouhinecFriend
Messages: 22
Registered: April 2020
Junior Member
Hi Ts Bao,
Did you create the session before trying to get it ?
Here is a code snippet from org.eclipse.sirius.tests.support.api.SiriusTestCase.createSession(URI) that may help you:
    /**
     * Create and open a session from this URI.
     *
     * @param sessionResourceURI
     *            the URI of the session to create.
     */
    protected void createSession(final URI sessionResourceURI) {
        WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {
            @Override
            protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException {
                SessionCreationOperation sessionCreationOperation = new DefaultLocalSessionCreationOperation(sessionResourceURI, new NullProgressMonitor());
                sessionCreationOperation.execute();
                session = sessionCreationOperation.getCreatedSession();
                // open UI session part
                final IEditingSession editingSession = SessionUIManager.INSTANCE.getOrCreateUISession(session);
                editingSession.open();
            }
        };
        try {
            operation.run(new NullProgressMonitor());
        } catch (InvocationTargetException e) {
            throw new RuntimeException("Impossible to create the session.", e.getCause());
        } catch (InterruptedException e) {
            throw new RuntimeException("Impossible to create the session.", e);
        }
    }


Another possible error source: the URI is not valid. Using getFullPath() can return an absolute path to the file as "C:\Users\user1\eclipse\workspace\MyProject.aird". Be careful to have a relative path such as "/MyProject/MyProject.aird". It depends on the way you retrieve your airdFile, but it can be a source of bugs in the future.

Regards,


Glenn Plouhinec - Obeo

Need training or professional services for Sirius?
http://www.obeodesigner.com/sirius
Re: NullPointerException when I create session using aird file [message #1847493 is a reply to message #1847476] Fri, 29 October 2021 03:02 Go to previous messageGo to next message
Ts Bao is currently offline Ts BaoFriend
Messages: 7
Registered: August 2021
Junior Member
hi,Glenn Plouhinec. Appreciate your reply.
I printed my airduri, and the result was
platform:/resource/LibraryMS/RequirementsModel/library.aird

I tried the code you provided
public void createSession(final URI sessionResourceURI) {
		
        WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {
            @Override
            protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException {
                SessionCreationOperation sessionCreationOperation = new DefaultLocalSessionCreationOperation(sessionResourceURI, new NullProgressMonitor());
 59               sessionCreationOperation.execute();
                session = sessionCreationOperation.getCreatedSession();
                // open UI session part
                final IEditingSession editingSession = SessionUIManager.INSTANCE.getOrCreateUISession(session);
                editingSession.open();
            }
        };
        try {
            operation.run(new NullProgressMonitor());
        } catch (InvocationTargetException e) {
            throw new RuntimeException("Impossible to create the session.", e.getCause());
        } catch (InterruptedException e) {
            throw new RuntimeException("Impossible to create the session.", e);
        }
}

but I encountered almost the same problem.
Caused by: java.lang.NullPointerException
	at org.eclipse.sirius.common.tools.api.resource.ResourceSetFactory.createFactory(ResourceSetFactory.java:77)
	at org.eclipse.sirius.business.internal.session.SessionFactoryImpl.prepareEditingDomain(SessionFactoryImpl.java:113)
	at org.eclipse.sirius.business.internal.session.SessionFactoryImpl.createSession(SessionFactoryImpl.java:82)
	at org.eclipse.sirius.business.api.session.DefaultLocalSessionCreationOperation.execute(DefaultLocalSessionCreationOperation.java:71)
	at utils.GenreateImages$1.execute(GenreateImages.java:59)

the code of org.eclipse.sirius.business.internal.session.SessionFactoryImpl.createSession is
public Session createSession(final URI sessionResourceURI, IProgressMonitor monitor) throws CoreException {
 82       final TransactionalEditingDomain ted = prepareEditingDomain(sessionResourceURI);

        boolean alreadyExistingResource = ted.getResourceSet().getURIConverter().exists(sessionResourceURI, null);
        Session session = null;
        if (alreadyExistingResource) {
            session = loadSessionModelResource(sessionResourceURI, ted, monitor);
        } else {
            session = createSessionResource(sessionResourceURI, ted, true, monitor);
        }
        return session;
}

the code of at org.eclipse.sirius.business.internal.session.SessionFactoryImpl.prepareEditingDomain is
protected final TransactionalEditingDomain prepareEditingDomain(final URI sessionResourceURI) {
113        ResourceSet set = ResourceSetFactory.createFactory().createResourceSet(sessionResourceURI);
        final TransactionalEditingDomain transactionalEditingDomain = EditingDomainFactoryService.INSTANCE.getEditingDomainFactory().createEditingDomain(set);

        // Configure the resource set, its is done here and not before the
        // editing domain creation which could provide its own resource set.
        set = transactionalEditingDomain.getResourceSet();

        set.getLoadOptions().put(DescriptionResourceImpl.OPTION_USE_URI_FRAGMENT_AS_ID, true);

        configureDomain(transactionalEditingDomain, sessionResourceURI);

        return transactionalEditingDomain;
}

I still don't know where I made a mistake. How should I correct it?
Regards
Re: NullPointerException when I create session using aird file [message #1847496 is a reply to message #1847493] Fri, 29 October 2021 07:40 Go to previous messageGo to next message
Pierre-Charles David is currently offline Pierre-Charles DavidFriend
Messages: 703
Registered: July 2009
Senior Member
Hi,

The line where the NPE occurs according to the stack trace (ResourceSetFactory.java:77) is:

} catch (final ClassCastException e) {
    DslCommonPlugin.getDefault().error(MessageFormat.format(Messages.ResourceSetFactory_creationError, configElement.getAttribute(CLASS_ATTRIBUTE)), e);
}


It's very strange something can be null here, unless maybe we are very early during the platform startup and some things are not yet completely initialized.
Whatever the cause of the NPE is here, this line is only executed at all if a ClassCastException has been thrown earlier, which can only happen at

ResourceSetFactory contributed = (ResourceSetFactory) configElement.createExecutableExtension(CLASS_ATTRIBUTE);


I don't know if it makes sense in your context, but from the information we have it looks like:

  • you (on someone/something in your execution context) registered a custom ResourceSetFactory using the org.eclipse.sirius.common.resourceSetFactory extension point, but the class provided is not a subclass of org.eclipse.sirius.common.tools.api.resource.ResourceSetFactory.
  • for some reason, the error handling code which is supposed to report this error fails itself with an NPE.


Really the best way to debug this is for you to put a breakpoint on ResourceSetFactory.java:77, trigger the bug and then:

  • inspect the ClassCastException to see what it the root issue;
  • find out which part of the line in question is null and triggers the NPE, and report that here so we can understand (and fix) it.


Regards,
Pierre-Charles David


Pierre-Charles David - Obeo

Need training or professional services for Sirius?
http://www.obeodesigner.com/sirius
Re: NullPointerException when I create session using aird file [message #1847506 is a reply to message #1847496] Fri, 29 October 2021 13:56 Go to previous message
Ts Bao is currently offline Ts BaoFriend
Messages: 7
Registered: August 2021
Junior Member
Thank you,David
I put a breakpoint on ResourceSetFactory.java:77, and debug as Eclipse Application.
The attached picture shows the information in the Variables column.
index.php/fa/41204/0/
The detailed information of e is:
java.lang.ClassCastException: class org.eclipse.sirius.common.xtext.internal.XtextResourceSetFactory cannot be cast to class org.eclipse.sirius.common.tools.api.resource.ResourceSetFactory (org.eclipse.sirius.common.xtext.internal.XtextResourceSetFactory is in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @4587c8ff; org.eclipse.sirius.common.tools.api.resource.ResourceSetFactory is in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @4c844b27)
Previous Topic:CreateRepresentation API call fails with 'No handler found for event: CreateRepresentationInput'
Next Topic:Problem When Calling Services
Goto Forum:
  


Current Time: Fri Apr 19 16:57:35 GMT 2024

Powered by FUDForum. Page generated in 0.02781 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top