missing return statement. Why?
Hi guys this might be an easy one for you all.
I am receiving the error below when compiling the class below. Any idea what kind of statement I should be returning?
Database.java:51: missing return statement
}
^
1 error
public Item search(String title){
String ItemName;
for (int i=0; i < items.length; i++) {
System.out.println (items[i]);
ItemName = items[i].getTitle(); // polymorphism
if (ItemName.equals(title)) {
System.out.println ("Match Exists!");
} else {
System.out.println ("No matches found");
}
}
}
[738 byte] By [
cases] at [2007-11-20 11:26:01]

# 1 Re: missing return statement. Why?
because your return statement is missing
your method declaration states that you will return an Item, but you never do.
//something like this
return items[0];
either that, or change your method declaration to return void
public void methodname(args..)
# 2 Re: missing return statement. Why?
Im not even sure what value to return though.
Why wouldn't return items[i]; work? Why do I need to specify an actual number like return items[0];?
cases at 2007-11-10 2:15:05 >

# 3 Re: missing return statement. Why?
well there is your problem, you need to know what to return first. returning items[i]; will work, but i havent looked at your code...
what are you trying to do?
# 4 Re: missing return statement. Why?
It would work if it was in your code but the code you've posted doesn't contain any return statements.
Having said that if you put return items[i]; in your for loop the compiler may still complain if there is no guarantee that that is the only way for the method to exit. For example if the items array length is 0 then the for loop will be bypassed and then method will end at the method's final curly brace (without a return statement). If there is nothing sensible that can be returned either return null or throw an exception.
keang at 2007-11-10 2:17:09 >

# 5 Re: missing return statement. Why?
looks like you need to return the i'th item if the title matches what you are looking for, and i am guessing you should return a null if you didnt find any items.
thats your best bet i suppose.
# 6 Re: missing return statement. Why?
We are able to compile database class using the code below but now I receive the error in my last post:
null
Exception in thread "main" java.lang.NullPointerException
at Database.search(Database.java:39)
at Driver.main(Driver.java:70)
Here is the current code:
public Item search(String title){
String ItemName;
for (int i=0; i < items.length; i++) {
System.out.println (items[i]);
ItemName = items[i].getTitle(); // polymorphism
if (ItemName.equals(title)) {
System.out.println ("Match Exists!");
return items[i];
} else {
System.out.println ("No matches found");
} // if
} // for loop
return null;
} // end of search() method
By the way, line 39 is as follows:
ItemName = items.getTitle();
To confirm what I am trying to do, I am trying to Search for an item by title in public static void man. As seen from the for loop I am covering the case where the item is found, and the case where a matching item cannot be found in the Database.
cases at 2007-11-10 2:19:15 >

# 7 Re: missing return statement. Why?
that probably means that items[i] == null at that line
you seem to print the item before line 39, so is it null?
id need to see the rest of your code to judge what is null where..
or you can put a bunch of print statements, and print all the objects that are in use in that function around line 39, one of them is null.
# 8 Re: missing return statement. Why?
A NullPointerException is thrown when you are trying to use an object variable that isn't referencing any obejcts ie it's value is null. So, in your case, this means that the array 'items' contains one (or possibly more) null values. If having nulls is valid then you need to test for null's before calling any methods on the non-existant object eg something like:
if ( items[i] == null )
continue; If the array isn't supposed to have any nulls in it then your problem is where your array is being intialised ie it's not being done properly.
keang at 2007-11-10 2:21:14 >

