Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    asmodean is offline Member
    Join Date
    Jul 2010
    Posts
    18
    Rep Power
    0

    Default Variable of an object in an array compared to an element of another array?

    I have an array of objects with four fields and another array of ints. I'm having trouble trying to see if each of the elements of the int array are equal to a certain (the same) field of each of the objects in the object array but I keep getting errors.

    if (studentIDNumbers[index] != student[index2].getStudentID())

    doesn't seem to work..

    studentIDNumbers is the array of ints and student[] is the object array. getStudentID is the accessor method for the int field of the object.

    Anyone know what I'm doing wrong?

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    hard to tell without more of your code especially on the class student

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    Try debugging your code by writing simple loops that goes thru the student array and prints out the values returned by the getStudentID() method and the values in the studentIDNumbers array. Then you can see if there should be matches.

    Further debugging add this:
    System.out.println("studentIDNumbers[index] = " + studentIDNumbers[index] + ", student[index2].getStudentID() =" + student[index2].getStudentID());
    just before the if() test where they are compared.

  4. #4
    asmodean is offline Member
    Join Date
    Jul 2010
    Posts
    18
    Rep Power
    0

    Default

    if (studentIDNumbers[index] != student[index2].getStudentID())


    That should be comparing the two int variables though, right? I mean the syntax isn't wrong is it? studentIDNumbers[] is just an array of ints so its calling the int in 'index' and 'getStundentID()' is the accessor method for the int field (it returns the value held in the studentID field) of the student object and the name of the array is 'student[]' so shouldn't that be the correct syntax for calling the student ID variable held by the student object in the 'index2' element position?

    I trying debugging with loops before I posted here and it prints out the id number but when I run the whole thing it throws an exception at the if statement above.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    it throws an exception at the if statement above.
    Please copy and paste here the full text of the error message.

  7. #7
    asmodean is offline Member
    Join Date
    Jul 2010
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Please copy and paste here the full text of the error message.
    Exception in thread "main" java.lang.NullPointerException
    at StudentDemo.main(StudentDemo.java:72)

    line 72 being the 'if' statement

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    What variable at line 72 is null?
    Add a println statement before line 72 to show the values of all the variables used in line 72 to find out which one is null.

  9. #9
    asmodean is offline Member
    Join Date
    Jul 2010
    Posts
    18
    Rep Power
    0

    Default

    import java.util.*;
    import java.io.*;

    public class StudentDemo
    {
    public static void main(String[] args)throws IOException
    {
    String filename;
    String filename2;

    Scanner keyboard = new Scanner(System.in);

    System.out.println("Enter the name of the file to open.");
    filename = keyboard.nextLine();

    File openFile = new File(filename);

    Scanner inputFile = new Scanner(openFile);

    String line = inputFile.nextLine();

    Student[] student = new Student[10];

    for(int index = 0; index < 10; index++)
    {
    while (inputFile.hasNext())
    {
    String[] tokens = line.split(",");

    student[index] = new Student(tokens[0], tokens[1], tokens[2], tokens[3]);

    line = inputFile.nextLine();
    }
    }
    inputFile.close();

    System.out.println("Enter the name of the file of student IDs to open.");
    filename2 = keyboard.nextLine();

    File openFile2 = new File(filename2);
    Scanner inputFile2 = new Scanner(openFile2);

    int[] studentIDNumbers = new int[10];

    for (int index = 0; index < 10; index++)
    {
    studentIDNumbers[index] = inputFile2.nextInt();
    }
    inputFile2.close();

    boolean found = false;

    for(int index = 0; index < 10; index++)
    {
    for(int index2 = 0; index2 < 10; index2++)
    {
    if (studentIDNumbers[index] == student[index2].getStudentID())
    {
    found = true;
    System.out.println(studentIDNumbers[index] + "is " +student[index2].getFirstName()+ " " +student[index2].getLastName());
    }
    else
    {
    found = false;
    }
    }
    if (!found)
    System.out.println(studentIDNumbers[index] + "was not found.");
    }

    }

    private static class Student
    {
    private String firstName;
    private String lastName;
    private int studentID;
    private String studentMajor;

    public Student(String fName, String lName, String id, String major)
    {
    firstName = fName;
    lastName = lName;
    studentID = Integer.parseInt(id);
    studentMajor = major;
    }

    public String getFirstName()
    {
    return firstName;
    }

    public String getLastName()
    {
    return lastName;
    }

    public int getStudentID()
    {
    return studentID;
    }

    public String getMajor()
    {
    return studentMajor;
    }
    }

    }

    thats the whole code.



    first txt file is a list of students with their first and last names, id number and major. I just made them up to test the program. The second one is a list of nothing but id numbers.

    first one:

    Bob,johnson,123,Math
    john,paul,456,Computer Science
    paul,john,789,Biology
    mark,anderson,012,Physics
    jane,hughs,034,Computer Science
    megan,jones,056,English
    lanie,mcintire,078,Math
    robert,robertson,090,History
    jimmy,Johnson,091,Math
    joan,bigguns,001,Computer Science

    second one:

    123
    456
    001
    765
    543
    999
    012
    000
    333
    777

    I did have a loop inside the loop that read the lines from the first file that would print out the individual tokens and that works but for some reason its not saving the tokens array to any of the student array objects. Also the second array seems to get the last number of the file saved to the first element and then zeros to the rest.
    Last edited by asmodean; 09-06-2010 at 08:58 PM.

  10. #10
    asmodean is offline Member
    Join Date
    Jul 2010
    Posts
    18
    Rep Power
    0

    Default

    import java.util.*;
    import java.io.*;

    public class StudentDemo
    {
    public static void main(String[] args)throws IOException
    {
    String filename;
    String filename2;

    Scanner keyboard = new Scanner(System.in);

    System.out.println("Enter the name of the file to open.");
    filename = keyboard.nextLine();

    File openFile = new File(filename);

    Scanner inputFile = new Scanner(openFile);

    String line = inputFile.nextLine();

    Student[] student = new Student[10];

    for(int index = 0; index < 10; index++)
    {
    while (inputFile.hasNext())
    {
    String[] tokens = line.split(",");

    student[index] = new Student(tokens[0], tokens[1], tokens[2], tokens[3]);

    line = inputFile.nextLine();
    }
    }
    inputFile.close();

    System.out.println("Enter the name of the file of student IDs to open.");
    filename2 = keyboard.nextLine();

    File openFile2 = new File(filename2);
    Scanner inputFile2 = new Scanner(openFile2);

    int[] studentIDNumbers = new int[10];

    for (int index = 0; index < 10; index++)
    {
    studentIDNumbers[index] = inputFile2.nextInt();
    }
    inputFile2.close();

    boolean found = false;

    for(int index = 0; index < 10; index++)
    {
    for(int index2 = 0; index2 < 10; index2++)
    {
    if (studentIDNumbers[index] == student[index2].getStudentID())
    {
    found = true;
    System.out.println(studentIDNumbers[index] + "is " +student[index2].getFirstName()+ " " +student[index2].getLastName());
    }
    else
    {
    found = false;
    }
    }
    if (!found)
    System.out.println(studentIDNumbers[index] + "was not found.");
    }

    }

    private static class Student
    {
    private String firstName;
    private String lastName;
    private int studentID;
    private String studentMajor;

    public Student(String fName, String lName, String id, String major)
    {
    firstName = fName;
    lastName = lName;
    studentID = Integer.parseInt(id);
    studentMajor = major;
    }

    public String getFirstName()
    {
    return firstName;
    }

    public String getLastName()
    {
    return lastName;
    }

    public int getStudentID()
    {
    return studentID;
    }

    public String getMajor()
    {
    return studentMajor;
    }
    }

    }

    thats the whole code.



    first txt file is a list of students with their first and last names, id number and major. I just made them up to test the program. The second one is a list of nothing but id numbers.

    first one:

    Bob,johnson,123,Math
    john,paul,456,Computer Science
    paul,john,789,Biology
    mark,anderson,012,Physics
    jane,hughs,034,Computer Science
    megan,jones,056,English
    lanie,mcintire,078,Math
    robert,robertson,090,History
    jimmy,Johnson,091,Math
    joan,bigguns,001,Computer Science

    second one:

    123
    456
    001
    765
    543
    999
    012
    000
    333
    777

    I did have a loop inside the loop that read the lines from the first file that would print out the individual tokens and that works but for some reason its not saving the tokens array to any of the student array objects. Also the second array seems to get the second to last number of the file saved to the first element and then zeros to the rest.

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    Is your problem solved now? I didn't see your question(s) in the last post.

    If you are going to post code, please use code tags to preserve formatting.

  12. #12
    asmodean is offline Member
    Join Date
    Jul 2010
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Is your problem solved now? I didn't see your question(s) in the last post.

    If you are going to post code, please use code tags to preserve formatting.
    No, it still says the array is full of null objects.

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    Each element in an array of objects must be assigned a value, otherwise it is null.
    Does your program do that?

  14. #14
    asmodean is offline Member
    Join Date
    Jul 2010
    Posts
    18
    Rep Power
    0

    Default

    Yeah, I have a loop that assigns values to an object's fields and then it assigns that object to the array. At least I'm pretty sure its supposed to be doing that. But for some reason the objects aren't getting assigned to the array, or the values aren't getting assigned to the objects' fields.

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    for some reason the objects aren't getting assigned to the array, or the values aren't getting assigned to the objects' fields.
    You need to do some debugging of your program to find out why.
    Print out the contents of the array after you add a item to it by using the Arrays.toString() method.

    Add the following method to the Student class:
    Java Code:
    public String toString() {
      return "fName=" + fName + ", id=" + studendID;
    }
    This will be what the Arrays.toString() method will show.

  16. #16
    asmodean is offline Member
    Join Date
    Jul 2010
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    You need to do some debugging of your program to find out why.
    Print out the contents of the array after you add a item to it by using the Arrays.toString() method.

    Add the following method to the Student class:
    Java Code:
    public String toString() {
      return "fName=" + fName + ", id=" + studendID;
    }
    This will be what the Arrays.toString() method will show.

    I did its at the bottom of the wall of text I posted yesterday, sorry.

    "I did have a loop inside the loop that read the lines from the first file that would print out the individual tokens and that works but for some reason its not saving the tokens array to any of the student array objects. Also the second array seems to get the last number of the file saved to the first element and then zeros to the rest."

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    second array seems to get the last number of the file saved to the first element and then zeros to the rest
    Does the array have a variable name?

    What does "zeros to the rest" mean?

    Do some more debugging by adding some println()s to see why the values are zeros.
    Print out each line as you read it in.
    Print out each time a new Student object is created.
    Print out the value of index as each Student object is added to the array.

  18. #18
    asmodean is offline Member
    Join Date
    Jul 2010
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Does the array have a variable name?

    What does "zeros to the rest" mean?

    Do some more debugging by adding some println()s to see why the values are zeros.
    Print out each line as you read it in.
    Print out each time a new Student object is created.
    Print out the value of index as each Student object is added to the array.
    Okay, I put this line inside the loop that assigns variables to the field of a student object.

    System.out.println(student[index].getFirstName());

    I put it in the loop but after the object is supposed to be added to the array, (line 32) and it spits out the correct names then, but I also added it to the code just after the loop executes (in another for loop on line 38,39) and it gives me a null pointer exception. . . I'm so frustrated. . . It does make any sense. . .

    Edit: I was wrong. Actually its starting the println loop that I added below the assignment loop but the first element of the array is the student object filled with the last line of the file. . . All the other elements are null. I know what all that means. What I don't is why its doing it. . .
    Last edited by asmodean; 09-07-2010 at 07:24 PM.

  19. #19
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    Why didn't you put the 3+ lines in your code as I suggested? If you put only one in at a time, the process will take a long time.

    Your print out could be enhanced a bit to show where you are in the loop and which variable is null by using three lines:
    System.out.println("index=" + index);
    System.out.println("student[index]=" + student[index]);
    System.out.println("getFirstName()=" + student[index].getFirstName());

    it gives me a null pointer exception
    When you get a NPE, you must look at the code and determine what variable is null.
    Then back track in the code to see why it is null and then change it so it is not null.

  20. #20
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    All the other elements are null.
    What does the print out of the value of index show?


    You need to work on one part of the program at a time.
    First concentrate on filling the array with data.
    After you think its full, use the Arrays.toString() method to print out the contents of the array.
    If there are any holes/null values or the values are all the same go back over your code that fills the array and find the problem.
    Last edited by Norm; 09-07-2010 at 07:45 PM.

Page 1 of 2 12 LastLast

Similar Threads

  1. deleting an element from an array
    By moamen in forum New To Java
    Replies: 11
    Last Post: 01-03-2010, 05:38 PM
  2. [SOLVED] indexing an element in an array help!
    By anthonym2121 in forum New To Java
    Replies: 1
    Last Post: 04-03-2009, 06:21 PM
  3. Replies: 1
    Last Post: 03-31-2009, 06:40 AM
  4. Unique element in an array
    By revathi17 in forum New To Java
    Replies: 2
    Last Post: 12-31-2007, 08:44 AM
  5. Max element in an Array
    By mew in forum New To Java
    Replies: 5
    Last Post: 12-03-2007, 05:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •