i am using an array of JButtons b[23]....

my objective is that when i click a button it has to change its background color to black

but it is giving a nullpointer exception....

if(ae.getActionCommand().equals("0"))
{
System.out.println("button b[o] is pressed");


b[0].setBackground(Color.black);


}


}

can anyone help me with this please........

Recommended Answers

All 5 Replies

Which line is throwing the NPE ? That will specified right next to the exception.

b1 = new JButton("b1");


it is throwing exception at this statement ....


i defined JButton and listener globally....

PROBLEM SOLVED....


i forgot to define frame globally....

Please mark this thread "solved" so nobody else wastes time reading through it only to find at the end it's solved anyway

Member Avatar for hfx642

Also... following your first post,
it should be

b[1] = new JButton ("B1");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.