简体   繁体   English

JTabbedPane给出空指针异常

[英]JTabbedPane gives null pointer exception

I'm using JTabbedPane with different panels and i'm using gridLayout for the buttons array that i will add. 我正在使用JTabbedPane与不同的面板,我正在使用gridLayout作为我将添加的按钮数组。 I tried looping the buttons to much more easy code. 我尝试将按钮循环到更简单的代码。 But i got a null pointer exception i don't how did i get that kind of error. 但我有一个空指针异常我不知道怎么会得到那种错误。 here's my code 这是我的代码

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]);

        }
    }
}

and please give me some tips on how i can code more clearer and much better approach 请给我一些提示,告诉我如何编写更清晰,更好的方法

You are getting a NullPointerException as deluxeSubPanel has not been instantiated when calling rooms() when calling: 您正在获取NullPointerException因为在调用时调用rooms()时尚未实例化deluxeSubPanel

deluxeSubPanel.add(deluxeRoom[x]);

As a general guideline, better to use a top-down approach when creating components with dependent components. 作为一般准则,在创建具有相关组件的组件时,最好使用自上而下的方法。 This would make the creation order — JTabbedPane - JPanel -Sub-Components. 这将创建创建顺序 - JTabbedPane - JPanel -Sub-Components。

Take a look at my suggestion, this is your tabbedPane() method: 看看我的建议,这是你的tabbedPane()方法:

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;
}

In the method above, the calls to classic(), deluxe() and presidential() already triggers invocations on rooms() method if you trace carefully. 在上面的方法中,如果仔细跟踪classic(), deluxe()classic(), deluxe()presidential()调用已经触发了对rooms()方法的调用。 Instead, make one rooms() call as I've just done above. 相反,我正在上面做一个rooms()电话。 Remove calls to rooms() from elsewhere in your code. 从代码中的其他位置删除对rooms()调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM