Hello all,
I am stuck with a nullpointerException at line 41 and 60.Can someone please help me out.any kind of help will be appreciated.

Thanks

import javax.swing.*;
import javax.swing.event.*;
import java.awt.Color;
import java.awt.*;

class calenders extends JFrame
{
int i=0;
int j=0;
int k=0;
JFrame jf;
FlowLayout fl;
JPanel jp[];
JButton jb [];
JButton jb3[];
Button b[];
calenders()
{
jf=new JFrame ("Happy 2014  :-)");
fl=new FlowLayout();
b=new Button[12];
jp=new JPanel[12];
String days[]={"Sun", "Mon","Tue", "Wed", "Thurs", "Fri", "Sat"};
String months[]={"Jan", "Feb","March", "Apr", "May", "July", "August","september","october","november","december"};
//JButton jb1=new JButton("Previous");
//JButton jb2=new JButton("Next");
jb3=new JButton[7];
String dates[]={"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20",
"21","22","23","24","25","26","27","28","29","30","31"};
 jb=new JButton[31];
Color colors[]={Color.blue, Color.yellow, Color.pink, Color.red, Color.orange, Color.white, Color.blue, Color.green, Color.pink,
Color.magenta, Color.darkGray,Color.cyan};
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(800,800);
CardLayout cl=new CardLayout();
for( i=0; i<13; i++)
{
jp=new JPanel[12];
b[i]=new Button(months[i]);
b[i].setBounds(30,30,770,55);
jp[i].add(b[i]);
for(j=0; j<days.length; j++)
{
jb3[j]=new JButton(days[j]);
jp[i].add(jb3[j]);
}
for(k=0; k<dates.length; k++)
{
jb[k]=new JButton(dates[k]);
jp[i].add(jb[k]);
}
}
jp[i].setLayout(fl);
jp[i].setBackground(colors[i]);
add(jp[i],cl);
setVisible(true);
}
public static void main(String...s)
{
new calenders();
}
}

jp=new JPanel[12];
creates an array of 12 references to JPanels, but it doesn't create any JPanels, all the references are null, which is why you get the NPE on line 41.
After creating the array you need to populate it by assigning new JPanels to each of its elements.

Thanks a ton :D

Glad to help. Please mark this "solved" for our database - you can always start another thread if you have new question.

but i have passes an array of button to each panel, how come they are null then? how am i supposed to correct it?

After creating the array you need to populate it by assigning new JPanels to each of its elements.
eg

for (int 1 = 0; i<12; i++) jp[i] = new JPanel();

oh ya got it :) done :) just one more question,

I have created the 2d array given below

String dates[][]={{"","","","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20",
"21","22","23","24","25","26","27","28","29","30","31"},{"","","","","","","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20",
"21","22","23","24","25","26","27","28","","",""},{"","","","","","","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20",
"21","22","23","24","25","26","27","28","29","30","31"},{"","","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20",
"21","22","23","24","25","26","27","28","29","30",""},{"","","","","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20",
"21","22","23","24","25","26","27","28","29","30","31"},{"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20",
"21","22","23","24","25","26","27","28","29","30","31"},{"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20",
"21","22","23","24","25","26","27","28","29","30",""},{"","","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20",
"21","22","23","24","25","26","27","28","29","30","31"},{"","","","","","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20",
"21","22","23","24","25","26","27","28","29","30","31"},{"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20",
"21","22","23","24","25","26","27","28","29","30","31"},{"","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20",
"21","22","23","24","25","26","27","28","29","30",""},{"","","","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20",
"21","22","23","24","25","26","27","28","29","30","31"},{"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20",
"21","22","23","24","25","26","27","28","29","30","31"},{"","","","","","","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20",
"21","22","23","24","25","26","27","28","29","30",""},{"","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20",
"21","22","23","24","25","26","27","28","29","30","31"}};

and i am trying to access it with this code

for(k=0; k<40; k++)
{
jb[k]=new JButton(dates[i][k]);
jp[i].add(jb[k]);
}

where i is varying from 0 to 12 but i am gettign arrayIndexOutOfBound exception.Can you please tell where is it out of bounds?

Thanks

The very first row of that array contains 23 elements, so with k going up to 40 that will give an index out of bounds exception. Remember that it's not a 2d array (strictly a rectangle), it's an array of arrays - each row can be a different length.
The safe way to process a 2D array looks like this:

for (int i = 0; i < data.length; i++) {
   for (int j = 0; j < data[i].length;j++) {
       ... data[i][j] ...

exception gone :) thanks :)

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.