array of objects

October 8, 2009 at 19:48:57
Specs: Windows XP
import java.io.DataInputStream;

class student
{
String name = new String("");
String sub[] = new String[4];
String roll = new String(" ");
int marks[],i;
String grade = new String("");
DataInputStream ds = new DataInputStream(System.in);
void get()
{
System.out.println("Enter");
}
void getdata()
{
System.out.println("Enter name of student: ");
try
{
name = ds.readLine();
}
catch(Exception e)
{}
System.out.println("Enter roll of student: ");
try
{
roll = ds.readLine();
}
catch(Exception e)
{}
for(i=1;i<=4;i++)
{
System.out.println("Enter Subject"+i+"of Student: ");
try
{
sub[i] = ds.readLine();
}
catch(Exception e)
{}
System.out.println("Enter Marks for Subject"+i);
try
{
marks[i] = Integer.parseInt(ds.readLine());
}
catch(Exception e)
{}
}
}
}

class m_stud
{
public static void main(String a[])
{
student[] s = new student[4];
int i=1;
for(i=1;i<=4;i++)
{
s[i].get();
}
}
}

this code doesnot work in java... it shows an error like Exception in thread "main" java.lang.NullPointerException
at m_stud.main(m_stud.java:59).
pls help



See More: array of objects

Report •


#1
October 9, 2009 at 01:39:03
The statement
student[] s = new student[4];
assigns s to an array of 4 student references. Each array element doesn't reference an actual object until you assign it to one.

Report •

#2
October 9, 2009 at 07:35:41
thanx... now i understand my mistake...

Report •

Related Solutions


Ask Question