wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes can't define a variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "can Watch "can New topic
Author

can't define a variable

james smitho
Ranch Hand

Joined: Nov 16, 2009
Posts: 32
Hi. I can't solve how I define variable db in this class named Search.

Basic is fine, no problemos. But in Search compiler says just one error;


And I want to make sure that also Data.getConnection() in Search would be all right and defined.

Here's Basic;




And Search;

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

you declare "db" twice here:



I think you can delete the line you suggest and it will work.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
james smitho
Ranch Hand

Joined: Nov 16, 2009
Posts: 32
Yes, you were right. I deleted that. But still error;


unreported exception java.lang.Exception; must be caught or declared to be thrown
[javac] Connection db = Data.getConnection();
^


Should I change Connection db ???
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
james smitho wrote:Yes, you were right. I deleted that. But still error;


unreported exception java.lang.Exception; must be caught or declared to be thrown
[javac] Connection db = Data.getConnection();
^


Should I change Connection db ???


Put it in a try/catch block. In the first section, you had this:



But later on, you call Data.getConnection(); with no try/catch.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
james smitho
Ranch Hand

Joined: Nov 16, 2009
Posts: 32
Indeed. Well, I think this is correct . But still same error than last time.









W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
james smitho wrote:Indeed. Well, I think this is correct . But still same error than last time.





I don't think you can have 2 catch's catching the same exception type.
james smitho
Ranch Hand

Joined: Nov 16, 2009
Posts: 32
I think this is right.

try {
db = Data.getConnection();
execute(req, out, db);
} catch (UserException e) {
out.println( "<h1>Error: " + e.getMessage() + "</h1>" );
} catch (Exception e) {
out.println( "<h1>Internal error: " + e + "</h1>" );
} finally {
if (db != null)
Data.closeConnection(db);
}


But: still;

unreported exception java.lang.Exception; must be caught or declared to be thrown
[javac] Connection db = Data.getConnection();
[javac] ^
james smitho
Ranch Hand

Joined: Nov 16, 2009
Posts: 32
I don't know how this problem go away? Please, help.




But: still compiler says;

unreported exception java.lang.Exception; must be caught or declared to be thrown
[javac] Connection db = Data.getConnection();
[javac] ^
Albareto McKenzie
Ranch Hand

Joined: Apr 08, 2009
Posts: 298
Show all the code of that method, Are you doing this:


james smitho
Ranch Hand

Joined: Nov 16, 2009
Posts: 32
Thanks for answering. I'm going crazy with this.

I tried like this;




And now I tried to change like you suggested;



But error; cannot find symbol
[javac] symbol : variable db
[javac] location: class theater.BasicServlet


( I think your line is better than mine, but how we solve this problem? )
Albareto McKenzie
Ranch Hand

Joined: Apr 08, 2009
Posts: 298
james smitho wrote:Thanks for answering. I'm going crazy with this.

I tried like this;




What was the result of this?



And now I tried to change like you suggested;



But error; cannot find symbol
[javac] symbol : variable db
[javac] location: class theater.BasicServlet


( I think your line is better than mine, but how we solve this problem? )


I didn't suggest you to do that, I asked you if you were doing that, that code will fail, the db is not declared but if it were declared you would have get the UncaughtException but I thought you could have been doing something like that
Rok Štelcer
Ranch Hand

Joined: Nov 03, 2009
Posts: 101
james smitho wrote:I don't know how this problem go away? Please, help.




But: still compiler says;

unreported exception java.lang.Exception; must be caught or declared to be thrown
[javac] Connection db = Data.getConnection();
[javac] ^


This is a bit odd ... I'm suspecting that you're looking in wrong lines.
Output from the compiler is following:
[javac] Connection db = Data.getConnection();
Search for that specific line!


Regards,
Rok


SCJP, SCWCD
james smitho
Ranch Hand

Joined: Nov 16, 2009
Posts: 32
Hi. Thank you. So the problem is a line



in Search- class. ? But how I solve that??
I don't know how?! Please help.










But still that same error in Search- class;



W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
james smitho wrote:Hi. Thank you. So the problem is a line



in Search- class. ? But how I solve that??
I don't know how?! Please help.










But still that same error in Search- class;





Do a Google search for catching exceptions in Java. I suspect it is because this line throws an exception and you aren't doing anything to catch it:

Connection db = Data.getConnection();
james smitho
Ranch Hand

Joined: Nov 16, 2009
Posts: 32
Thanks. All right, that must be the reason! This is my best shot. ( I don't have a chance to test this for hours...unfortunately...).
But does that exception ( same as in other class... ) look good?
Or if it doesn't, how should it be changed in this case?


Rok Štelcer
Ranch Hand

Joined: Nov 03, 2009
Posts: 101
James,

james smitho wrote:Thanks. All right, that must be the reason! This is my best shot. ( I don't have a chance to test this for hours...unfortunately...).
But does that exception ( same as in other class... ) look good?
Or if it doesn't, how should it be changed in this case?

No, you still didn't fix it ... the result will still be the same.
Problematic code snip:



Your code should look something like:

Let us know the results.


Regards,
Rok
james smitho
Ranch Hand

Joined: Nov 16, 2009
Posts: 32
Thanks! That was great.

Well, still errors. But here is the whole code now and the results;




And the errors;

Rok Štelcer
Ranch Hand

Joined: Nov 03, 2009
Posts: 101
James,

Since you're using db var multiple times, change yout finally block to:


# .java:96: db is already defined in doPost(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
# [javac] Connection db = null;
# [javac] ^
# .java:137: db is already defined in doPost(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
# [javac] Connection db = null;
# [javac] ^
# [javac] 7 errors
#
# BUILD FAILED

Remove following lines: 69 & 110 (declaration & initialization of same object ... in your case db).
or instead of: "Connection db = null;", change it to "db = null;"

# cannot find symbol
# [javac] symbol : variable db
# [javac] location: class theater.BasicServlet
# [javac] db = Data.getConnection();
# [javac] ^
# .java:30: cannot find symbol
# [javac] symbol : variable db
# [javac] location: class theater.BasicServlet
# [javac] db = Data.getConnection();
# [javac] ^
# .java:31: cannot find symbol
# [javac] symbol : variable db
# [javac] location: class theater.BasicServlet
# [javac] execute(req, out, db);
# [javac] ^
# .java:37: cannot find symbol
# [javac] symbol : variable db
# [javac] location: class theater.BasicServlet
# [javac] if (db != null)
# [javac] ^
# .java:38: cannot find symbol
# [javac] symbol : variable db
# [javac] location: class theater.BasicServlet
# [javac] Data.closeConnection(db);
# [javac]

This error has to do with BasicServlet and NOT with SearchServlet.


Regards,
Rok
james smitho
Ranch Hand

Joined: Nov 16, 2009
Posts: 32

Thanks again, Rok! This is really difficult problem.
still errors;




And the code;

Rok Štelcer
Ranch Hand

Joined: Nov 03, 2009
Posts: 101
James,

Perhaps I was not clear enough in previous post.
Rok Štelcer wrote:
This error has to do with BasicServlet and NOT with SearchServlet.
Compiler is complaining that db var is not defined in BasicServlet.
So, in order to help you, you'll have to share with us the source of mentioned class.


Regards,
Rok
james smitho
Ranch Hand

Joined: Nov 16, 2009
Posts: 32
Thanks. All right. This is difficult, I'm sorry. It would be great to
get this work! Here's the code;




james smitho
Ranch Hand

Joined: Nov 16, 2009
Posts: 32
And if I add a line Connection db = null;
like this;




compiler says one error:



But, I don't know...
Rok Štelcer
Ranch Hand

Joined: Nov 03, 2009
Posts: 101
Here you go:


Regards,
Rok
james smitho
Ranch Hand

Joined: Nov 16, 2009
Posts: 32
FANTASTIC!!! YES!! IT COMPILES, NO ERRORS!!

And now it works, yes indeed!

Rok, you are great!!! Thanks so much!! Magnificent help!
You helped ( of course thanks to all! ) me!!!



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can't define a variable
 
Similar Threads
Servlet can't find other servlet
File upload - Error 404 - servlet not available
Postgres on WSAD 5.1 - it works
Is it correct ???
Tomcat version