Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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

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.

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |