CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 32
  1. #16
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    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

  2. #17
    Join Date
    Aug 2011
    Posts
    15

    Re: classInstanceInitalization

    Product p=new Product(IdP, Pname, Pcategory, Pprice);
    return p;

  3. #18
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: classInstanceInitalization

    Is that your full method?
    How is the code you posted used in the getFromId method?
    Norm

  4. #19
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #20
    Join Date
    Aug 2011
    Posts
    15

    Re: classInstanceInitalization

    Quote Originally Posted by Norm View Post
    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.

  6. #21
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  7. #22
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    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

  8. #23
    Join Date
    Aug 2011
    Posts
    15

    Re: classInstanceInitalization

    Quote Originally Posted by Norm View Post
    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.

  9. #24
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    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

  10. #25
    Join Date
    Aug 2011
    Posts
    15

    Re: classInstanceInitalization

    Quote Originally Posted by Norm View Post
    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.

  11. #26
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: classInstanceInitalization

    Show the source.
    Norm

  12. #27
    Join Date
    Aug 2011
    Posts
    15

    Re: classInstanceInitalization

    Quote Originally Posted by Norm View Post
    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(); }
    }
    }
    
    }
    }

  13. #28
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: classInstanceInitalization

    Which line is this on?
    hint shown in code editor.
    Norm

  14. #29
    Join Date
    Aug 2011
    Posts
    10

    Re: classInstanceInitalization

    You forgot the 't' in "ex.printStackTrace();".

  15. #30
    Join Date
    Aug 2011
    Posts
    10

    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. ;->

Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured