Advertisement

12.23.2006 at 12:16AM PST, ID: 22102211
[x]
Attachment Details

java.lang.NullPointerException

Asked by eugene007 in Java Programming Language

Subject.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","Programming");
            mySubject.addStudents(new Students("Mary","A111",30.4));
            Vector studentsList = mySubject.getStudents();
            Collections.sort(studentsList);
            
            Iterator iter = studentsList.iterator();
            while(iter.hasNext())
            {
                  System.out.println(iter.next());
            }
      }
}


I am getting an error:

Exception in thread "main" java.lang.NullPointerException
        at Subject.addStudents(Subject.java:52)
        at TestSubject.main(TestSubject.java:8)
Press any key to continue...


How do I solve this problem?


Your help is kindly appreciated.


Regards

Eugene
Start Free Trial
 
Loading Advertisement...
 
[+][-]12.23.2006 at 12:32AM PST, ID: 18191063

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Java Programming Language
Sign Up Now!
Solution Provided By: sudhakar_koundinya
Participating Experts: 2
Solution Grade: A
 
 
[+][-]12.23.2006 at 12:35AM PST, ID: 18191066

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12.23.2006 at 01:29AM PST, ID: 18191133

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32