I have checked almost all kinds of declarations for an array of objects. Please suggest the required changes.This code gives me a null pointer exception as follows:
Exception in thread "main" java.lang.NullPointerException at objarray.main(objarray.java:33)
import java.util.*;
class Prod {
private int pno, pcost;
private String pname;
void accept() {
Scanner sc = new Scanner( System.in );
System.out.println( "Enter pno" );
pno = sc.nextInt();
System.out.println( "Enter pname" );
pname = sc.next();
System.out.println( "Enter pcost" );
pcost = sc.nextInt();
}
void print() {
System.out.println( pno + "\t" + pname + "\t" + pcost );
}
}
class objarray {
public static void main( String[] args ) {
int i;
Prod[] p = new Prod[3];
for( i = 0; i < 3; i++ )
p[i].accept();
for( i = 0; i < 3; i++ )
p[i].print();
}
}
p[i]=new Prod();