Null Pointer Exception

Discussion in 'Java' started by BlackJackal, Feb 7, 2007.

  1. BlackJackal

    BlackJackal Guest

    I am working on a homework problem and I have run into a brick wall.
    I am receving a

    java.lang.NullPointerException
    at InputGrades.main(InputGrades.java:15)

    Error message when trying to run the program, it compiles just fine.
    As far as I can tell everything looks fine. The problem calls for me
    to create three classes with the first two being the base and the
    third being where all the action takes place. Please take a look and
    point out my stupidity.

    public class CollegeCourse
    {
    private String ID;
    private int CH;
    private char LG;
    public static void main(String[] args)
    {

    }
    public String getID()
    {
    return ID;
    }
    public int getCH()
    {
    return CH;
    }
    public char getLG()
    {
    return LG;
    }
    public void setID(String a)
    {
    ID = a;
    }
    public void setCH(int a)
    {
    CH = a;
    }
    public void setLG(char a)
    {
    LG = a;
    }

    }






    public class Student
    {
    private int stuid;
    private CollegeCourse courses[] = new CollegeCourse[5];
    public static void main(String[] args)
    {



    }
    public int getstuid()
    {
    return stuid;
    }
    public void setstuid(int a)
    {
    stuid = a;
    }
    public CollegeCourse getcourses(int a)
    {
    return courses[a];
    }
    public void setcourses(int a, CollegeCourse c)
    {
    courses[a] = c;
    }
    }








    import javax.swing.*;
    public class InputGrades
    {
    public static void main(String[] args)
    {
    Student students[] = new Student[10];
    String temp;
    int sel;
    char grade;
    CollegeCourse place = new CollegeCourse();
    String grades = "aAbBcCdDfF";
    for(int i = 0; i < 10; ++i) {
    temp = JOptionPane.showInputDialog(null,"Enter ID for
    Student #" + (i+1));
    sel = Integer.parseInt(temp);
    students.setstuid(sel);
    for(int b = 0; b < 5; ++b) {
    temp = JOptionPane.showInputDialog(null,"Enter in
    Course ID for Course #" + (b+1));
    place.setID(temp);
    temp = JOptionPane.showInputDialog(null,"Enter in
    Credit Hours for Course #" + (b+1));
    sel = Integer.parseInt(temp);
    place.setCH(sel);
    do {
    temp = JOptionPane.showInputDialog(null,"Enter in
    Letter Grade for Course #" + (b+1));
    grade = temp.charAt(0);
    if(grades.indexOf(grade) == -1) {
    JOptionPane.showMessageDialog(null,"You entered
    an Incorrect Grade Try again");
    }
    } while (grades.indexOf(grade) == -1);
    place.setLG(grade);
    students.setcourses(b, place);
    }
    }
    }
    }
    BlackJackal, Feb 7, 2007
    #1
    1. Advertising

  2. On 7 Feb 2007 06:29:22 -0800, BlackJackal wrote:
    > I am working on a homework problem and I have run into a brick wall.
    > I am receving a
    >
    > java.lang.NullPointerException
    > at InputGrades.main(InputGrades.java:15)


    The following line creates an array of 10 Student references, however
    it doesn't create the actual Student objects, so each array element is
    initially null:

    > Student students[] = new Student[10];


    [...]

    A few lines further down, this is where your exception occurs:

    > students.setstuid(sel);


    You need to create the Student objects before you can use them.

    /gordon

    --
    [ don't email me support questions or followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Feb 7, 2007
    #2
    1. Advertising

  3. BlackJackal

    BlackJackal Guest

    > The following line creates an array of 10 Student references, however
    > it doesn't create the actual Student objects, so each array element is
    > initially null:
    >
    > > Student students[] = new Student[10];

    >
    > [...]
    >
    > A few lines further down, this is where your exception occurs:
    >
    > > students.setstuid(sel);

    >
    > You need to create the Student objects before you can use them.
    >
    > /gordon
    >
    > --
    > [ don't email me support questions or followups ]
    > g o r d o n + n e w s @ b a l d e r 1 3 . s e


    So do I have to put information into the Student objects first? If so
    what is the most effiecient way to accomplish this task. Thanks in
    advance
    BlackJackal, Feb 7, 2007
    #3
  4. On 7 Feb 2007 06:59:08 -0800, BlackJackal wrote:
    > So do I have to put information into the Student objects first?


    You have to *create* the Student objects first. Something like this:

    for (int i=0; i<10; i++) {
    students = new Student();
    }

    You probably don't need a loop just for this, you could also create
    each element at the start of the existing loop.

    /gordon

    --
    [ don't email me support questions or followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Feb 7, 2007
    #4
  5. BlackJackal

    BlackJackal Guest

    Thanks for the Help!!!!!
    BlackJackal, Feb 7, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    26,498
    Mike Schilling
    Mar 29, 2006
  2. Ken
    Replies:
    69
    Views:
    12,476
    Michael D. Borghardt
    Jul 31, 2004
  3. Ken
    Replies:
    5
    Views:
    2,515
    Victor Bazarov
    Jul 24, 2004
  4. aneuryzma
    Replies:
    3
    Views:
    691
    Jim Langston
    Jun 16, 2008
  5. Christopher
    Replies:
    4
    Views:
    428
    Ruben Safir
    Jul 9, 2011
Loading...

Share This Page