hello i am beginner to java i wrote a simple program of object array but it is showing run time error

Exception in thread "main" java.lang.NullPointerException
at classarr.main(classarr.java:14)

class abc{
int i;
}



class classarr{

public static void main(String arg[])
{
abc a[]=new abc[4];
int j;
for(j=0;j<=3;j++)
a[j].i=j+1;


for(j=0;j<=3;j++)
System.out.print(a[j].i);
}
}

please help me

Recommended Answers

All 3 Replies

abc a[]=new abc[4];
creates an array of reference variables that can refer to abc objects, but it doesn't populate those references with anything, so the initial values in that array are like {null, null, null, null}.
You need to create four new abc instances for the four array elements, eg
for(j=0;j<=3;j++) a[j]= new abc();

can you please give me one example.

I did - last line of previous post.

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.