下面的代碼不會編譯失敗,但是在運行時它說在行號爲20和41的Line上有java.lang.NullPointerException。另外我有點好奇,想知道什麼是空指針異常,運行時會發生什麼?程序中的空指針異常
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Tool
{
private JToolBar toolbar1;
private JToolBar toolbar2;
private JPanel panel;
public Tool()
{
JFrame frame= new JFrame();
panel = new JPanel();
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
JButton one = new JButton("one");
JButton two = new JButton("two");
JButton three = new JButton("three");
JButton four = new JButton("four");
toolbar1 = new JToolBar();
toolbar2 = new JToolBar();
toolbar1.add(one);
toolbar1.add(two);
toolbar2.add(three);
toolbar2.add(four);
toolbar1.setAlignmentX(0);
toolbar2.setAlignmentX(0);
panel.add(toolbar1);
panel.add(toolbar2);
frame.add(panel,BorderLayout.NORTH);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setSize(400,300);
frame.setTitle("ZOOP");
frame.setVisible(true);
}
public static void main (String args[])
{
Tool zoop = new Tool();
}
}
http://download.oracle.com/javase/6/docs/api/java/lang/NullPointerException.html +數百這裏的問題。 – Bozho 2010-08-13 16:17:12
好的,我更新了程序,但爲什麼我的工具欄可停靠,如何禁用該功能也請告訴我一些JToolBar的常用構造函數 – subanki 2010-08-13 16:23:31