Hi I have a code that accept input from a user then the program process it and transfer it into Microsoft word, the problem is when I use a debugger the String text returns null even though I called the getText function from the XWPFRun class, here's the sample code that I have written:
@FXML
private void handlePrintButtonAction() {
try {
//Part where it reads and manipulates the content inside the indigency
String IndigencyPath = "C:\\Users\\aliya\\OneDrive\\Desktop\\leng\\cagbaby\\BrgyCert\\Indigency.docx";
XWPFDocument document = new XWPFDocument(new FileInputStream(IndigencyPath));
//User input data is retrieved and assigned to the string variables
String residentFirstName = firstNameField.getText();
String residentMiddleName = middleNameField.getText();
String residentLastName = lastNameField.getText();
String residentSuffix = suffixField.getText();
String residentAge = ageField.getText();
String residentZone = zoneField.getText();
String residentBarangay = barangayField.getText();
String residentMunicipality = municipalityField.getText();
String residentProvince = provinceField.getText();
//Iterates to every paragraph of the document to replace with the actual resident data
for (XWPFParagraph paragraph : document.getParagraphs()) {
List<XWPFRun> runs = paragraph.getRuns();
if (runs != null) {
for (XWPFRun run : runs) {
String text = run.getText(0);
//Method to replace placeholders with actual user-inpu personal data
if (text != null) {
if (text.contains("<<ResidentFirstName>>")) {
text = text.replace("<<ResidentFirstName>>", residentFirstName);
}
if (text.contains("<<ResidentMiddleName>>")) {
text = text.replace("<<ResidentMiddleName>>", residentMiddleName);
}
if (text.contains("<<ResidentLastName>>")) {
text = text.replace("<<ResidentLastName>>", residentLastName);
}
if (text.contains("<<ResidentSuffix>>")) {
text = text.replace("<<ResidentSuffix>>", residentSuffix);
}
if (text.contains("<<ResidentAge>>")) {
text = text.replace("<<ResidentAge>>", residentAge);
}
if (text.contains("<<ResidentZone>>")) {
text = text.replace("<<ResidentZone>>", residentZone);
}
if (text.contains("<<ResidentBarangay>>")) {
text = text.replace("<<ResidentBarangay>>", residentBarangay);
}
if (text.contains("<<ResidentMunicipality>>")) {
text = text.replace("<<ResidentMunicipality>>", residentMunicipality);
}
if (text.contains("<<ResidentProvince>>")) {
text = text.replace("<<ResidentProvince>>", residentProvince);
}
run.setText(text, 0);
}
}
}
}
System.out.println("Print button clicked");
//Document with the personal data is inputted in a temporary file
File temporaryFile = File.createTempFile("resident_cert", ".docx");
FileOutputStream outputStream = new FileOutputStream(temporaryFile);
document.write(outputStream); //flushes data sa docx
outputStream.close();
//Opens the document in the default app MICROSOFT WORD
Desktop.getDesktop().open(temporaryFile);
} catch (IOException e) {
e.printStackTrace();
// Handle any exceptions that occur during the process
}
}
Im expecting that the string text is not equal to null