Java-NullPointerException问题
我一直在做一个程序,当我运行它时,我得到一个错误,第43行和第84行有一个NullPointerException。这是代码。我已经在第43行和第84行中添加了注释。我正在尝试制作一个像微软word这样的文字处理器Java-NullPointerException问题,java,Java,我一直在做一个程序,当我运行它时,我得到一个错误,第43行和第84行有一个NullPointerException。这是代码。我已经在第43行和第84行中添加了注释。我正在尝试制作一个像微软word这样的文字处理器 import javax.swing.*; import java.awt.*; public class Graphics { // listing all the components JFrame f1; JPanel colorspanel;
import javax.swing.*;
import java.awt.*;
public class Graphics {
// listing all the components
JFrame f1;
JPanel colorspanel;
JPanel sizepanel;
JPanel fontpanel;
JPanel mainpanel;
JTextField Maintextfield;
JLabel colorlabel;
JLabel sizelabel;
JLabel fontlabel;
JButton colorbuttons[];
JButton sizebuttons[];
JButton fontbuttons[];
Graphics() {
// making instances of panels
colorspanel = new JPanel();
sizepanel = new JPanel();
fontpanel = new JPanel();
mainpanel = new JPanel();
// setting the size of the panels
colorspanel.setSize(216, 144);
sizepanel.setSize(216, 144);
fontpanel.setSize(216, 144);
mainpanel.setSize(612, 756);
// making instances of button
colorbuttons = new JButton[9];
sizebuttons = new JButton[14];
fontbuttons = new JButton[9];
// setting content for buttons
// colorbuttons
colorbuttons[0].setBackground(Color.black);//line 43
colorbuttons[1].setBackground(Color.red);
colorbuttons[2].setBackground(Color.blue);
colorbuttons[3].setBackground(Color.yellow);
colorbuttons[4].setBackground(Color.green);
colorbuttons[5].setBackground(Color.gray);
colorbuttons[6].setBackground(Color.DARK_GRAY);
colorbuttons[7].setBackground(Color.ORANGE);
colorbuttons[8].setBackground(Color.pink);
colorbuttons[9].setBackground(Color.magenta);
// sizebuttons
sizebuttons[0].setText("8");
sizebuttons[1].setText("10");
sizebuttons[2].setText("12");
sizebuttons[3].setText("14");
sizebuttons[4].setText("16");
sizebuttons[5].setText("18");
sizebuttons[6].setText("20");
sizebuttons[7].setText("22");
sizebuttons[8].setText("24");
sizebuttons[9].setText("26");
sizebuttons[10].setText("28");
sizebuttons[11].setText("30");
sizebuttons[12].setText("32");
sizebuttons[13].setText("34");
sizebuttons[14].setText("36");
// fontbuttons
fontbuttons[0].setText("New Times Roman");
fontbuttons[1].setText("Blackadder ITC");
fontbuttons[2].setText("Andy");
fontbuttons[3].setText("Buxton Sketch");
fontbuttons[4].setText("Arial Black");
fontbuttons[5].setText("Comic Sans MS");
fontbuttons[6].setText("Old English Text MT");
fontbuttons[7].setText("SketchFlow Print");
fontbuttons[8].setText("Harlow Solid Italic");
fontbuttons[9].setText("Algerian");
f1.setVisible(true);
}
public static void main(String[] args){
Graphics graphics = new Graphics();//line 84
}
}
您创建了一个数组,但从未用任何东西填充它。您需要在数组中放置按钮。NullPointerException表示您试图引用某个对象,但找到的是null值,而不是具有方法或属性的对象。比如说
Object x = null;
x.toString(); // NPE
vs
在本例中,您创建了一个超过2行的数组;只需在一行中创建所有内容,但不要在其中添加按钮。因此,当您调用colorButtons[0]时,无论您试图访问索引0处引用上的任何内容。但是由于您没有在数组中放入任何内容,因此该引用为null
做一些更像
JButton[] colorButtons = new JButton[9]; // initialize array
for (int i = 0; i < colorButtons.length; i++) {
JButton button = ... // initialize button each time thru
// do any common setup on the buttons
colorButtons[i] = button; // put the button in the array.
}
你没有在colorbuttons数组中放入任何内容!当然是空的。 在这里:
分配一个新的JButton数组,但不分配其中的元素:
colorbuttons = new JButton[9];
应该有一个相应的:
for (int i = 0; i < 9; i++) {
colorbuttons[i]= new JButton(...);
}
否则,您将为按钮数组分配空间,但不会实际初始化每个jbutton。因此,colorbuttons[0]为空,colorbuttons[0]。blah导致NPE。您的JButton数组为空。你只是宣布了而已 通过向数组中添加按钮来填充数组 大概是这样的:
colorbuttons = new JButton[9];
for (int i = 0; i < 9; i++) {
//Your logic here
colobuttons[i] = new JButton();
}
您还应该获得ArrayOutOfBoundsException?是的,我修复了ArrayOutOfBoundsException,因此JFrame f1尚未初始化,这将在稍后引起问题。我添加了注释,因为他刚刚在另一个已关闭的问题中询问了此问题。
for (int i = 0; i < 9; i++) {
colorbuttons[i]= new JButton(...);
}
colorbuttons = new JButton[9];
for (int i = 0; i < 9; i++) {
//Your logic here
colobuttons[i] = new JButton();
}