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 6 of 6

Thread: returning null instead of int

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default returning null instead of int

    Hi,
    I have the following code. The point is to test returns.

    public class objects
    {
        public static void main(String[]args)
        {
            int x = 0;
        }
     
        public int testZero(int x)
        {
            if (x == 0)
                {
                    System.out.println(x);
                    return ;
                }
            else
                {
                    return x;
                }
        }
    }

    As is, I get a "missing return value" error from the compiler for the line "return ;", line 13. If I remove that statement, I get "missing return statement".
    How should/could I handle this? If my method has a return type, MUST I return a value of that type? Is there a way not to do that?

    Thank you,
    Mike


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: returning null instead of int

    an int can never have a null value because it's a Java primitive. However, there are wrapper classes for primitives which can be null.

    ex.:

    // didn't test run this, hopefully it works
    public Integer testZero(int x)
    {
        if(x == 0)
        {
            return null;
        }
        else
        {
            return x;
        }
    }

    Every execution path must end in either a return value or throwing something (usually an exception). There are a few odd cases where a method doesn't terminate normally (for example, System.exit()), but these are rare.

    Why not use booleans? These are meant to hold true/false values, ideal for answering yes/no type questions.

    public boolean isZero(int x)
    {
        if(x == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    youngstorm (November 19th, 2012)

  4. #3
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: returning null instead of int

    Hello Mike!

    Quote Originally Posted by youngstorm View Post
    How should/could I handle this? If my method has a return type, MUST I return a value of that type? Is there a way not to do that?
    Yes, you are correct. You must return a value of that type. There is no way you can avoid that - as far as I know.
    A common way to handle this situation is to have some "default" return values. For example, return 0 for default or -1 for an error. Then in your program you can check if these values are returned and handle them accordingly.

    Hope this helps.

  5. The Following User Says Thank You to andreas90 For This Useful Post:

    youngstorm (November 19th, 2012)

  6. #4
    Junior Member
    Join Date
    Oct 2010
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: returning null instead of int

    Thanks to both of you for your help. This is the info I was looking for. Also, glad to know about the wrapper classes, that will do what I was hoping to do. Again, no special reason; I'm just playing and learning.

    Thanks again guys.

    --- Update ---

    How do I mark this thread as solved?

  7. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: returning null instead of int

    At the top, under Thread Tools > Mark this thread as solved

  8. #6
    Junior Member
    Join Date
    Oct 2010
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: returning null instead of int

    Thanks again.

Similar Threads

  1. List methods add(int k, Data data), set(int k, Data data), remove(int k)
    By Enirox in forum Object Oriented Programming
    Replies: 3
    Last Post: September 20th, 2012, 06:43 AM
  2. ArrayList<Class> is returning null
    By havinFun in forum What's Wrong With My Code?
    Replies: 19
    Last Post: April 14th, 2012, 04:59 PM
  3. Using enum, returning null
    By 9erNumber16 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 13th, 2011, 05:40 PM
  4. Singly Linked List of Integers, get(int i) function throws Null Pointer Exception
    By felixtum2010 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 23rd, 2011, 06:55 PM
  5. Returning Null
    By Woody619 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: July 22nd, 2010, 12:53 PM