Page 1 of 1

XComponentLoader.loadComponentFromURL returns null

Posted: Wed Nov 13, 2019 8:34 pm
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.

Re: XComponentLoader.loadComponentFromURL returns null

Posted: Thu Nov 14, 2019 6:56 pm
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.