Reputation: 165
I've got stuck here in a simply method because I can't get it compiling.
I'm doing for each loop of objList
which is an ArrayList<anObject>
. I know the compiler can't get it compiled because there is no "Top-Level Return".
anObject getObjByID(int id){
for(anObject obj : objList ){
if (obj.getID() == id){
return node;
}
}
}
Also I don't know what to return when the conditions doesn't met. I can't return a anObject because there isn't. Hope you can help me to provide solutions.
Upvotes: 2
Views: 1017
Reputation: 140427
There are basically two different options:
Not finding an object is a valid scenario; then you might return null
. Or maybe some special Singleton object that represents an empty search result - as many people argue that using null
in such a context is a bad idea (it forces the caller of your method to check for null; or you risk a NullPointerExceptions
)
Not finding an object is an error that should not occur: then you can simply throw an exception after the loop.
Upvotes: 0
Reputation: 62864
There are several things you can do:
null
Not the best practice, though, because the method's client will be forced to handle the possibly returned null
(and if he doesn't, a NullPointerException
can be thrown).
Your method would look like:
anObject getObjByID(int id){
for(anObject obj : objList ){
if (obj.getID() == id){
return obj;
}
}
return null;
}
In this case, the client will have to deal with the case of null
results:
anObject result = getObjByID(someId);
if (result != null) {
//proceed
}
IllegalArgumentException
(or some other Exception
)You can throw IllegalArgumentException
to denote there's no corresponding Node
for the provided id
. The client will be forced to handle the exception.
Your method would look like:
anObject getObjByID(int id) throws IllegalArgumentException {
for(anObject obj : objList ){
if (obj.getID() == id){
return obj;
}
}
throw new IllegalArgumentException("Invalid index");
}
and the client will be have to handle the exception:
try {
anObject result = getObjByID(someId);
//proceed
} catch (IllegalArgumentException e) {
//handle the exception
}
Optional<T>
typeJava8 introduces the Optonal<T>
class, which is something like an alien - it may or it may not exist. Specifying the return type of a method to be Optional<T>
helps the client have the awareness that a return value may exist, or may not.
Your method would look like:
Optional<anObject> getObjByID(int id){
Optional<anObject> result = Optional.empty();
for(anObject obj : objList ){
if (obj.getID() == id){
result = Optional.of(obj);
}
}
return result;
}
The client will use this method like this:
Optional<anObject> result = getObjByID(someId);
if (result.isPresent()) {
anObject realResultObject = result.get();
}
Upvotes: 4
Reputation: 15886
From ls-8.4.7
If a method is declared to have a return type, then a compile-time error occurs if the body of the method can complete normally (§14.1).
Mean return your expected object to complete method or return null
.
And From jls-8.4.5
Return types may vary among methods that override each other if the return types are reference types. The notion of return-type-substitutability supports covariant returns, that is, the specialization of the return type to a subtype.
A method declaration d1 with return type R1 is return-type-substitutable for another method d2 with return type R2, if and only if the following conditions hold:
If R1 is void then R2 is void.
If R1 is a primitive type, then R2 is identical to R1.
If R1 is a reference type then:
R1 is either a subtype of R2 or R1 can be converted to a subtype of R2 by unchecked conversion (§5.1.9), or
R1 = |R2|
Means if you are using java 7 then return Type should be as above condition.
If you are using java 8 check JLS 8.4.5. Method Result
Upvotes: 0
Reputation: 936
You could simply throw an exception with an specific message, which is a good solution in my opinion. But this will intrerrupt the flow on the program if not catched.
anObject getObjByID(int id){
for(anObject obj : objList ){
if (obj.getID() == id){
return node;
}
}
thronw new UserDefinedException("Item not found in list");
}
Upvotes: 0
Reputation: 95948
Also I don't know what to return when the conditions doesn't met.
That really depends on your logic. If you want to allow null
, you shouldn't return it if no object is found, you can create your own object that indicates that the object was not found or throw an exception that will be caught later.
If your object cannot be null
, you can assume that null
is returned when the object is not found.
If you're using Java 8, you have much more better alternatives, see Optional
.
Please follow Java naming conventions
Upvotes: 1
Reputation: 35557
You have to return
some default
value if your searching object
not found. you can return null
here.
anObject getObjByID(int id){
for(anObject obj : objList ){
if (obj.getID() == id){
return node; // will return when searching matched
}
}
return null; // will return matching not found.
}
Throwing an exception
is another option you can use.
anObject getObjByID(int id){
for(anObject obj : objList ){
if (obj.getID() == id){
return node;
}
}
throw new MyException("Element Not Found");
}
Upvotes: 3
Reputation: 3557
You need to return a value in every case. Null seems good here.
anObject getObjByID(int id){
for(anObject obj : objList ){
if (obj.getID() == id){
return node;
}
}
return null;
}
You always need to consider that you can call this method and the if statement doesnt equal to true. Therefore what should your method return? It needs either an object of type anObject
or you return null
. Sometimes it makes sense to throw an exception if this case should never happen.
Upvotes: 1
Reputation: 7042
anObject getObjByID(int id){
for(anObject obj : objList ){
if (obj.getID() == id){
return node;
}
}
return null;
}
Upvotes: 0