You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java循环中反射调用方法无法获取属性值及iText表格渲染问题求助

Fixing Reflection Null Values & iText 5 Questions

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 CloudHolder has a property Doc_No, but your test uses Doc_no in selectedColumns.
  • The auto-generated getter for Doc_No would be getDoc_No(), but your code tries to call getDoc_no()—this would throw a NoSuchMethodException (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 left and top parameters 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, and document.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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 17:48:11