2013-03-04 41 views
0

收到此错误:添加JTabbedPane的一个JTabbedPane的错误

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

我无法将rptinvestment选项卡窗格添加到面板tabinvestment

公共类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); 
} 

}

+0

要补充或引用之前初始化你的面板一样'在你的代码tabinvestment'到它?原因我看不到它,你是否遗漏了那部分代码? – 2013-03-04 07:12:10

回答

2

您从不初始化tabinvestment。你将它声明为一个实例变量,但是你永远不会在构造函数中初始化它。有时您需要拨打JTabbedPane tabinvestment = new JTabbedPane()

+0

仍然没有出现,因为它应该是,请看看它, 我编辑了代码 – 2013-03-04 08:25:51

+0

@MuhsinaM更新您的问题,以反映新问题 – MadProgrammer 2013-03-04 08:33:41

2

你试图使用它被布局管理器

JTabbedPane中使用自己的布局逻辑,除非你真的想知道,以创建自己的UI类,不能改变重写绝对定位添加组件。

同样,你设置的框架布局管理器BorderLayout的,这将覆盖任何位置值可能设置(以前以其他方式)

+0

所以我应该怎么做,请推荐 – 2013-03-04 08:31:29

+0

问题是。你有什么想要实现 – MadProgrammer 2013-03-04 08:34:23

+0

我有一个主要的JTabbed窗格与标签现金流量,投资和汇率,低于投资窗格我有两个子选项卡主要是官方,数位和商品 – 2013-03-04 08:39:46