Details
-
Bug
-
Status: Closed
-
Critical
-
Resolution: Won't Fix
-
1.5.5
-
None
-
None
-
jdk1.6u6
Description
The following code can not be executed in Groovy Console, which complains:
Exception thrown: java.lang.NullPointerException: Cannot invoke method add() on null object java.lang.NullPointerException: Cannot invoke method add() on null object at Demo.<init>(Script7:82)
import java.awt.BorderLayout; import java.awt.Container; import java.awt.Dimension; import java.awt.Toolkit; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; public class Demo extends JFrame{ Container contentPane; ImageIcon img = new ImageIcon("002.jpg"); JPanel paneTop = new JPanel(); JPanel paneMid = new JPanel(); JPanel paneBut = new JPanel(); JPanel paneAll = new JPanel(); JLabel lblTop = new JLabel(); JLabel lblName = new JLabel(); JLabel lblPwd = new JLabel(); JLabel lblApply = new JLabel(); JLabel lblForget = new JLabel(); JLabel lblModel = new JLabel(); JLabel lblNull = new JLabel(); JTextField txtName = new JTextField(15); JPasswordField txtPwd = new JPasswordField(15); JComboBox cmb = new JComboBox(); JCheckBox chk = new JCheckBox(); JButton btnKill = new JButton("查杀木马"); JButton btnSet = new JButton("设置"); JButton btnLogin = new JButton("登录"); public Demo(){ lblTop.setIcon(img); paneTop.add(lblTop); lblName.setText("QQ帐号:"); lblApply.setText("申请帐号 "); lblPwd.setText("QQ密码:"); lblForget.setText("忘记密码?"); lblModel.setText("状态:"); String[] s1 = ["隐身","在线","忙碌"] as String[]; cmb.addItem(s1[0]); cmb.addItem(s1[1]); cmb.addItem(s1[2]); chk.setText("自动登录"); paneMid.add(lblName); paneMid.add(txtName); paneMid.add(lblApply); paneMid.add(lblPwd); paneMid.add(txtPwd); paneMid.add(lblForget); paneMid.add(lblModel); paneMid.add(cmb); paneMid.add(chk); paneBut.add(btnKill); paneBut.add(btnSet); paneBut.add(btnLogin); contentPane = this.getContentPane(); contentPane.add(paneTop,BorderLayout.NORTH); contentPane.add(paneMid,BorderLayout.CENTER); contentPane.add(paneBut,BorderLayout.SOUTH); setTitle("欢迎使用QQ"); setSize(330,240); Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); setLocation((screen.width - getSize().width)/2,(screen.height - getSize().height)/2 ); setVisible(true); setResizable(false); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args){ Demo d = new Demo(); } }
After I commented the codes about contentPane variable, the code can not be executed too, the error message suggest us that the inheritance in Groovy does not work:
Exception thrown: groovy.lang.MissingMethodException: No signature of method: Demo.setLocation() is applicable for argument types: (java.lang.Double, java.lang.Double) values: {475.0, 280.0} groovy.lang.MissingMethodException: No signature of method: Demo.setLocation() is applicable for argument types: (java.lang.Double, java.lang.Double) values: {475.0, 280.0} at Demo.invokeMethod(Script8) at Demo.<init>(Script8:91)
import java.awt.BorderLayout; import java.awt.Container; import java.awt.Dimension; import java.awt.Toolkit; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; public class Demo extends JFrame{ Container contentPane; ImageIcon img = new ImageIcon("002.jpg"); JPanel paneTop = new JPanel(); JPanel paneMid = new JPanel(); JPanel paneBut = new JPanel(); JPanel paneAll = new JPanel(); JLabel lblTop = new JLabel(); JLabel lblName = new JLabel(); JLabel lblPwd = new JLabel(); JLabel lblApply = new JLabel(); JLabel lblForget = new JLabel(); JLabel lblModel = new JLabel(); JLabel lblNull = new JLabel(); JTextField txtName = new JTextField(15); JPasswordField txtPwd = new JPasswordField(15); JComboBox cmb = new JComboBox(); JCheckBox chk = new JCheckBox(); JButton btnKill = new JButton("查杀木马"); JButton btnSet = new JButton("设置"); JButton btnLogin = new JButton("登录"); public Demo(){ lblTop.setIcon(img); paneTop.add(lblTop); lblName.setText("QQ帐号:"); lblApply.setText("申请帐号 "); lblPwd.setText("QQ密码:"); lblForget.setText("忘记密码?"); lblModel.setText("状态:"); String[] s1 = ["隐身","在线","忙碌"] as String[]; cmb.addItem(s1[0]); cmb.addItem(s1[1]); cmb.addItem(s1[2]); chk.setText("自动登录"); paneMid.add(lblName); paneMid.add(txtName); paneMid.add(lblApply); paneMid.add(lblPwd); paneMid.add(txtPwd); paneMid.add(lblForget); paneMid.add(lblModel); paneMid.add(cmb); paneMid.add(chk); paneBut.add(btnKill); paneBut.add(btnSet); paneBut.add(btnLogin); // contentPane = this.getContentPane(); //contentPane.add(paneTop,BorderLayout.NORTH); // contentPane.add(paneMid,BorderLayout.CENTER); // contentPane.add(paneBut,BorderLayout.SOUTH); setTitle("欢迎使用QQ"); setSize(330,240); Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); setLocation((screen.width - getSize().width)/2,(screen.height - getSize().height)/2 ); setVisible(true); setResizable(false); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args){ Demo d = new Demo(); } }