Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Java-get-example Java error missing return statement

Related Tutorials


 
 

Share on Google+Share on Google+

Java error missing return statement

Advertisement
Java error missing return statement are those error in Java that occurred when a programmer forget to write a return statement inside the conditional clauses. method with a non-void return provides you an object of the specific type.

Java error missing return statement

     

Java error missing return statement are those error in Java that occurred when a programmer forget to write a return statement inside the conditional clauses. method with a non-void return provides you an object of the specific type.

Understand with Example

In this Example we want to describe you a code that explain you  in understanding Java error missing return object. For this we have taken a non void method 'operation( )'that return you an int type and this method include a  for loop clause that check the condition If there is anything other than void method, then you must specify the return type in it. The given below code is missing a return statement in the non-void method so, whenever the compiler execute the code the code returns a missing return statement error.

There are two way to resolve this problem-

1)Change the non-void method to a void method.

2)Add or write a return expression to the non-void method.

Missingreturnstatement.java


public class Missingreturnstatement {

  public int operation(int num) {
  for (int i = 0; i <= num; i++) {
  System.out.println(i);
  }
  
  }

  public static void main(String[] args) {
  Missingreturnstatement m = new Missingreturnstatement();
  m.operation(7);
  }
}


Output

Compiling source file to /home/girish/NetBeansProjects/errors/build/classes
/home/girish/NetBeansProjects/errors/src/
Missingreturnstatement.java:
9: missing return statement

  }
error
BUILD FAILED (total time: seconds)


To resolve this error you have to give the return statement as return num;

Download code
Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 4, 2008

Related Tutorials

Discuss: Java error missing return statement  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:3
Naveen Kumar
March 31, 2011
Error

HI It is clear that you are not returning any value. The method operation() should return an integer.
René Verbunt
January 6, 2012
Help!!!!!

I am creating an Twitter App using NetBeans and i see an missing return statement error and im just a beginning developer so i dont know what to to whit this error. So i thought you can help me whit this error: private String arrayToString(char[] arr) { <----- here is the missing return statement error StringBuffer result = new StringBuffer(); for(int i=0;i<arr.length;i++) { result.append(arr[i]); return result.toString(); } } }
sk8terboi87
January 9, 2012
Thankk youu =)

I reallly 'not so java' guy... it really helped me out.. thankk you!! )
DMCA.com