And also take care of Collections.sort(studentsL
Regards
Sudhakar Chavali
Main Topics
Browse All TopicsSubject.java
--------------
import java.util.*;
public class Subject
{
private String myCode, myName;
private Vector students;
public Subject()
{
setStudents();
}
public Subject(String code, String name)
{
setCode(code);
setName(name);
}
public void setCode(String code)
{
this.myCode = code;
}
public void setName(String name)
{
this.myName = name;
}
public String getCode()
{
return this.myCode;
}
public String getName()
{
return this.myName;
}
public void setStudents()
{
if(students==null)
students = new Vector();
}
public Vector getStudents()
{
return students;
}
public boolean addStudents(Students s)
{
return getStudents().add(s);
}
public String toString()
{
return getName();
}
}
Students.java
----------------
public class Students
{
String myName, myID;
double myMark;
public Students(String name, String id, double mark)
{
myName = name;
myID = id;
myMark = mark;
}
}
TestSubject.java
-------------------
import java.util.*;
public class TestSubject
{
public static void main(String[] args)
{
Subject mySubject = new Subject("CS101","Programmi
mySubject.addStudents(new Students("Mary","A111",30.
Vector studentsList = mySubject.getStudents();
Collections.sort(studentsL
Iterator iter = studentsList.iterator();
while(iter.hasNext())
{
System.out.println(iter.ne
}
}
}
I am getting an error:
Exception in thread "main" java.lang.NullPointerExcep
at Subject.addStudents(Subjec
at TestSubject.main(TestSubje
Press any key to continue...
How do I solve this problem?
Your help is kindly appreciated.
Regards
Eugene
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Your setStudents method should not be responsible for initializing a null collection. It should merely be the complement of of getStudents. So do
public Subject()
{
students = new Vector();
}
and follow what Sudhakar said about the other ctor.
At the design level though, you shouldn't be coupling students directly to Subject. This association should be happening in a higher level class
Business Accounts
Answer for Membership
by: sudhakar_koundinyaPosted on 2006-12-23 at 00:32:56ID: 18191063
Modify your constructor as below
public Subject(String code, String name)
{
this();
setCode(code);
setName(name);
}
Then it will work for you .
The reason is you are calling setStrudents in your default constructor. So vector instance will be created in your default constructor but not in your overloaded constructor. Hence you need to call your default constructor in overloaded.
Regards
Sudhakar Chavali