Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JTabbedPane提供空指针异常_Java_Arrays_Swing_Oop - Fatal编程技术网

Java JTabbedPane提供空指针异常

Java JTabbedPane提供空指针异常,java,arrays,swing,oop,Java,Arrays,Swing,Oop,我将JTabbedPane与不同的面板一起使用,并将gridLayout用于我将添加的按钮数组。我试着把按钮循环成更简单的代码。但是我得到了一个空指针异常,我不知道我是怎么得到这种错误的。 这是我的密码 import javax.swing.*; import javax.swing.border.CompoundBorder; import java.awt.*; public class HotelRoomsGUI extends JPanel{ private JTabbedPan

我将JTabbedPane与不同的面板一起使用,并将gridLayout用于我将添加的按钮数组。我试着把按钮循环成更简单的代码。但是我得到了一个空指针异常,我不知道我是怎么得到这种错误的。 这是我的密码

import javax.swing.*;
import javax.swing.border.CompoundBorder;
import java.awt.*;
public class HotelRoomsGUI extends JPanel{
    private JTabbedPane mainJTP;
    private JPanel classicTab,deluxeTab,presidentialTab,classicSubPanel,deluxeSubPanel,presidentialSubPanel;
    private String classicRoomNo[] = {"101","102","103","104","105","106","107","108","109","101","111","112"};
    private String deluxeRoomNo[] = {"201","202","203","204","205","206","207","208","209","201","211","212"};
    private String presidentialRoomNo[] = {"301","302","303","304","305","306","307","308","309","301","311","312"};
    private JButton[] classicRoom, deluxeRoom, presidentialRoom;
    public HotelRoomsGUI(){

        setLayout(null);
        setBackground(new Color(90,90,90));
        add(tabbedPane());

    }
    public JPanel classic()
    {
        classicTab = new JPanel();
        classicTab.setBackground(new Color(70,70,70));
        classicTab.setLayout(null);
        classicSubPanel();
        return classicTab;
    }
    public JPanel classicSubPanel()
    {
        classicSubPanel = new JPanel();
        classicSubPanel.setBounds(10,10,605,455);
        classicSubPanel.setLayout(new GridLayout(4,3,10,10));
        classicSubPanel.setBackground(new Color(70,70,70));
        classicTab.add(classicSubPanel);
        rooms();
        return classicTab;
    }
    public JPanel deluxe()
    {
        deluxeTab = new JPanel();
        deluxeTab.setBackground(new Color(70,70,70));
        deluxeTab.setLayout(null);
        deluxeSubPanel();
        return deluxeTab;
    }
    public JPanel deluxeSubPanel()
    {
        deluxeSubPanel = new JPanel();
        deluxeSubPanel.setBounds(10,10,605,455);
        deluxeSubPanel.setLayout(new GridLayout(4,3,10,10));
        deluxeSubPanel.setBackground(new Color(70,70,70));
        deluxeTab.add(deluxeSubPanel);
        rooms();
        return deluxeSubPanel;
    }
    public JPanel presidential()
    {
        presidentialTab = new JPanel();
        presidentialTab.setBackground(new Color(70,70,70));
        presidentialTab.setLayout(null);
        presidentialSubPanel();
        return presidentialTab;
    }
    public JPanel presidentialSubPanel()
    {
        presidentialSubPanel = new JPanel();
        presidentialSubPanel.setBounds(10,10,605,455);
        presidentialSubPanel.setLayout(new GridLayout(4,3,10,10));
        presidentialSubPanel.setBackground(new Color(70,70,70));
        presidentialTab.add(presidentialSubPanel);
        rooms();
        return presidentialSubPanel;
    }
    public JTabbedPane tabbedPane()
    {
        UIManager.put("TabbedPane.selected", Color.ORANGE); 
        mainJTP = new JTabbedPane();
        mainJTP.setBackground(Color.WHITE); 
        mainJTP.setBounds(3,1,630,500);
        mainJTP.addTab("Classic",classic());
        mainJTP.addTab("Deluxe",deluxe());
        mainJTP.addTab("Presidential",presidential());
        return mainJTP;
    }
    public void rooms()
    {
        JButton presidentialRoom[] = new JButton[presidentialRoomNo.length];        
        JButton deluxeRoom[] = new JButton[deluxeRoomNo.length];
        JButton classicRoom[] = new JButton[classicRoomNo.length];
        for(int x = 0;x<classicRoomNo.length;x++){
            //classic rooms
            ImageIcon imageC = new ImageIcon("C:\\Users\\John\\workspace\\SystemTest\\src\\Images\\classicRooms.JPG"); // image
            classicRoom[x] = new JButton(classicRoomNo[x],imageC);
            classicRoom[x].setBackground(Color.WHITE);
            classicRoom[x].setBorder(new CompoundBorder(BorderFactory.createEtchedBorder(Color.WHITE,Color.GRAY),
                    BorderFactory.createEtchedBorder(Color.WHITE,Color.GRAY)));
            classicSubPanel.add(classicRoom[x]);
            //deluxe rooms
            ImageIcon imageD = new ImageIcon("C:\\Users\\John\\workspace\\SystemTest\\src\\Images\\deluxeRooms.JPG"); // image
            deluxeRoom[x] = new JButton(deluxeRoomNo[x],imageD);
            deluxeRoom[x].setBackground(Color.WHITE);
            deluxeRoom[x].setBorder(new CompoundBorder(BorderFactory.createEtchedBorder(Color.WHITE,Color.GRAY),
                    BorderFactory.createEtchedBorder(Color.WHITE,Color.GRAY)));
            deluxeSubPanel.add(deluxeRoom[x]);
            //presidential rooms
            ImageIcon imageP = new ImageIcon("C:\\Users\\John\\workspace\\SystemTest\\src\\Images\\presidentialRooms.JPG"); // image
            presidentialRoom[x] = new JButton(deluxeRoomNo[x],imageP);
            presidentialRoom[x].setBackground(Color.WHITE);
            presidentialRoom[x].setBorder(new CompoundBorder(BorderFactory.createEtchedBorder(Color.WHITE,Color.GRAY),
                    BorderFactory.createEtchedBorder(Color.WHITE,Color.GRAY)));
            presidentialSubPanel.add(presidentialRoom[x]);

        }
    }
}
import javax.swing.*;
导入javax.swing.border.CompoundBorder;
导入java.awt.*;
公共级酒店客房GUI扩展JPanel{
专用JTabbedPane mainJTP;
私人JPanel classicTab、deluxeTab、presidentialTab、classicSubPanel、deluxeSubPanel、presidentialSubPanel;
私有字符串classicRoomNo[]={“101”、“102”、“103”、“104”、“105”、“106”、“107”、“108”、“109”、“101”、“111”、“112”};
私有字符串deluxeRoomNo[]={“201”、“202”、“203”、“204”、“205”、“206”、“207”、“208”、“209”、“201”、“211”、“212”};
私人字符串presidentialRoomNo[]={“301”、“302”、“303”、“304”、“305”、“306”、“307”、“308”、“309”、“301”、“311”、“312”};
私人按钮[]古典室、消光室、总统室;
公共酒店客房{
setLayout(空);
挫折背景(新颜色(90,90,90));
添加(选项卡窗格());
}
公共JPanel classic()
{
classicTab=新的JPanel();
classicTab.挫折背景(新颜色(70,70,70));
classicTab.setLayout(空);
classicSubPanel();
返回classicTab;
}
公共JPanel classicSubPanel()
{
classicSubPanel=newJPanel();
classicSubPanel.立根(10,10605455);
classicSubPanel.setLayout(新网格布局(4,3,10,10));
classicSubPanel.setBackground(新颜色(70,70,70));
添加(classicSubPanel);
房间();
返回classicTab;
}
公共JPanel豪华版()
{
deluxeTab=新的JPanel();
退避地(新颜色(70,70,70));
deluxeTab.setLayout(空);
deluxeSubPanel();
返回deluxeTab;
}
公共JPanel deluxeSubPanel()
{
deluxeSubPanel=新的JPanel();
消光亚板收进边界(10,10605455);
deluxeSubPanel.setLayout(新网格布局(4,3,10,10));
新色(70,70,70));
deluxeTab.add(deluxeSubPanel);
房间();
返回子面板;
}
公共JPanel总统
{
presidentialTab=新JPanel();
总统选项卡挫折背景(新颜色(70,70,70));
presidentialTab.setLayout(空);
主席分板();
返回主席选项卡;
}
公共JPanel主席分板()
{
presidentialSubPanel=新的JPanel();
主席分组会议(10,10605455);;
总统副面板设置布局(新网格布局(4,3,10,10));
总统副面板挫折背景(新颜色(70,70,70));
主席选项卡。添加(主席子面板);
房间();
返回主席子面板;
}
公共JTabbedPane选项卡窗格()
{
UIManager.put(“TabbedPane.selected”,Color.ORANGE);
mainJTP=新JTabbedPane();
mainJTP.挫折地面(颜色:白色);
主要JTP立根(3,1630500);
mainJTP.addTab(“Classic”,Classic());
mainJTP.addTab(“豪华”,豪华());
mainJTP.addTab(“Presidential”,Presidential());
返回mainJTP;
}
公共休息室()
{
JButton presidentialRoom[]=新JButton[presidentialRoom编号长度];
JButton deluxeRoom[]=新JButton[deluxeRoomNo.length];
JButton classicRoom[]=新JButton[classicRoomNo.length];

对于(int x=0;x您将获得一个
NullPointerException
作为
deluxeSubPanel
在调用
rooms()时未实例化
当调用:

deluxeSubPanel.add(deluxeRoom[x]);

作为一般指导原则,在创建具有依赖组件的组件时,最好使用自顶向下的方法。这将使创建顺序-
JTabbedPane
-
JPanel
-子组件。

当调用
roo时,
deluxeSubPanel
未被实例化,您将得到一个
NullPointerException
ms()
调用时:

deluxeSubPanel.add(deluxeRoom[x]);

一般来说,在使用依赖组件创建组件时,最好使用自顶向下的方法。这将使创建顺序-
JTabbedPane
-
JPanel
-子组件。

看看我的建议,这是您的
选项卡窗格()
方法:

public JTabbedPane tabbedPane() {
    UIManager.put("TabbedPane.selected", Color.ORANGE);
    mainJTP = new JTabbedPane();
    mainJTP.setBackground(Color.WHITE);
    mainJTP.setBounds(3, 1, 630, 500);
    mainJTP.addTab("Classic", classic());
    mainJTP.addTab("Deluxe", deluxe());
    mainJTP.addTab("Presidential", presidential());
    rooms();  // I've inserted this, no panic!
    return mainJTP;
}

在上面的方法中,如果仔细跟踪,对
classic()、deluxe()
presidential()
的调用已经触发了对
rooms()
方法的调用。相反,像我刚才做的那样进行一次
rooms()
调用。删除对
rooms()的调用
来自代码中的其他地方。

看看我的建议,这是您的
选项卡窗格()
方法:

public JTabbedPane tabbedPane() {
    UIManager.put("TabbedPane.selected", Color.ORANGE);
    mainJTP = new JTabbedPane();
    mainJTP.setBackground(Color.WHITE);
    mainJTP.setBounds(3, 1, 630, 500);
    mainJTP.addTab("Classic", classic());
    mainJTP.addTab("Deluxe", deluxe());
    mainJTP.addTab("Presidential", presidential());
    rooms();  // I've inserted this, no panic!
    return mainJTP;
}

在上面的方法中,如果仔细跟踪,对
classic()、deluxe()
presidential()
的调用已经触发了对
rooms()
方法的调用。相反,像我刚才做的那样进行一次
rooms()
调用。删除对
rooms()的调用
从代码的其他地方开始。

不要使用空布局;添加堆栈跟踪并显示行号;编辑您的问题以包含一个。@user1708134请检查我的解决方案!不要使用空布局;添加堆栈跟踪并显示行号;编辑您的问题以包含一个。@user1708134请检查我的解决方案!您能解释更多关于我的解决方案吗错误..我对你的方法很感兴趣。你能解释一下我的错误吗..我对你的方法很感兴趣。你能给出一个自顶向下方法的例子吗?制作一个代码只是为了演示。谢谢你能给出一个自顶向下方法的例子吗?制作一个代码只是为了演示。谢谢