java - 将 JTabbedPane 添加到 JTabbedPane 错误

标签 java swing nullpointerexception jtabbedpane absolutelayout

收到此错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Project.reportpane.addComponent(reportpane.java:69)

我无法将 rptinvestment tabbedPane 添加到面板 tabinvestment

公共(public)类reportpane扩展了JPanel{

JTabbedPane rpt;
JPanel tabcashflow;
JPanel tabinvestment;
JPanel tabexchangerates;
JTabbedPane rptinvestment;
JPanel tabofficial;
JPanel tabdem;
JPanel tabcommodities;

public reportpane() {

    rpt = new JTabbedPane();
    tabcashflow = new JPanel();
    tabinvestment = new JPanel();
    tabexchangerates = new JPanel();

    tabofficial = new JPanel();
    tabdem = new JPanel();
    tabcommodities = new JPanel();


    rpt.add("Cashflow", tabcashflow);
    rpt.add("Investement", tabinvestment);
    rpt.add("Exchange rates", tabexchangerates);
    rpt.setBackground(new Color(255, 255, 255));
    rpt.setFont(new Font("sansserif", Font.PLAIN, 14));
    rpt.setTabPlacement(javax.swing.JTabbedPane.TOP);
    rpt.setBorder(BorderFactory.createEmptyBorder());
    rpt.setSize(750, 500);


    rptinvestment = new JTabbedPane();
    rptinvestment.add("Official", tabofficial);
    rptinvestment.add("DEM", tabdem);
    rptinvestment.add("Commodities", tabcommodities);
    rptinvestment.setBackground(new Color(255, 255, 255));
    rptinvestment.setFont(new Font("sansserif", Font.PLAIN, 14));
    rptinvestment.setTabPlacement(JTabbedPane.TOP);
    rptinvestment.setBorder(BorderFactory.createEmptyBorder());
    rptinvestment.setSize(750, 500);

    addComponent(tabinvestment, rptinvestment, 0, 0, 675, 570);
    addComponent(this, rpt, 10, 10, 675, 570);



    this.setLayout(new BorderLayout());
    this.setBackground(Color.WHITE);
    this.setBorder(null);
    this.revalidate();
    this.repaint();

}

public void addComponent(Container container, Component c, int x, int y, int width, int height) {
    c.setBounds(x, y, width, height);
    container.add(c);
}

}

最佳答案

您永远不会初始化 tabinvestment。您将其声明为实例变量,但从未在构造函数中初始化它。有时您需要调用 JTabbedPane tabinvestment = new JTabbedPane()

关于java - 将 JTabbedPane 添加到 JTabbedPane 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15196309/

相关文章:

java - Hibernate 生成额外的表

java - 学习Spring/Java : How To Inject Spring Bean

java - Log4j 日志在错误的文件中滚动(在当天之前,跳过周末)

android - 捕获运行在 Android 上的 native 代码引发的异常

java - 由于 java.lang.ClassNotFoundException : cucumber. runner.TimeServiceEventBus,Serenity/Cucumber 测试未运行

java - 公共(public) Java Swing 应用程序连接到数据库

java - 图形叠加 jlabel 图像 图标

java - 使用剪贴板时 KeyListener 中的奇怪行为

java - 什么是NullPointerException,我该如何解决?

java - Spring-WS:尝试使用 WebServicesParserFactory 设置 NameSpaceAware 时出现 NullPointerException