-
August 23rd, 2011, 02:41 PM
#16
Re: classInstanceInitalization
Is there a definition for the variable p? Is it in scope where you are trying to use it?
You need to move the definition of p to a place where it is visible to any code that wants to reference it.
The Scope/visibility of variables is defined by their enclosing {}s
Norm
-
August 23rd, 2011, 04:15 PM
#17
Re: classInstanceInitalization
Product p=new Product(IdP, Pname, Pcategory, Pprice);
return p;
-
August 23rd, 2011, 04:31 PM
#18
Re: classInstanceInitalization
Is that your full method?
How is the code you posted used in the getFromId method?
Norm
-
August 23rd, 2011, 04:32 PM
#19
Re: classInstanceInitalization
When posting code please use code tags, it makes it so much easier for us to read your code.
Your problem is you have blindly added a 'return p' to the end of your code which makes no sense whatsoever. Norm has correctly pointed out that you haven't a return value at every possible exit of the method and hence you are getting a compile error. But there's a reason the code can exit at the bottom of the method and you need to identify the reason before attempting to add code.
Before you just add a return statement think about how the thread of execution is getting to that point. It can only get there if an exception has been thrown and caught within the method thus bypassing the original return statement. Given there's a problem which prevents you from creating a Product object all you can realistically do is return null or throw an exception for the calling method to handle.
-
August 24th, 2011, 03:52 AM
#20
Re: classInstanceInitalization
Originally Posted by Norm
Is that your full method?
How is the code you posted used in the getFromId method?
Code:
public static Product getFromId(int productID)
{
Connection conn = null;
{
try
{
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
conn = DriverManager.getConnection ("jdbc:mysql://localhost/prodaja", "root", "");
System.out.println(conn.isClosed());
}
catch(Exception ex)
{
System.out.println("Connection not established");
}
try
{
Statement st = conn.createStatement();
st.executeQuery("select * from products WHERE ID_product="+productID);
ResultSet rs = st.getResultSet();
while (rs.next())
{
int IdP = rs.getInt(1);
String Pname = rs.getString(2);
String Pcategory = rs.getString(3);
Double Pprice = rs.getDouble(4);
Product p=new Product(IdP, Pname, Pcategory, Pprice);
return p;
}
}
catch (SQLException s){
System.out.println("SQL statement not executed!");
}
finally {
try
{
if(conn!=null && !conn.isClosed())
conn.close();
System.out.println(conn.isClosed());
}
catch(Exception ex) { }
return null;
}
}
}
}
I put "return null" statement at the end of class, as you can see, and now there is no error message at all, but method returns nothing.
-
August 24th, 2011, 04:50 AM
#21
Re: classInstanceInitalization
The point of using code tags is to preserve your indentation but you haven't used consist indentation so it's only marginally better than it was before. Always format your code consistently, it makes it so much easier for you to follow and if you use an industry standard format it makes it so much easier for other people to follow. Bare in mind the easier it is to follow your code the more likely it is that one of us will be willing to spend time trying to help you.
I put "return null" statement at the end of class, as you can see, and now there is no error message at all, but method returns nothing.
In that case it must be throwing an exception.
Make sure you print something out from every exception you catch and tell us what is being printed to the screen?
BTW if you catch a exception during connecting to the DB why are you not exiting (ie returning null) there and then. There's no point trying to execute the SQL statement if you don't have a connection.
-
August 24th, 2011, 06:44 AM
#22
Re: classInstanceInitalization
but method returns nothing.
Are you sure. The compiler forced you to return something. So either it returns a object or it returns the null value. Which is being returned?
Add some more println statements to your code to show where the execution flow goes.
Add calls to printStackTrace to ALL of the catch blocks to show what the errors are if there are any.
For example:
Code:
catch(Exception ex) {ex.prinStackTrace(); }
Norm
-
August 24th, 2011, 08:11 AM
#23
Re: classInstanceInitalization
Originally Posted by Norm
Are you sure. The compiler forced you to return something. So either it returns a object or it returns the null value. Which is being returned?
Add some more println statements to your code to show where the execution flow goes.
Add calls to printStackTrace to ALL of the catch blocks to show what the errors are if there are any.
For example:
Code:
catch(Exception ex) {ex.prinStackTrace(); }
When I call method from main class and run it, the output is like this:
run:
false
true
BUILD SUCCESSFUL (total time: 0 seconds)
Also, when I implement printStackTrace, compiler tells me "Throwable.printStackTrace should be removed".
Then, when I run it like this, the result is the same like above.
Last edited by nera1981; August 24th, 2011 at 08:23 AM.
-
August 24th, 2011, 08:31 AM
#24
Re: classInstanceInitalization
run:
false
true
What does the above mean?
compiler tells me "Throwable.printStackTrace should be removed"
What is the full text of the error message?
Norm
-
August 24th, 2011, 08:46 AM
#25
Re: classInstanceInitalization
Originally Posted by Norm
run:
false
true
What does the above mean?
compiler tells me "Throwable.printStackTrace should be removed"
What is the full text of the error message?
Like I said before, this is the output when I run program.
"Throwable.printStackTrace should be removed" is hint shown in code editor.
-
August 24th, 2011, 08:46 AM
#26
Re: classInstanceInitalization
Norm
-
August 24th, 2011, 09:12 AM
#27
Re: classInstanceInitalization
Originally Posted by Norm
Show the source.
Code:
public static Product getFromId(int productID)
{
Connection conn = null;
{
try
{
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
conn = DriverManager.getConnection ("jdbc:mysql://localhost/prodaja", "root", "");
System.out.println(conn.isClosed());
}
catch(Exception ex)
{ ex.prinStackTrace();
System.out.println("Connection not established");
}
try
{
Statement st = conn.createStatement();
st.executeQuery("select * from products WHERE ID_product="+productID);
ResultSet rs = st.getResultSet();
while (rs.next())
{
int IdP = rs.getInt(1);
String Pname = rs.getString(2);
String Pcategory = rs.getString(3);
Double Pprice = rs.getDouble(4);
Product p=new Product(IdP, Pname, Pcategory, Pprice);
return p;
}
}
catch (SQLException s)
{s.prinStackTrace();
System.out.println("SQL statement not executed!");
}
finally {
try
{
if(conn!=null && !conn.isClosed())
conn.close();
System.out.println(conn.isClosed());
}
catch(Exception ex) {ex.prinStackTrace(); }
}
}
}
}
-
August 24th, 2011, 09:19 AM
#28
Re: classInstanceInitalization
Which line is this on?
hint shown in code editor.
Norm
-
August 24th, 2011, 09:43 AM
#29
Re: classInstanceInitalization
You forgot the 't' in "ex.printStackTrace();".
-
August 24th, 2011, 09:46 AM
#30
Re: classInstanceInitalization
The quickest easiest way to fix the last remaining compile problem is to add "return null;" as the last line in the method. This will fix the "This method must return a result of type Product" compiler error message. But it could cause your program to fail in other places. ;->
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|