0

I want to return a Course object with this method but only if the object isn't null.

public Course getClass1()
{
    if(class1 != null)
    {
        return class1;
    }
}

I have a 'missing return statement' error because it may not return anything, but I don't want to return anything, not even an empty String, if the object is null. I am okay with changing the method to a void

public void getClass1()

{
    if(class1 != null)
    {
        System.out.println(class1);
    }
}

But if I do that I can't call the methods in my toString

public String toString()
{
    return super.getName() + "\t" + super.getAge() + "\t" + getStudentNumber() +
            "\n" + getClass1() + "\n" + getClass2() + "\n" + getClass3();
}

because void types aren't allowed.

Maciej Dobrowolski
  • 11,561
  • 5
  • 45
  • 67
user3006513
  • 39
  • 1
  • 4
  • 2
    *but I don't want to return anything, not even an empty String, if the object is null* then you must make sure to **always** have the `class1` variable initialized, and the code can be reduced to `return this.class1`. – Luiggi Mendoza Nov 18 '13 at 22:21
  • 1
    _why_ don't you want to return anything if the object is null? – Michał Rybak Nov 18 '13 at 22:21
  • 3
    The language simply doesn't allow what you're trying to do. I suggest returning null, or some other data type that can express the various results. – Mike Christensen Nov 18 '13 at 22:22

6 Answers6

7

You can't have two different return types, your method must return a specific type or nothing (void).

The only viable solution here is to have it return a Course, or null if no course is available.

public Course getClass1() {
  return class1;
}

public String toString() {
  Course class1 = getClass1();
  boolean class1Available = class1 != null;
  if (class1Available) {
    // something
  else {
    // something else
  }
}
Jack
  • 131,802
  • 30
  • 241
  • 343
2

You're going to have to return something, the Java language requires. You have a few choices:

  • Return a blank Course object:

    public Course getClass1() {
        if(class1 != null) {
            return class1;
        }
        return new Course();
    }
    
  • Return null.
    • This is fairly simple so I'll skip the code example
  • Back out and throw an exception:

    public Course getClass1() {
        if(class1 == null) {
            throw new NullPointerException("class1 is null");
        }
        return class1;
    }
    
    • NullPointerException is unchecked so no explicit throws clause.
artgento
  • 33
  • 1
  • 6
nanofarad
  • 40,330
  • 4
  • 86
  • 117
2

You should rather return null in getClass1 if there is null .

If you don't want to have nulls in toString() you can simply use three argument operator:

public Course getClass1() {
    return class1;
}

public String toString() {
    return super.getName() + "\t" + super.getAge() + "\t" + getStudentNumber() +
        (getClass1()==null?"":("\n" + getClass1())) + "\n" + getClass2() + "\n" + getClass3();
}

it works as following:

(condition ? value_if_true : value_if_false)

there's more explanation of this operator

Community
  • 1
  • 1
Maciej Dobrowolski
  • 11,561
  • 5
  • 45
  • 67
  • Sure, but this way you call getClass1() twice, which may be a problem if it does some heavy calculation. – PNS Nov 18 '13 at 22:33
  • @PNS In this context it's only simple getter for some kind of attribute. JVM will even change invocations of `getClass1()` to `this.class1`. – Maciej Dobrowolski Nov 18 '13 at 22:36
2

Cannot be done. If your method is declared to return ClassX, you must return either an instance of that class, one of its subclasses, or null. It should be a responsibility of a method client (caller) to perform different logic based on the value returned (or null).

amphibient
  • 29,770
  • 54
  • 146
  • 240
1

Problem here is what it should retrun in class1 is null? Any default/dummy object or Simply null. If its not returning default or dummy object, just remove this if condition as its not useful.

BSS
  • 76
  • 2
  • Method is expecting some value to be returned. Either class1 object or null. Here if requirement is do nothing in class1 == null, then will prefer to remove this if condition. If requirement is to return some default object like new Class1(); then if condition is usful. – BSS Nov 18 '13 at 22:32
1

There is no normal way to "return nothing" from a method, unless:

  1. The method has a void return type. This is not applicable in your case, unless you use an instance variable (e.g., this.class1) and modify that variable in the getClass1() method (which returns void), but this is not a good programming practice and if you follow it, at least rename getClass1() to something like update()
  2. The method throws an Exception. This does not make much sense here, since toString() should not throw exceptions.

If getClass1() is your main concern, you can easily modify the toString() method, as follows:

public String toString()
{
    Course c = getClass1();

    return super.getName() + "\t" + super.getAge() + "\t" + getStudentNumber() +
            "\n" + (c==null?"":c) + "\n" + getClass2() + "\n" + getClass3();
}

To generalize this behavior to any method with an Object return type, you can have a simple method print(), like

public String print(Object o)
{
    return (o==null?"":o.toString());
}

and then wrap all methods that may return a null value, inside print(), e.g.

public String toString()
{
    return super.getName() + "\t" + super.getAge() + "\t" + getStudentNumber() +
            "\n" + print(getClass1()) + "\n" + print(getClass2()) + "\n" + print(getClass3());
}
PNS
  • 19,295
  • 32
  • 96
  • 143