You must have been given a ParasiteBuffer from mainWindow.makeTextureBuffer(). In general, buffer.getScreenshot() doesn’t work unless you know a priori that you have a true offscreen buffer.
For what you are doing, though, it’s probably better to use buffer.addRenderTexture(tex, buffer.RTMCopyTexture) to copy the frame contents to a Texture at the end of the frame. This works for both ParasiteBuffers and true buffers. You can use buffer.clearRenderTextures() to remove it when you have determined that the texture is ready (though this also removes any other buffers you have set up).
Depending on your needs, (ie if you’re willing to wait a frame) there is also the RTMTriggeredCopyRam mode, when you call triggerCopy it will copy the texture to RAM the next frame so that you can then read it out by simply storing the texture in a PNMImage.