所以我正在为雀科编写一个GUI,我刚刚开始设置它。它还没有功能,因为我还没有写它但我想知道为什么这不显示抛出异常。
package Code;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import javax.swing.*;
/**
*
* @author
*/
public class LabAssign6 extends JFrame {
private JPanel contents, row1, row2, row3, row4;
private JLabel redLabel, greenLabel, blueLabel;
private JTextField rValue, gValue, bValue;
private JButton enter, clear;
public LabAssign6() {
super("Finch GUI");
contents = new JPanel();
contents.setLayout(new GridLayout(3, 1, 5, 5));
row1 = new JPanel();
row2 = new JPanel();
row3 = new JPanel();
redLabel = new JLabel("Red Value 0-255:");
redLabel.setForeground(Color.RED);
rValue = new JTextField(10);
row1.add(redLabel);
row1.add(rValue);
greenLabel = new JLabel("Green Value 0-255:");
greenLabel.setForeground(Color.GREEN);
gValue = new JTextField(10);
row2.add(greenLabel);
row2.add(gValue);
blueLabel = new JLabel("Blue Value 0-255:");
blueLabel.setForeground(Color.BLUE);
bValue = new JTextField(10);
row3.add(blueLabel);
row3.add(bValue);
enter = new JButton("Enter");
clear = new JButton("Clear");
row4.add(enter);
row4.add(clear);
contents.add(row1);
contents.add(row2);
contents.add(row3);
contents.add(row4);
add(contents);
setSize(335, 225);
setVisible(true);
this.setLocation(600, 400);
this.setResizable(false);
}
public static void main(String[] args) {
LabAssign6 finch = new LabAssign6();
finch.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
输出
run:
Exception in thread "main" java.lang.NullPointerException
at Code.LabAssign6.<init>(LabAssign6.java:45)
at Code.LabAssign6.main(LabAssign6.java:61)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)
答案 0 :(得分:2)
row4.add(enter);
您尚未初始化row4
。您应该在初始化其他行的地方初始化它