Results 1 to 13 of 13
  1. #1
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default no array reference

    i am having trouble with my for loop:

    Java Code:
    private String firstName, lastName;
    	private Address homeAddress, schoolAddress;
    	private int testNumber; // Contains 1, 2, and 3
    	private double testScore; // Contains the value of the 3 tests
    	private double[] testScores;
    
    	public Student(String first, String last, Address home, Address school, double[] testScores1) {
    		int numberOfScores;
    		firstName = first;
    		lastName = last;
    		homeAddress = home;
    		schoolAddress = school;
    		double[] testScores = testScores1;
    		testScores = new double [numberOfScores];
    		
    	}
    
    	/*public void setTestScore(int testNumber, double testScore) {
    		this.testNumber = testNumber;
    		this.testScore = testScore;
    	}*/
    
    	// Initialize values with 0
    	public Student(double[] testScores) {
    		Arrays.fill(testScores, 0);
    	}
    
    	// Find the average of the test scores
    	public  double average() {
    		int i;
    		double total = 0;
    		double averageScore = 0;
    		// double[] testScores= {98.5, 99.7,56.1};
    
    		for(i=0; i<testScores.length; i++) {
    			total = total + testScores[i];
    		}
    
    		averageScore = total / testScores.length;
    		
    		return averageScore;
    	}
    Java Code:
    double[] johnGrades = {98.5, 99.7,56.1};
    		Student john = new Student("John", "Smith", jHome, school, johnGrades );
    Why can't it find the array? if i define the array in my average method, it works. in eclipse, in my constructor, it is saying that I am not using my testScores array...
    Last edited by droidus; 10-05-2011 at 04:15 AM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: no array reference

    Where are you declaring johnGrades? Did you get an error? This question is awfully vague in it's current form, consider revising it to give us more information.

  3. #3
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: no array reference

    sorry; i am declaring john grades in a driver. the rest of the code is from the class file
    the error i get is this:
    Exception in thread "main" java.lang.NullPointerException
    at Student.average(Student.java:38)
    at StudentBody.main(StudentBody.java:28)
    1) points to my for loop (class)
    2) points to my john.average (driver)

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: no array reference

    Is the full code much longer than what you posted? If not, just post the full code.

  5. #5
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: no array reference

    Java Code:
    public class StudentBody {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// The address of the school
    		Address school = new Address("800 Lancaster Ave", "Villanova", "PA",
    				19085);
    
    		// Student #1
    		Address jHome = new Address("21 Jump Street", "Lynchburg", "VA", 24551);
    		double[] johnGrades = {98.5, 99.7,56.1};
    		Student john = new Student("John", "Smith", jHome, school, johnGrades );
    
    		// Student #2
    		Address mHome = new Address("123 Main Street", "Euclid", "OH", 44132);
    		//Student marsha = new Student("Marsha", "Hones", mHome, school, 98.5, 99.7, 100.1);
    		
    		System.out.println(john.average());
    		System.out.println();
    		// System.out.println(marsha + "\nThe test score average is: " +  Student.average(100.0, 100.0, 100.0));
    
    	}
    
    }
    Java Code:
    import java.util.Arrays;
    
    public class Student {
    	private String firstName, lastName;
    	private Address homeAddress, schoolAddress;
    	private int testNumber; // Contains 1, 2, and 3
    	private double testScore; // Contains the value of the 3 tests
    	private double[] testScores;
    
    	public Student(String first, String last, Address home, Address school, double[] testScores1) {
    		int numberOfScores;
    		firstName = first;
    		lastName = last;
    		homeAddress = home;
    		schoolAddress = school;
    		numberOfScores = testScores1.length;
    		double[] testScores = testScores1;
    		testScores = new double [numberOfScores];
    	}
    
    	/*public void setTestScore(int testNumber, double testScore) {
    		this.testNumber = testNumber;
    		this.testScore = testScore;
    	}*/
    
    	// Initialize values with 0
    	public Student(double[] testScores) {
    		Arrays.fill(testScores, 0);
    	}
    
    	// Find the average of the test scores
    	public  double average() {
    		int i;
    		double total = 0;
    		double averageScore = 0;
    		// double[] testScores= {98.5, 99.7,56.1};
    
    		for(i=0; i<testScores.length; i++) {
    			total = total + testScores[i];
    		}
    
    		averageScore = total / testScores.length;
    		
    		return averageScore;
    	}
    
    	/*public String toString() {
    		String result;
    
    		result = firstName + " " + lastName + "\n";
    		result += "Home Address:\n" + homeAddress + "\n";
    		result += "School Address:\n" + schoolAddress;
    		result += "\nTest scores:\n   Test #1:" + testOne + "\n   Test #2: " + testTwo
    				+ "\n   Test #3: " + testThree;
    		//result += "Test Score average: " + average();
    
    		return result;
    	}*/
    }
    and the address class is running fine...

    but here it is, in case you want to test it:
    Java Code:
    public class Address {
    private String streetAddress, city, state;
    private long zipCode;
    	/**
    	 * @param args
    	 */
    	public Address (String street, String town, String st, long zip)
    	{
    		streetAddress = street;
    		city = town;
    		state = st;
    		zipCode = zip;
    	}
    	
    	public String toString() 
    	{
    		String result;
    		
    		result = streetAddress + "\n";
    		result += city + ", " + state + " " + zipCode;
    		
    		return result;
    	}
    
    }

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: no array reference

    In your constructor:
    Java Code:
            double[] testScores = testScores1;
            testScores = new double [numberOfScores];
    When does the class variable testScores getting initialized? Which testScores is the compiler supposed to use in this second line? The one you initialized a line previously, or the class instance variable one? If this doesn't point out the mistake, let me know.

  7. #7
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: no array reference

    i think i am confusing myself here.. lol.
    do i need to do something like double testScores[] = new double [1] to initialize it?
    in the second line, the compiler is suppose to use the array, testScores. i am trying to update the array with the number of slots that i need.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: no array reference

    Ok, I was hinting at something (vaguely). This is a problem, first of shadowing. Since you created an local variable with the same name in the constructor, the compiler will use that one when you simply refer to testScores. When you have two variables of the same name you must be sure which one you are referring to, to refer to the local one--the one declared in the method--simply use the name. To use the classes instance variable, you must append 'this.', so
    Java Code:
    this.testScores
    However; this is not really your problem. Why do you feel the need to initialize an array at all? Isn't it true that the array which is passed into the constructor is already initialized? Can't you just directly assign the argument array to the classes instance variable?

  9. #9
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: no array reference

    great, works like a charm!
    only problem now, is that I get a 0.0 out :p

    Java Code:
    int numberOfScores = testScores1.length;
    		this.testScores = testScores1;
    		this.testScores = new double [numberOfScores];

  10. #10
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: no array reference

    oh, i think i see. it overwrites my values with the "new" call for the array?

    nevermind! got it. just had to take out the last line.

  11. #11
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: no array reference

    so now i gotta figure out why i get the memory allocation for my student calls. i get something like Student@527c6768

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: no array reference

    Yup, you shouldn't be creating a new array unless you absolutely mean to. Generally a constructor which takes an argument for the array can be handled with simple assignments. Also, if you get lazy and don't want to think of new variable names you can do something like this:
    Java Code:
    public class SomeClass{
      String someVar;
      int anotherVal;
    
      public SomeClass(String someVar, int anotherVal){
        this.someVar = someVar;
        this.anotherVal = anotherVal;
      }
    }

  13. #13
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: no array reference

    That which I like to call gobbledygook, is the default toString method inherited by all classes from the root class, Object. Simple fix: provide an overridden toString method. Also the gobbledygook is the class named followed by an '@' followed by the hexadecimal representation of the classes hashcode.

Similar Threads

  1. Replies: 1
    Last Post: 04-11-2011, 08:25 PM
  2. Sending array index by reference
    By mattie in forum New To Java
    Replies: 15
    Last Post: 02-11-2011, 01:17 PM
  3. Reference variable
    By lala in forum New To Java
    Replies: 2
    Last Post: 02-03-2011, 07:27 PM
  4. Replies: 16
    Last Post: 07-08-2010, 09:13 PM
  5. reference to elements in array
    By Igor in forum New To Java
    Replies: 1
    Last Post: 12-14-2007, 11:56 AM

Posting Permissions

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