# 9 Re: missing return statement. Why?
Keang I believe you have hit the nail on the head. The array does have items and I am able to print out a list of items from public static void main but the problem is we are to initialize the array from public static void main and then search for the item to see if it exists using the method in this post.
This is what I have in public main:
public static void main(String[] args){
Item[] items;
items = new Item[2];
items[0] = new Book ("Davinci Code", "Blah);
items[1] = new Book ("Jaws", "Tom Hanks");
Now my job is to call the search method (the one I pasted in the original post) to search if "Davinci code" exists or not. Any suggestions?
cases at 2007-11-10 2:22:12 >

# 10 Re: missing return statement. Why?
You are declaring 'items' as a local variable in your main method, the 'items' that you are using in your search method is a different variable altogether.
keang at 2007-11-10 2:23:17 >

# 11 Re: missing return statement. Why?
Thanks Keang for pointing this out but as you can see im a newbie and will need a little guidance in getting this to work. I assume you understand the problem at hand, how do I tell void main to have search search for items in the parent class to see if they exist or not?
cases at 2007-11-10 2:24:16 >

# 12 Re: missing return statement. Why?
We're not here to teach Java, we help to solve peoples programming problems and unfortunately this post has now moved from the problem to teaching domain. The sort of information you need is available in hundreds of online tutorials I suggest you read a few of them.
However I'm feeling generous today so here's what you do. Your main method has to do the following
1. Create an instance of the class (presumably it's in the same class as the search method).
2. Populate the instance with data ie create and add the new books to it. You may need to implement an addBook(..) method to fill the item array (which should really be an ArrayList) with books.
3. Call the instance's search(..) method.
keang at 2007-11-10 2:25:14 >

# 13 Re: missing return statement. Why?
Hi Keang,
The public void main class is actually a separate class so I'll be calling the method from that particular class. I do have an addItem method which belongs to the same class as the search method. So im guessing what I need to do is something such as the following:
public static void main(String[] args){
Item[] items;
items = new Item[2];
items[0] = new Book ("Davinci Code", "Blah);
items[1] = new Book ("Jaws", "Tom Hanks");
List db = new List();
additem.items[0];
search("Jaws");
Of course I may be way off here, please don't laugh if I am though (although im sure you will).
cases at 2007-11-10 2:26:23 >

# 14 Re: missing return statement. Why?
The public void main class is actually a separate class so I'll be calling the method from that particular class. I do have an addItem method which belongs to the same class as the search method. Have you tried reading what you just wrote? It makes no sense at all without some context...
The code you posted makes no sense either - did you leave out a large chunk?
Of course I may be way off here, please don't laugh if I am though (although im sure you will).It's not really a laughing matter - I think you need to step back from this and think about what you're doing. Read Keang's last post more carefully, and find out the difference between static and non-static (instance) methods and data.
The most important single aspect of software development is to be clear about what you are trying to build...
B. Stroustrup
dlorde at 2007-11-10 2:27:21 >

# 15 Re: missing return statement. Why?
As I said before you need to read up on some of the basics. Programming in Java is not best learned by trial and error (which what you appear to be doing now). When you come across something you don't understand read as much as you can about how to do it before attempting to code it.
Ask yourself what is the point of creating your object 'db' when you don't use it anywhere, what does the line additem.items[0]; actually do and how does the compiler know which object's search method you are trying to call.
keang at 2007-11-10 2:28:25 >

# 16 Re: missing return statement. Why?
Hi dlorde,
Yes the last bit of code I pasted was incomplete. I have two classes, the first class contains the following:
public static void main(String[] args){
Item[] items;
items = new Item[2];
items[0] = new Book ("Davinci Code", "Blah);
items[1] = new Book ("Jaws", "Tom Hanks");
}
The second class contains the following:
public Item search(String title){
String ItemName;
for (int i=0; i < items.length; i++) {
System.out.println (items[i]);
ItemName = items[i].getTitle(); // polymorphism
if (ItemName.equals(title)) {
System.out.println ("Match Exists!");
return items[i];
} else {
System.out.println ("No matches found");
} // if
} // for loop
return null;
} // end of search() method
What I am trying to do is search for the Item methods in the first class using the method in the second class and to return a string if the match exists or doesn't exist. My problem is I am having difficulty calling the search method from the first class. I guess the syntax to use is my biggest problem.
cases at 2007-11-10 2:29:20 >

# 17 Re: missing return statement. Why?
What I am trying to do is search for the Item methods in the first class using the method in the second class and to return a string if the match exists or doesn't exist. My problem is I am having difficulty calling the search method from the first class. I guess the syntax to use is my biggest problem.Syntax isn't the biggest problem. Ignoring advice is your biggest problem
Keang has already told you that you've declared the 'items' array as a local variable in a static method, so it can't possibly be the same 'items' array you're accessing from a different method. He also told you how to achieve what you said you want to do. I told you to read again what he said, and we both told you to read up on the basics.
Yet you ignore all that, and just post up the same stuff again.
If a method can't access local variables in another method of the same class, then it clearly follows that it can't access local variables in a method of a different class.
I suggest you scrap that code, work through a basic tutorial (http://java.sun.com/docs/books/tutorial/index.html), then start again when you understand the basics about classes, static methods & variables, instance methods & variables, and access levels.
Why do we never have time to do it right, but always have time to do it over?
Anon.
dlorde at 2007-11-10 2:30:28 >

