Strange error message - linked lists (again)

I have to keep at this linked list thing if I'm ever going to really understand it!!

I have created a method to create a linked list via user input. I'm receiving only one strange ERROR, which states:

"This method must return a result of of type ListADT.Node."

And I have just started building my class ListADT...

class ListADT{

public Node createList(int aNumber){// THIS IS WHERE THE ERROR POINTS

/*

* integer variable aNumber is the length of the list user wishes to create.

*/

Scanner console =new Scanner(System.in);

int number;// declare variable number as an integer.

Node newNode =new Node();// declare and initialize newNode.

Node first =new Node();// declare and initialize first.

Node last =new Node();// delcare and initizlize last.

try

{

for(int i = 0; i <= aNumber; i++){// for loop to enter numbers to build list.

System.out.println("Please enter an integer: ");// prompt user.

number = console.nextInt();// assign user input to variable number.

newNode.info = number;// assign number to newNode;

if(first ==null){// checks to see if Node first is a null value.

first = newNode;// if true, then this node is the first and last node in the list.

last = newNode;

}

else{// if first is not null, then newNode is placed at the end of the list.

last.link = newNode;

last = newNode;

}

}

return first;

}

catch (InputMismatchException imeRef)//Catches non-integer values

{

System.out.println("You entered a non-Integer! " + imeRef.toString());//Prints error message

}

}

[3042 byte] By [aiki985a] at [2007-10-3 8:10:19]
# 1
Its because theres a possibility that nothing will be returned because of your try...catch block.
CaptainMorgan08a at 2007-7-15 3:14:41 > top of Java-index,Java Essentials,New To Java...
# 2
> Its because theres a possibility that nothing will be> returned because of your try...catch block.So I'm using a try catch block in the wrong place? Maybe better to use in the main method?
aiki985a at 2007-7-15 3:14:41 > top of Java-index,Java Essentials,New To Java...
# 3
No, its fine. Its possible for the code in the try block to never be reached. If that happened, nothing would be returned. So you need to return something in your catch block.
CaptainMorgan08a at 2007-7-15 3:14:41 > top of Java-index,Java Essentials,New To Java...
# 4

> No, its fine. Its possible for the code in the try

> block to never be reached. If that happened, nothing

> would be returned. So you need to return something

> in your catch block.

That worked!! Now why can't the error messages help out a little more? I would have never guessed that there was a problem with the catch block not returning anything!!

thanks

aiki985a at 2007-7-15 3:14:41 > top of Java-index,Java Essentials,New To Java...
# 5
Or you could return something after the catch block.The important part is, keep in mind that try/catch blocks change the execution path of your code, and you have to make certain that any path through the method returns the value your method says it returns.
paulcwa at 2007-7-15 3:14:41 > top of Java-index,Java Essentials,New To Java...
# 6

> Now why can't the error messages help

> out a little more? I would have never guessed that

> there was a problem with the catch block not

> returning anything!!

The message was pretty informative -- it told you what was wrong. If the compiler could be so smart as to be able to tell you exactly what to fix, then the compiler would be able to write code all by itself -- and then you'd be out of a job.

paulcwa at 2007-7-15 3:14:41 > top of Java-index,Java Essentials,New To Java...
# 7
As a general rule (note this is not always true) the last line of your method should be a return statement. If you can manage this then you probably won't ever get that message again.
floundera at 2007-7-15 3:14:41 > top of Java-index,Java Essentials,New To Java...
# 8
> As a general rule (note this is not always true) the> last line of your method should be a return> statement. If you can manage this then you probably> won't ever get that message again.I understand...and will be more careful...thanks
aiki985a at 2007-7-15 3:14:41 > top of Java-index,Java Essentials,New To Java...