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). 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. 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.
**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. Type1 and Type2 are objects. I'm using NetBeans.