gpt4 book ai didi

java - itext变量问题

转载 作者:行者123 更新时间:2023-12-02 07:16:37 25 4
gpt4 key购买 nike

我试图在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com