我试图在 pdf 文档中显示“dbproperties”类的变量值。下面的代码说明了这一点,
dbproperties db = new dbproperties();
Chunk text1 = new Chunk(db.index_number);
Paragraph p = new Paragraph();
p.add(text1);
document.close();
但出现错误
non-static variable cannot be referenced from a static content" for "Chunk text1 = new Chunk(db.index_number);
谁能帮我解决这个问题吗?
最佳答案
由于代码不完整,再次尝试猜测:
是否db
是类的非静态成员,但代码是在某些静态方法或代码块中执行的?例如
class YourClass
{
dbproperties db = new dbproperties();
public static void main(String[] args)
{
[...]
Chunk text1 = new Chunk(db.index_number);
Paragraph p = new Paragraph();
p.add(text1);
document.close();
[...]
}
}
在这种情况下,代码无法访问db
,因为代码位于静态方法中(即不在类的单个实例的上下文中),而要访问的成员与单个实例相关联。实例。
在这种情况下,您需要一个类的实例,例如
[...]
YourClass instance = new YourClass();
Chunk text1 = new Chunk(instance.db.index_number);
关于java - itext变量问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14871509/