Trouble with buttonhandler - Exception in thread "AWT-EventQueue-0"
I get this Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
when i click the button Start, below is it's handler
Can't understand why...Please help me!!!!!!!!!!!!!!!!!
private void StartActionPerformed(java.awt.event.ActionEvent evt) {
int numberOfButtons = a.length+ a2.length+2;
System.out.println(numberOfButtons);
jPanel1.setVisible(true);
jPanel1.setEnabled(true);
jPanel1.setBackground(Color.LIGHT_GRAY);
jPanel1.setLayout(new FlowLayout(FlowLayout.CENTER));
for(int i=0; i<numberOfButtons; i++)
{
JPanel p[] = new JPanel[numberOfButtons];
JButton b[] = new JButton[numberOfButtons];
p[i] = new JPanel();
b[i] = new JButton();
b[i].setEnabled(true);
b[i].setVisible(true);
b[i].setText("gf");
b[i].setSize(50, 30);
jPanel1.setLayout(new FlowLayout(FlowLayout.TRAILING));
jPanel1.add(b[0]);
if(i==1){
b[1].setBounds(30,50,40,50); //1
jPanel1.setBounds(300, 300, 100, 100);
jPanel1.add(b[1]);
b[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
JDialog dwindow = new JDialog();
dwindow.setEnabled(true);
dwindow.setVisible(true);
dwindow.setSize(200, 120);
}
});
}//end of for
}
Re: Trouble with buttonhandler - Exception in thread "AWT-EventQueue-0"
What line is the error on? What is happening on that line? Something must be null there- where do you initialize each of the variables you're dereferencing?
Re: Trouble with buttonhandler - Exception in thread "AWT-EventQueue-0"
in this line is the error :jPanel1.add(b[0]);
i want to put a button into jPanel1 ...when I use it outside the for there is no problem
Somehow there is a problem in the for loop
Re: Trouble with buttonhandler - Exception in thread "AWT-EventQueue-0"
You still haven't posted the actual error- what is the actual Exception? You also haven't used highlight tags, which makes your code pretty hard to read.
Hint: What is the value of each element of the array when you're trying to use it? Use some print statements to check your assumptions.
Re: Trouble with buttonhandler - Exception in thread "AWT-EventQueue-0"
this the error : Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
in this line : jPanel1.add(b[0]);
the line in blue
start is a button and bellow is it's actionPerformed event
private void StartActionPerformed(java.awt.event.ActionEvent evt)
{
int numberOfButtons = a2;
System.out.println(numberOfButtons);
jPanel1.setVisible(true);
jPanel1.setEnabled(true);
jPanel1.setBackground(Color.LIGHT_GRAY);
jPanel1.setLayout(new FlowLayout(FlowLayout.CENTER));
for(int i=0; i<numberOfButtons; i++)
{
JPanel p[] = new JPanel[numberOfButtons];
JButton b[] = new JButton[numberOfButtons];
p[i] = new JPanel();
b[i] = new JButton();
b[i].setEnabled(true);
b[i].setVisible(true);
b[i].setText("gf");
b[i].setSize(50, 30);
jPanel1.add(b[0]);
b[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
JDialog dwindow = new JDialog();
dwindow.setEnabled(true);
dwindow.setVisible(true);
dwindow.setSize(200, 120);
}
});
}//end of for
}
Re: Trouble with buttonhandler - Exception in thread "AWT-EventQueue-0"
Ah, so it's a NullPointerException. Now your job is to figure out what's null in that line. It could either be jPanel1 or b[0], right? So what is the value of each? Are you sure? Step through it with a debugger or add some print statements to test your assumptions.
Re: Trouble with buttonhandler - Exception in thread "AWT-EventQueue-0"
Thanks KevinWorkman!!
Yes as I checked the b[0] is null. But why?? I create the buttons
" JButton b[] = new JButton[numberOfButtons];
b[i] = new JButton();
b[i].setEnabled(true);
b[i].setVisible(true);
b[i].setText("gf");
b[i].setSize(50, 30);
"
Re: Trouble with buttonhandler - Exception in thread "AWT-EventQueue-0"
You seem to be declaring your array inside the for loop that is supposed to fill it? Don't do that, but instead declare it *before* the for loop. Don't try to manipulate b[0] from within the for loop either.
Re: Trouble with buttonhandler - Exception in thread "AWT-EventQueue-0"
Quote:
Originally Posted by
curmudgeon
You seem to be declaring your array inside the for loop that is supposed to fill it? Don't do that, but instead declare it *before* the for loop. Don't try to manipulate b[0] from within the for loop either.
That's what I was trying to get him to find for himself by stepping through it with a debugger or adding print statements. Ah well.
Re: Trouble with buttonhandler - Exception in thread "AWT-EventQueue-0"