I just found Apache POI library very useful for editing Word files using Java. Specifically, I want to edit a DOCX file using Apache POI\'s XWPF classes. I
The first chunk of code is giing me a NullPointerException, anyone know what is wrong?
run.getText(int position) - from documentation: Returns: the text of this text run or null if not set
Just check if it is not null before calling contains() on it
And btw if you want to replace the text you need to set it in position from which you get it, in this case r.setText(text, 0);. Otherwise text will be added not replaced