XComponentLoader.loadComponentFromURL returns null

Java, C++, C#, Delphi... - Using the UNO bridges
Post Reply
ayverma
Posts: 1
Joined: Wed Nov 13, 2019 7:50 pm

XComponentLoader.loadComponentFromURL returns null

Post by ayverma »

Below is the java code used to create XComponentLoader object for .docx file present at a server location using loadComponentFromUrl().

Code: Select all

                        XComponentContext xcomponentcontext = Bootstrap.createInitialComponentContext(null);
			XUnoUrlResolver urlResolver = UnoUrlResolver.create(xcomponentcontext);
			Object initialObject = urlResolver.resolve("uno:socket,host=localhost,port=8100;urp;StarOffice.ServiceManager");
			XMultiComponentFactory xOfficeFactory = (XMultiComponentFactory) UnoRuntime.queryInterface(XMultiComponentFactory.class, initialObject);
			XPropertySet xProperySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xOfficeFactory);
			Object oDefaultContext = xProperySet.getPropertyValue("DefaultContext");
			XComponentContext xOfficeComponentContext = (XComponentContext) UnoRuntime.queryInterface(XComponentContext.class, oDefaultContext);
			Object oDesktop = xOfficeFactory.createInstanceWithContext("com.sun.star.frame.Desktop",xOfficeComponentContext);
			XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(XDesktop.class, oDesktop);
			
                        File f = new File(fileNamePath);
			if (!f.canRead() || f == null || !f.exists()) {
				try {
					errorJson.put("Msg", "Cannot load template:" + fileNamePath);
					errorJson.put("Status", "Error");
					excelDataArr.put(errorJson);
				} catch (JSONException ex) {
					ex.printStackTrace();
				}
				return excelDataArr.toString();
			}
                        xCompLoader = (XComponentLoader) UnoRuntime.queryInterface(com.sun.star.frame.XComponentLoader.class, xDesktop); 
                        String docURL = "file:///" + fileNamePath.replace(File.separatorChar, '/');
		        PropertyValue[] propertyValues = new PropertyValue[0];
			propertyValues = new PropertyValue[1];
			propertyValues[0] = new PropertyValue();
			propertyValues[0].Name = "Hidden";
			propertyValues[0].Value = new Boolean(false);
				
			XComponent xComp = xCompLoader.loadComponentFromURL(docURL, "_blank", 0, propertyValues); //returns null
				
I have read various questions in the forum and couldn't get to know for what reason the method returns null. Alos, please suggest some alternative or permanent fix to the problem.

Thank you.
Last edited by MrProgrammer on Thu Nov 14, 2019 12:29 am, edited 1 time in total.
Reason: Moved from General Discussion forum to External Programs
LibreOffice 5.4 on RedHatEnterpriseLinux 5.6
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: XComponentLoader.loadComponentFromURL returns null

Post by JeJe »

I don't know Java but I'd look at the URL first... make sure its a valid URL and check the file exists.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply