• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can anyone help me with this error

 
Amritpal Singh Moomie
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys i am getting this error with my code

SimpleLogin.java:70: missing return statement
}
^
1 error
Press any key to continue . . .


 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What gets returned if an SQLException occurs?
 
Amritpal Singh Moomie
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont know , i have not run it before
But i dont think its about sql exception
I think some syntax error
i cant figure it out
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code has 2 syntax errors. A mis-placed open-curly-bracket ('{') and a missing close parenthesis (')'). I will let you find them.

But the error you get suggests there is valid route through your method in which there is no return statement by the end of the executed code. So again, what happens when a SQLException occurs?

<Edit>
I mean logically... Trace the route through your code and predict what gets returned.
 
Amritpal Singh Moomie
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve i have email you the full code .
Sorry i am really new to java.
what i am basically trying to do is call CheckValidUser for a jsp page to check login from database
 
rahul sdfsdf
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
after ending the Catch block you have to add return statement and than it will work fine.
 
Kr Manish
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the function returns a String, compiler has to make sure that it actually does in ALL cases. You have 3 cases. You return "success" in one, "fail" in another. But if sqlexception occurs, you don't return string. Just return "fail" from catch block as well, apart from correcting the syntax errors. Should work fine.
 
Amritpal Singh Moomie
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But issue is that i want only "Success" or "fail" output
 
Amritpal Singh Moomie
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Manish
Let me try
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amritpal Singh Moomie wrote:Steve i have email you the full code .
Sorry i am really new to java.
what i am basically trying to do is call CheckValidUser for a jsp page to check login from database


Let's keep this on the forum. There is no need to use full code, or send the email.

1) Fix the syntax errors above
2) Walk through your code, and figure out what path(s) may not return a value
3) Determine what value you do want to return in those cases and modify the code accordingly
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the error says you are missing any return statement anywhere in your code.and i thinnk you got it where you are getting it.
 
Amritpal Singh Moomie
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help guys
i really appreciate

I have made this change to my code acording to Manish
But it is giving me only fail output even if login infomation is right.

 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there may be some mismatch of the words because you are using equals for comparing the two objects..

use equalsIgnoreCase for username
and equals for password..
it will not cause unnessary mistakes in your code..

Like password should only be case sensitive not username
 
Amritpal Singh Moomie
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Shanky that was helpful

But i am still stuck on that thing that it is still giving "fail" as default value
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amritpal Singh Moomie wrote:Thanks Shanky that was helpful

But i am still stuck on that thing that it is still giving "fail" as default value


You have a logic error in your IF(...) conditions. Presumably you have more than one user. What happens if the user who is trying to login is not the first user returned from the database?

Here is what I suggest:
Take a pen and paper and write out exactly the steps that occur. When you know why the logic is bad, see if you can fix it. One of the things you need to consider is: What are the exact conditions that create a failed login? I can think of 2 (plus an error condition). Make sure your method captures all conditions. Write now it only captures one, and only compares on the first user.
 
Amritpal Singh Moomie
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Steve
Can you please tell me is that my code line 34 i.e
return"fail";
setting the value to fail
Even if we have only one record in the database and I enter the same username and password.
It will return fail ?
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, only the first return statement will execute. That one at the end would not override a 'success' if return "success"; line was reached. That will return "fail" only if the method completes execution without reaching a different return statement. That can happen
- If there is an exception in the try {} block that gets caught in one of the catch {} blocks.
- If there are no results in the ResultSet (if rs.next() returns false the first time it is called)

 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not you check your code by debugging..

because what i think is your query is not executing properly and thats why line 30 is executing always..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic