Unsatisfied query for interface of type com.sun.star.text.XTextTable!

I want to create a table in a Writer document. For this, I am doing the following:

Reference<XComponentContext> xContext(::cppu::bootstrap());
Reference<XMultiComponentFactory> xServiceManager = xContext->getServiceManager();
Reference<XInterface> xDesktop = xServiceManager->createInstanceWithContext(OUString("com.sun.star.frame.Desktop"), xContext);
Reference<XDesktop2> xDesktop2(xDesktop, UNO_QUERY_THROW);
Reference<XComponent> xComponent = xDesktop2->loadComponentFromURL(
      "private:factory/swriter",
      OUString::createFromAscii("_blank"), 0,
      Sequence <::com::sun::star::beans::PropertyValue>());
Reference<XTextDocument> xTextDocument(xComponent, UNO_QUERY_THROW);
try {
  Reference<XMultiComponentFactory> xServiceFactory(xContext->getServiceManager());
  // Create a new text table
  Reference<XInterface> xInterface(xServiceFactory>createInstanceWithContext("com.sun.star.text.TextTable", xContext));
  // Query for the XTextTable interface
  Reference<XTextTable> xTextTable(xInterface, UNO_QUERY_THROW);
  // Check if the query was successful
  if (xTextTable.is()) {
            // Interface query succeeded, proceed with your operations
            // ...
            fprintf(stdout, "XTextTable is\n");
  }
  else
  {
            // Interface query failed
            fprintf(stdout, "failed to query XTextTable interface\n");
  }
}
catch (Exception& e)
{
  fprintf(stdout, "exception: %s\n", OUStringToOString(e.Message, RTL_TEXTENCODING_UTF8).getStr());
}

But I get a runtime exception: unsatisfied query for interface of type com.sun.star.text.XTextTable!

What am I doing wrong here?

Without looking thoroughly, the first thing is using UNO_QUERY_THROW, and a check after that. The UNO_QUERY_THROW Reference constructor makes sure that the passed object implements the wanted interface, and throws if not. So in the following check, xTextTable.is() can’t be false - in those cases, an exception is thrown, which you see; and you either need to implement a specific exception handler (if you need to handle it specially), or use UNO_QUERY.

Then, if I recall correctly, you can’t create tables outside of a document. So you need not a standalone service manager, but you need to use the component’s service factory.

yeah… but the exception indicates that the line

Reference<XTextTable> xTextTable(xInterface, UNO_QUERY_THROW);

did not work. What is wrong here?

Wrong is not there, but a line before. createInstanceWithContext failed, returned an empty Reference (by the way, the code in the question is wrong, likely manually edited in the question - xServiceFactory>createInstanceWithContext can’t compile).

yeah… i pulled out the lines from my code. In the mentioned line, while editing, i might have deleted the ‘hyphen’.

Can you point me to any resource which specifically creates a text table?

As I wrote: try using the XMultiServiceFactory from the component, not XMultiComponentFactory from xContext->getServiceManager().

https://opengrok.libreoffice.org/search?full="createInstance(com.sun.star.text.TextTable"&project=core&sort=fullpath&n=250

This solves my temporary problem.

On a general note, the “Unsatisfied query for interface of type *” tells you exactly that: the providing object does not satisfy the requirements of the left hand side of the statement.