Home arrow static arrow Java Programming [Archive] - Error: A return statement with expression must be contained in a method dec
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Error: A return statement with expression must be contained in a method dec
This topic has 9 replies on 1 page.

Posts:41
Registered: 8/8/04
Error: A return statement with expression must be contained in a method dec  
Aug 8, 2004 5:48 AM



 
i get an error when i compile this: Error: A return statement with expression must be contained in a method declaration that is declared to return a value.

public static void UserLinkList ()
{
try
{
InputStream in = null;
String url = "jdbc:odbc:Users";
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
Connection dbconn = DriverManager.getConnection (url);
Statement stmt = dbconn.createStatement ();
ResultSet rs = stmt.executeQuery ("SELECT Name, Surname, Username, Password, Group FROM Userlist;");
StringBuffer buffer = new StringBuffer ();

while (rs.next ())
{
buffer.append (rs.getString (1) + "\t");
buffer.append (rs.getString (2) + "\t");
buffer.append (rs.getString (3) + "\t");
buffer.append (rs.getString (4) + "\t");
buffer.append (rs.getString (5) + "\n");
return buffer.toString ();
//Adding values from database to specified arrays

//System.out.println (name + " \t " + surname + " \t " + username + "\t\t " + password);
//System.out.println();
//System.out.println (name);

//System.out.println(userarray);

}

rs.close ();
stmt.close ();
dbconn.close ();

}
 

Posts:1,135
Registered: 1/16/04
Re: Error: A return statement with expression must be contained in a method dec  
Aug 8, 2004 5:56 AM (reply 1 of 9)



 
Well here is your method declaration...
public static void UserLinkList ()

notice that you declared it as a void meaning that this method does not return a value of any type...
but... here...
return buffer.toString ();

you are returning a String...
and that is why you are getting the error...

If you want to return a String then the method must be declared to return a String...
public static String UserLinkList ()

As you can note, now it has a return type of String and no longer void...

Hope this helps...

- MaxxDmg...

- ' He who never sleeps... '
 

Posts:18,384
Registered: 21.03.00
Re: Error: A return statement with expression must be contained in a method  
Aug 8, 2004 6:21 AM (reply 2 of 9)



 
Hi,

I this line of code:

   return buffer.toString();


Should be outside the loop, after you have closed the statement, records etc.

/Kaj
 

Posts:41
Registered: 8/8/04
Re: Error: A return statement with expression must be contained in a method  
Aug 8, 2004 6:34 AM (reply 3 of 9)



 
guys when i do this:i get this:Error: The method "java.lang.String UserLinkList();" must contain a return statement with an expression compatible with type "String".

public static String UserLinkList ()
{
try
{
InputStream in = null;
String url = "jdbc:odbc:Users";
Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
Connection dbconn = DriverManager.getConnection (url);
Statement stmt = dbconn.createStatement ();
ResultSet rs = stmt.executeQuery ("SELECT Name, Surname, Username, Password, Group FROM Userlist;");

while (rs.next ())
{
NameBuffer.append (rs.getString (1) + "\n");
SurnameBuffer.append (rs.getString (2) + "\t");
UsernameBuffer.append (rs.getString (3) + "\t");
PasswordBuffer.append (rs.getString (4) + "\t");
GroupBuffer.append (rs.getString (5) + "\n");
// return buffer.toString ();
//Adding values from database to specified arrays

//System.out.println (name + " \t " + surname + " \t " + username + "\t\t " + password);

}

rs.close ();
stmt.close ();
dbconn.close ();
return NameBuffer.toString();

}

plz show me the solution it is imprtant

 

Posts:18,384
Registered: 21.03.00
Re: Error: A return statement with expression must be contained in a method  
Aug 8, 2004 6:38 AM (reply 4 of 9)



 
Hi,

public static String UserLinkList () {    try {        InputStream in = null;        String url = "jdbc:odbc:Users";        Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");        Connection dbconn = DriverManager.getConnection (url);        Statement stmt = dbconn.createStatement ();        ResultSet rs = stmt.executeQuery ("SELECT Name, Surname, Username, Password, Group FROM                 Userlist;");        StringBuffer buffer = new StringBuffer ();         while (rs.next ()) {                buffer.append (rs.getString (1) + "\t");                buffer.append (rs.getString (2) + "\t");                buffer.append (rs.getString (3) + "\t");                buffer.append (rs.getString (4) + "\t");                buffer.append (rs.getString (5) + "\n");        }         rs.close ();        stmt.close ();        dbconn.close ();        return buffer.toString ();   } catch (Exception e) {        e.printStackTrace();   }}


But then, as stated before, you
 

Posts:18,384
Registered: 21.03.00
Re: Error: A return statement with expression must be contained in a method  
Aug 8, 2004 6:39 AM (reply 5 of 9)



 
Oops... Hadn't finished typing. But as stated before, you should do something with the exceptions.

/Kaj
 

Posts:41
Registered: 8/8/04
Re: Error: A return statement with expression must be contained in a method  
Aug 8, 2004 6:50 AM (reply 6 of 9)



 
still giving me the error:Error: The method "java.lang.String UserLinkList();" must contain a return statement with an expression compatible with type "String".
 

Posts:18,384
Registered: 21.03.00
Re: Error: A return statement with expression must be contained in a method  
Aug 8, 2004 6:55 AM (reply 7 of 9)



 
Add return null after the catch statement.

/Kaj
 

Posts:1,135
Registered: 1/16/04
Re: Error: A return statement with expression must be contained in a method  
Aug 8, 2004 6:58 AM (reply 8 of 9)



 
Add return null after the catch statement.

/Kaj

Oops... I did not "catch" that one... :-P

- MaxxDmg...

- ' He who never sleeps... '
 

Posts:41
Registered: 8/8/04
Re: Error: A return statement with expression must be contained in a method  
Aug 8, 2004 6:59 AM (reply 9 of 9)



 
THANX,all the **** worked.great help.i am actually busy with a project,so i will hav stupid questions to be answer,plz bear with me if u want to
 
This topic has 9 replies on 1 page.