Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Null Error

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Null Error

    The problem with the codes it that, in "getAverage" it is not allowing the "return null". I assume this is because it is declared a double. What is the proper way to return the "getAverage"?

    public class StudentScores
    {
       private ArrayList<Student> scores;
       public StudentScores()
       {
          scores = new ArrayList<Student>();
       }
       public void add(String name, int score)
       {
          scores.add(new Student(name, score));
       }
       public Student getHighest()
       {
          if (scores.size() == 0)
             return null;
          Student highest = scores.get(0);
          for (int i = 1; i < scores.size(); i++)
             if (scores.get(i).getScore() > highest.getScore())
                highest = scores.get(i);
          return highest;
        }
       public Student getLowest()
       {
          if (scores.size() == 0)
             return null;
          Student lowest = scores.get(0);
          for (int i = 1; i < scores.size(); i++)
             if (scores.get(i).getScore() < lowest.getScore())
                lowest = scores.get(i);
          return lowest;
       }   
         public double getAverage()
       {
          if (scores.size() == 0)
             return null; //will not return, BlueJ gives error of "incompatible types"
          Student lowest = scores.get(0);
          for (int i = 1; i < scores.size(); i++)
             if (scores.get(i).getScore() < lowest.getScore())
                lowest = scores.get(i);
          return lowest;
       }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Null Error

    The getAverage method is declared to return a double primitive not an object, so you cannot return null. A few other alternatives would be to throw an exception (perhaps an IllegalStateException), use an assertion, or return something like Double.MAX_VALUE. I would opt for the first but leave it up to you to decide - however it is done it should be documented in the comments (which your code lacks)

Similar Threads

  1. Null Exception Error & Help with PrintWriter
    By willo in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 29th, 2011, 09:08 AM
  2. Null Pointer Exception error
    By Keitho55 in forum Exceptions
    Replies: 3
    Last Post: November 18th, 2011, 03:29 AM
  3. Null or not an object error
    By James W in forum Other Programming Languages
    Replies: 4
    Last Post: November 1st, 2011, 01:06 PM
  4. NULL POINTER EXCEPTION error
    By beefwithrice in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 28th, 2011, 06:26 AM
  5. Getting Null Pointer Exception in runtime
    By RoadRunner in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2009, 01:21 PM