注册 登录
编程论坛 JAVA论坛

请高手帮我看看

sql377288 发布于 2015-05-10 11:32, 613 次点击
import java.awt.*;
import javax.swing.*;
class Link extends JFrame{
    public static final int ROW=6;
    public static final int COL=7;
    private JPanel p1=new JPanel();
    private JPanel p2=new JPanel();
    private JButton[][] button=new JButton[ROW][COL];
    public Link(){
        this.setSize(600,500);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new BorderLayout());
        p1.setBackground(Color.BLUE);
        p1.setLayout(new GridLayout());
        p1.setPreferredSize(new Dimension(500,500));
        
        for(int i=0;i<ROW;i++){
            for(int j=0;j<COL;j++){
                button[i][j]=new JButton();
                p1.add(button[i][j]);
            }
        }
        p2.setBackground(Color.YELLOW);
        p2.setPreferredSize(new Dimension(100,500));
        
        this.add(p1,BorderLayout.CENTER);
        this.add(p2,BorderLayout.EAST);
        
        this.setVisible(true);
        
    }

}
运行时老是提行:Exception in thread "main" java.lang.NullPointerException
4 回复
#2
calix2015-05-11 13:01
没问题啊
Exception in thread "main" java.lang.NullPointerException
应该是调用的那个main方法有问题
#3
xuewei61112015-05-12 11:27
没Main函数可以运行吗?
#4
wangnannan2015-05-22 11:19
一般报java.lang.NullPointerException的原因有以下几种
字符串变量未初始化;
接口类型的对象没有用具体的类初始化 按照这个思路 自己排查一下问题吧
#5
zq10627879412015-05-24 12:48
我粘贴你的代码,自己新建个main方法,然后运行,没有问题啊????
1