简体   繁体   English

如何在不遇到NullPointerException的情况下使用动态JLabels和JButton创建新的JFrame?

[英]How to create a new JFrame with dynamic JLabels & JButtons without encountering NullPointerException?

I've been trying to create a scenario where when a button is clicked, a new JFrame is generated with x amount of JLabels and JButtons (x is dependent on user inputs in another part of the program). 我一直试图创建一个场景,当单击一个按钮时,将生成一个x数量的JLabel和JButtons(x取决于程序另一部分中的用户输入)的新JFrame。 However, whenever I try to do this, I get the " Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException " error I cannot figure out what the error is. 但是,每当尝试执行此操作时,都会收到“ 线程“ AWT-EventQueue-0” java.lang.NullPointerException中的异常“错误,我无法弄清错误是什么。 My code's syntax is fine and compiles with no errors. 我的代码的语法很好,并且编译时没有错误。 Code that is bolded (not really but has ... ) are lines that the error points to. 加粗的代码(不是真正的但具有... )是错误所指向的行。 There are also other lines that are pointed to but those are the lines generated by java. 还有其他指向的行,但是这些行是由java生成的。

**public class MainUI extends javax.swing.JFrame {**

public MainUI() {
    initComponents();
}
private void ViewList(java.awt.event.ActionEvent evt) {                                
    this.dispose();
    ListFrame list = new ListFrame();
    ArrayList<Type1> miniList1 = DataStorage.getMiniList1();
    ArrayList<Type2> miniList2 = DataStorage.getMiniList2();
    int counter = 0;
    java.awt.Label[] rLabels;
    rLabels = new java.awt.Label[miniList1.size() + miniList2.size()];
    javax.swing.JButton[] rButtons;
    rButtons = new javax.swing.JButton[rLabels.length];
for (int g=0;g<rLabels.length;g++)
{
    **rLabels[g].setPreferredSize(new Dimension(150,35));**
    rLabels[g].setAlignment(Label.CENTER);
    rLabels[g].setLocation(30,30*g);
}
for (int h=0;h<rButtons.length;h++)
{
    rButtons[h].setText("Show");
}
for (int i=counter;i<miniList1.size();i++)
{
    String name1 = miniList1.get(i).getName();
    rLabels[i].setText(name1);
    Type1 item1 = miniList1.get(i);
    rButtons[i].addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent e) {
            ItemScreen iScreen = new ItemScreen();
            String item1Display = DataStorage.type1ToString(item1);
            iScreen.iTitleLabel.setText(name1);
            iScreen.iTextLabel.setText(item1Display);
            iScreen.setVisible(true);
        }

    });
    counter++;
}
for (int j=counter;j<miniList2.size() + counter;j++)
{
    String name2 = miniList2.get(j-counter).getName();
    rLabels[j].setText(name2);
    Type2 item2 = miniList2.get(j);
    rButtons[i].addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent e) {
            ItemScreen iScreen = new ItemScreen();
            String item2Display = DataStorage.type2ToString(item2);
            iScreen.iTitleLabel.setText(name2);
            iScreen.iTextLabel.setText(item2Display);
            iScreen.setVisible(true);
        }

    });
}
for (int m=0;m<rLabels.length;m++)
{
    list.add(rLabels[m]);
}
for (int n=0;n<rLabels.length;n++)
{
    list.add(rButtons[n]);
}
    list.setVisible(true);
}

ListFrame and ItemScreen are JFrames in my project and DataStorage is another class in my project. ListFrame和ItemScreen是我项目中的JFrame,而DataStorage是我项目中的另一个类。 Type1 and Type2 are objects. Type1和Type2是对象。 I'm using NetBeans. 我正在使用NetBeans。

When you create an array, it creates n number of empty/uninitialised slots 创建数组时,它会创建n个空/未初始化的插槽

So when you do something like... 所以当你做类似的事情...

rLabels = new java.awt.Label[miniList1.size() + miniList2.size()];

All you have is an array of null elements... 您所拥有的只是一个null元素数组...

Before you can modify an object at a given position, you need to initialise the value at the given slot... 在修改给定位置的对象之前,需要在给定的插槽处初始化值...

for (int g=0;g<rLabels.length;g++)
{
    rLabels[g] = new JLabel();
    //...
}

This goes for all arrays. 这适用于所有阵列。 Take a look at the Arrays tutorial for more details 查看Arrays教程了解更多详细信息

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

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