Computing.Net > Forums > Programming > array of objects

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

array of objects

Reply to Message Icon

Name: somjeet
Date: October 8, 2009 at 19:48:57 Pacific
OS: Windows XP
Subcategory: Java
Comment:

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




Sponsored Link
Ads by Google

Response Number 1
Name: klint
Date: October 9, 2009 at 01:39:03 Pacific
Reply:

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.

1

Response Number 2
Name: somjeet
Date: October 9, 2009 at 07:35:41 Pacific
Reply:

thanx... now i understand my mistake...


1

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More






Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: array of objects

Array of Objects 2 www.computing.net/answers/programming/array-of-objects-2/14653.html

Array of objects in Java www.computing.net/answers/programming/array-of-objects-in-java/12255.html

Create an array of objects www.computing.net/answers/programming/create-an-array-of-objects/19935.html