Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can't define a variable

 
james smitho
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12021
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you declare "db" twice here:



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

 
james smitho
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
james smitho
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed. Well, I think this is correct . But still same error than last time.









 
W. Joe Smith
Ranch Hand
Posts: 710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 299
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Show all the code of that method, Are you doing this:


 
james smitho
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 299
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
james smitho
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! That was great.

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




And the errors;

 
Rok Štelcer
Ranch Hand
Posts: 101
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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




And the code;

 
Rok Štelcer
Ranch Hand
Posts: 101
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if I add a line Connection db = null;
like this;




compiler says one error:



But, I don't know...
 
Rok Štelcer
Ranch Hand
Posts: 101
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here you go:


Regards,
Rok
 
james smitho
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic