renturn 错误
1 package ui.cfg; 2 3 import java.awt.BorderLayout; 4 import java.awt.FlowLayout; 5 6 import javax.swing.JButton; 7 import javax.swing.JFrame; 8 import javax.swing.JPanel; 9 import javax.swing.JTabbedPane; 10 11 import util.FrameUtil; 12 13 public class FrameConfig extends JFrame{ 14 15 private JButton btnOk = new JButton("确定"); 16 17 private JButton btnContrl = new JButton("取消"); 18 19 private JButton btnUser = new JButton("应用"); 20 21 public FrameConfig() { 22 //设置布局管理器为“边界布局” 23 this.setLayout(new BorderLayout()); 24 //添加主面板 25 this.add(createMainPanel(),BorderLayout.CENTER); 26 //添加按钮面板 27 this.add(this.createButtonPanel(),BorderLayout.SOUTH); 28 this.setSize(512, 256); 29 //居中 30 FrameUtil.setFrameCenter(this); 31 //TODO测试用 32 this.setDefaultCloseOperation(3); 33 this.setVisible(true); 34 } 35 36 /** 37 * 创建按钮面板 38 */ 39 private JPanel createButtonPanel() { 40 //创建按钮面板,流式布局 41 JPanel jp = new JPanel(new FlowLayout(FlowLayout.RIGHT)); 42 jp.add(this.btnOk); 43 jp.add(this.btnContrl); 44 jp.add(this.btnUser); 45 return jp; 46 } 47 48 /** 49 * 创建主面板 50 * @return 51 */ 52 private JTabbedPane createMainPanel() { 53 return null; 54 } 55 56 public static void main(String[] args) { 57 new FrameConfig(); 58 } 59 }
源码如上,下面是报错:
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at javax.swing.JFrame.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at ui.cfg.FrameConfig.<init>(FrameConfig.java:25)
at ui.cfg.FrameConfig.main(FrameConfig.java:57)
找了十多分钟,终于发现
private JTabbedPane createMainPanel()
{ return null; }
这个方法的renturn出错了,不应该return null,而应该return new JTabbedPane()才对,改完之后运行就没问题了。