gpt4 book ai didi

java - 空指针异常的未知原因

转载 作者:行者123 更新时间:2023-11-30 08:24:19 24 4
gpt4 key购买 nike

我有以下代码,将 JLabels 添加到 JPanel,然后将此 JPanel 添加到 JFrame,但是我得到了一个 NullPointerException 并且我不知道为什么。我的代码如下:

public void createImage(String view) {

String [] viewSplit = view.split("(?!^)");

if(viewSplit.length == 25) {
fiveByFiveLook.setLayout(null);
fiveByFiveLook.setBackground(Color.WHITE);
fiveByFiveLook.setBounds(48, 75, 404, 404);
fiveByFiveLook.setVisible(true);


ArrayList<String> viewArray = new ArrayList<String>(Arrays.asList(viewSplit));



JLabel fiveByFiveLabels[][] = new JLabel[5][5];

for(int i = 0, k = 0; i <= 4; i++, k = k + 80) {
for(int j = 0, l = 0; j <= 4; j++, l = l + 80) {


if(viewArray.get(0).equals("X")) {
fiveByFiveLabels[i][j].setIcon(new ImageIcon("Blank.jpg"));
fiveByFiveLook.add(fiveByFiveLabels[i][j]);
viewArray.remove(0);
}
else if(viewArray.get(0).equals("#")) {
fiveByFiveLabels[i][j].setIcon(new ImageIcon("Wall.jpg"));
fiveByFiveLook.add(fiveByFiveLabels[i][j]);
viewArray.remove(0);
}
else if(viewArray.get(0).equals(".")) {
fiveByFiveLabels[i][j].setIcon(new ImageIcon("Floor.jpg"));
fiveByFiveLook.add(fiveByFiveLabels[i][j]);
viewArray.remove(0);
}
else if(viewArray.get(0).equals("E")) {
fiveByFiveLabels[i][j].setIcon(new ImageIcon("Exit.jpg"));
fiveByFiveLook.add(fiveByFiveLabels[i][j]);
viewArray.remove(0);
}
else if(viewArray.get(0).equals("G")) {
fiveByFiveLabels[i][j].setIcon(new ImageIcon("Gold.png"));
fiveByFiveLook.add(fiveByFiveLabels[i][j]);
viewArray.remove(0);
}
fiveByFiveLabels[i][j].setBounds(l, k, 85, 85);
}
}
humanGameWindow.add(fiveByFiveLook);
}

else if(viewSplit.length == 49) {

sevenBySevenLook.setLayout(null);
sevenBySevenLook.setBackground(Color.WHITE);
sevenBySevenLook.setBounds(48, 75, 404, 404);
sevenBySevenLook.setVisible(true);

JButton sevenBySevenLabels[][] = new JButton[7][7];

for(int i = 0, k = 0; i <= 6; i++, k = k + 57) {
for(int j = 0, l = 0; j <= 6; j++, l = l + 57) {
sevenBySevenLabels[i][j] = new JButton();
sevenBySevenLabels[i][j].setBounds(l, k, 62, 62);
//sevenBySevenLabels[i][j].setIcon(new ImageIcon("Sword copy.jpg"));
sevenBySevenLook.add(sevenBySevenLabels[i][j]);
}
}

humanGameWindow.add(sevenBySevenLook);
}
}

错误是:

java.lang.NullPointerException
at dodGUI.createImage(dodGUI.java:324)
at dodGUI.handleResponse(dodGUI.java:61)
at dodGUI$1.run(dodGUI.java:79)
at java.lang.Thread.run(Thread.java:744)
Null Pointer2: java.lang.NullPointerException

并出现在这一行:

fiveByFiveLabels[i][j].setBounds(l, k, 85, 85);

请有人告诉我这是为什么:)

最佳答案

线

JLabel fiveByFiveLabels[][] = new JLabel[5][5];

创建一个可以容纳 5x5 标签的空数组。但是里面没有标签。所有数组条目都是 null

插入这一行

for(int i = 0, k = 0; i <= 4; i++, k = k + 80) {
for(int j = 0, l = 0; j <= 4; j++, l = l + 80) {

fiveByFiveLabels[i][j] = new JLabel(); // <--- This one...

并阅读http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

关于java - 空指针异常的未知原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23047023/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com