Array question

I have a question about arrays to ask. I was reading an Intro to Java book that I'm using for a programming class, and i was reading ahead of where I am to the chapter on Arrays. We did an assignment in class that looked like it would be easier if using arrays, so I tried to remake it. What I tried doing was making a class array, using a class I created named Student. I also made the array so that the user could enter the number of students to create, and also the data to be used in a method to set all instance variables of the class Student.

Student[leftbr][rightbr] Scholar = new Student[leftbr]studentCount[rightbr]

is what I used, with studentCount being an int input by the user one line above this. I use a for loop with i starting at 0, and have the user input data to be used for the arguments of the setAll method of the Student class. What I'd like to know is why I keep getting a "NullPointerException" message when I attempt to run the program? Can an object within an array (i.e. Scholar[leftbr]0[rightbr]) utilize a method of the original class, Student? If any of that doesn't make sense, feel free to let me know, because sometimes I'm a bit unclear in my explanations. Thanks in advance for any help you can give me. (And by the way, I read through the entire chapter on arrays in the book. It says that you can use a class for an array, but doesn't reference anything about the methods after that.)
"Never underestimate the predictability of stupidity." - Bullet-Tooth Tony (Snatch)

Comments

  • Hi,

    The solution is pretty simple - though if you'd posted your code I could have given you a more solid 'plug it in and its fixed' answer.

    Your code is just creating 'the array', that is to say the space to be able to hold X Students, but you've not actually created the Objects - only the space required to hold references to the objects.

    So let me give you an example fix....

    Student studentArray[] = new Student[studentCount];

    Now we must actually create our students so we do this...

    [code]
    for (int c = 0; c < studentCount; c++)
    {
    studentArray[c] = new Student();
    }
    [/code]

    Now you can manipulate your array from 0..(studentCount-1) (note that arrays are 0..n, not 1..n, so if the user enters 4 you need to remember not to do 4 but to count to 3, thus the less than operator)

    e.g. studentArray[n].setWhatever();

    NOTE: You mention about setting all instances to a given value, if this is like a "default" for all students, e.g. test score is 0 until they've taken a test... then in the Student Constructor have

    [code]
    public Student()
    {
    testScore = 0;
    }
    [/code]

    or whatever, that way when the code from above tht Creates each Student Object runs, the test scores or whatever you have in mind will be set to their default initial value - of course you could pass in some data too.....

    Hope this helps, any other questions just post etc.
    Cheers
    -Phill
    Qudon Foundation Computing, Mathematics & Creative Think Tank
    http://www.qudon.com



    : I have a question about arrays to ask. I was reading an Intro to Java book that I'm using for a programming class, and i was reading ahead of where I am to the chapter on Arrays. We did an assignment in class that looked like it would be easier if using arrays, so I tried to remake it. What I tried doing was making a class array, using a class I created named Student. I also made the array so that the user could enter the number of students to create, and also the data to be used in a method to set all instance variables of the class Student.
    :
    : Student[leftbr][rightbr] Scholar = new Student[leftbr]studentCount[rightbr]
    :
    : is what I used, with studentCount being an int input by the user one line above this. I use a for loop with i starting at 0, and have the user input data to be used for the arguments of the setAll method of the Student class. What I'd like to know is why I keep getting a "NullPointerException" message when I attempt to run the program? Can an object within an array (i.e. Scholar[leftbr]0[rightbr]) utilize a method of the original class, Student? If any of that doesn't make sense, feel free to let me know, because sometimes I'm a bit unclear in my explanations. Thanks in advance for any help you can give me. (And by the way, I read through the entire chapter on arrays in the book. It says that you can use a class for an array, but doesn't reference anything about the methods after that.)
    : "Never underestimate the predictability of stupidity." - Bullet-Tooth Tony (Snatch)
    :
  • Thank you for the help. It seems like I overlooked something quite obvious, so I'm sorry for that. The book I have just doesn't give any examples of implementing the use of an array with classes; it only says that you can, basically.
    "Never underestimate the predictability of stupidity." - Bullet-Tooth Tony (Snatch)

  • Years back when I was first learning java I found the exact same problem, that the book I'd bought sort of glossed over Arrays of Objects. At the time my solution was to find a book that put more emphasis on this specific area and buy it......and strangely it was rubbish for everything but Arrays of Objects - but at the time you just want the solution.

    It still amazes me how many Java books go, "You do an array like this....." "Oh and you can also do the same sort of thing with Objects"
    Then later in the book they forget all about arrays of Objects and jump straight to Linked Lists or Vectors.

    Glad it helped etc.
    Cheers
    -Phill
    Qudon Foundation: Computing, Mathematics & Creative Think Tank
    www.qudon.com


    : Thank you for the help. It seems like I overlooked something quite obvious, so I'm sorry for that. The book I have just doesn't give any examples of implementing the use of an array with classes; it only says that you can, basically.
    : "Never underestimate the predictability of stupidity." - Bullet-Tooth Tony (Snatch)
    :
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories