Java循环中反射调用方法无法获取属性值及iText表格渲染问题求助
Let's tackle your issues step by step, starting with the reflection problem that's blocking you.
1. Why Reflection Calls Return Null/Default Values
Looking at your test code, there are two critical issues causing this behavior:
a. You're Invoking Getters on a New Empty Object
In your value assignment line, you're creating a brand-new CloudHolder instance instead of using the one you already populated with values:
// ❌ Wrong: new CloudHolder() is empty, no set values value = String.valueOf(method.invoke(new CloudHolder(),null)) ; // ✅ Correct: use the pre-configured object from itemsList value = String.valueOf(method.invoke(itemsList.get(i), null)) ;
This explains why you're seeing default values like 0.0 for Carat (default float value) and null for String fields—you're querying an uninitialized object, not the one you called setCarat()/setCategory() on.
b. Potential Case Mismatch Between Column Names and Property Names
Check that the col values in your selectedColumns exactly match the property names in CloudHolder, including capitalization. For example:
- Your
CloudHolderhas a propertyDoc_No, but your test usesDoc_noinselectedColumns. - The auto-generated getter for
Doc_Nowould begetDoc_No(), but your code tries to callgetDoc_no()—this would throw aNoSuchMethodException(or silently fail if your error handling skips it).
Fix the column names to match the exact property casing, and your reflection calls will pull the correct values.
2. iText 5 Questions
a. Why table.writeSelectedRows() Has No Output
This method writes a subset of table rows directly to a PdfContentByte canvas, not to the main Document by default. Common reasons for no output:
- Missing correct canvas reference: You need to pass the
PdfWriter's direct content canvas, e.g.,writer.getDirectContent(). - Incorrect coordinates: The
leftandtopparameters define where the table is drawn on the page. If these are outside the page bounds (e.g., negative values or beyond the page height), the table won't be visible. - Document not opened/closed: Ensure you've called
document.open()before writing, anddocument.close()afterward—iText flushes content to the file only when the document is closed. - Table not properly configured: If the table has no rows, or column widths are set to 0, it won't render.
b. Understanding the currentpos <= 149 Pagination Logic
In iText, currentpos is almost certainly tracking the current Y-coordinate on the page (how far down you've drawn content). The threshold 149 is a custom value calculated to prevent content from being drawn below the page's bottom margin.
For example, on an A4 page (height = 842 units), if you have a bottom margin of 50 units, 149 might represent the minimum Y-coordinate where there's enough space left to draw a table row without cutting it off. When currentpos drops to this value, the code triggers a new page to continue rendering the table, ensuring rows don't get clipped at the bottom of the page.
内容的提问来源于stack exchange,提问作者Pemba Tamang

