How could I like programming ?

When you spend 2 days on one single, pointless line and the only thing you get to see is an ERROR-response. It`s really inhuman and I cant imagine who would want this voluntarily.

Nevertheless, what am I yammering about is following error, that is absolutely god-awful:

<body>


<%@ page import="java.sql.*" %> 
<%@ page import="java.io.*" %> 
<%@ page language="java" session="true" %>
 <%@ include file="basic_login.jsp" %>
 
<%
java.sql.Connection conn;
        java.sql.Statement stmt;
        java.sql.ResultSet resDisplay;
        java.sql.ResultSet resInsert;

try{

			out.println("TRYING......");
			
			 Class.forName("org.postgresql.Driver");        
		        conn = DriverManager.getConnection(dburl);   

 /// variables definition and commands follow

} 

catch(Exception e){e.printStackTrace();}
    	
    	finally{
            try{
                if(resInsert != null){
                    resInsert.close();
                }
                
                if(stmt !=null){
                	stmt.close();
                }
                   
                if(conn != null){
                    conn.close();
                }
            }
            catch(Exception e2){
                out.println("Unable to close connection: "+e2.getMessage());
            }
         }
%>

SO. I run the html that uses this JSP, fill in the fields, press the submit button and, oh wonder, an error comes along. Well what does this failure says? A simple :

The local variable resInsert may not have been initialized
105: if(resInsert != null){


The f* variable is DEFINITELY INITIALIZED, right at the beginning:
java.sql.ResultSet resInsert; and has been worked with all the way in the <% code %>
and then suddenly at the end, it occurs to the compiler, that the variable is not initialized?

Such things make me very angry. awfully angry.

WHY ?


P.S. I`ve just removed this line and it goes on with the next one saying:
The local variable stmt may not have been initialized
107: if(stmt !=null){

and so on with all three of them


WHY pleas tell me what is wrong here??

Recommended Answers

All 9 Replies

Well, what would happen if the connection (or something else) failed before the code ever got to the point where the ResultSets could be created? The would be "uninitialised", right? Since they have never had a value assigned to them, right? So, on the lines where you declare them, why don't you try assigning a value to them as well? Such as null (for a quick and dirty fix), maybe?

Local variables are not assigned any kind of value, at all, when they are defined. So, you must make certain that, regardless of the flow through the code, that the variable will have had a value assigned to it before you attempt to access that value.

Class and instance variables are automatically assigned a value when declared, but local variables are not.

P.S. get rid of all these scriplets in your JSPs, in the first place. This was the way of doing things in JEE version 1, we are now on JEE Version 6 and scriptlets have long been deprecated. They are a maintenance nightmare, and simply not scaleable, in the least (among other possible/probable problems).

Ok, let me show you my whole code, because it appears to not function at all.
This should be a simple login action, where username and password are validated. nothing more, nothing less.

<%@ page import="java.sql.*" %> 
<%@ page import="java.io.*" %> 
<%@ page language="java" session="true" %>
 <%@ include file="basic_einloggen.jsp" %>
 

<%
		//Getting the text values from create login page
		String kuname   = request.getParameter("kuname");
		String passwort  = request.getParameter("passwort");
   
        String loginid  = kuname.trim();
	    String loginpin = passwort.trim();
        
	    
       Connection conn;
       Statement stmt;
       ResultSet resDisplay;
       ResultSet resInsert;
        
		
        String id  =  null;
	String pin =  null;
		
	try{

	out.println("TRYING Connection......");
			
	Class.forName("org.postgresql.Driver");        
	conn = DriverManager.getConnection(dburl);     
           

	out.println("Making Query......);

	String sql = "SELECT * FROM Login WHERE userID='"+loginid+"'";
	stmt = conn.createStatement();
	resInsert = stmt.executeQuery(sql);
				
	out.println("PROCESSING RESULTS......");
				
		while( resInsert.next() )
		{
			out.println("READING DATA......");
			
			id =  resInsert.getString("loginid");
			pin = resInsert.getString("passwort");
			
		    /// pin = resInsert.getString("passwort");
			// id  = resInsert.getString("loginid");
			  
			// pin.trim();
			 //id.trim();
			 
			 out.println(id);
			 out.println(pin);
			 
			 //i++;
		}

		
	
	 	
	 	if(loginid.equals(id) && loginpin.equals(pin))
		{
	         out.println("login successful");
		 %>
		 <jsp:forward page="/I_am_anArtist.htm"></jsp:forward>
		 <% 
        }
		else
		{
           out.println("incorrect username/password combination");
           %>
  		 <jsp:forward page="/login.html"></jsp:forward>
  		 <% 
		}
	 	
		}
        
        
    	catch(Exception e){e.printStackTrace();}
    	
    	finally{
            try{
               	
            	if(resInsert !=null){
            		stmt.close();
            	}
                
                if(stmt !=null){
                	stmt.close();
                }
                   
                if(conn != null){
                    conn.close();
                }
            }
            catch(Exception e2){
                out.println("Unable to close connection: "+e2.getMessage());
            }
         }
%>

The reason why I`ve used out.printlns so much is beacuse I wanted to follow where the code breaks.
And I only get this : "TRYING Connection......Making Query......

And to get this message at all, I have to completely remove the finally-block, because of the error I depicted above.


And yes, I`m getting crazy. It can`t be that hard.

?!

Thank you Masijade, your suggestion was the solution for the finally problem. Nevertheless, the rest also doesnt work, although it makes absolut sense to me.

Then provide the exception you're getting.

Then provide the exception you're getting.

no exeptions occur. I start the login.html, fill in the fields, click submit and I get the blan page with this one line :

" Trying Connection......Making Query"


no "processing results", no "getting data", no redirections, no nothing.

As if the compiler stops at the line, before/after the query has been made.

You can check this article (<URL SNIPPED>HOW TO: SQL in JAVA) for the best practices on how to connect to SQLServer from Java. It also describes how pass SQL queries, pass parameters, call stored procedures etc.

no exeptions occur. I start the login.html, fill in the fields, click submit and I get the blan page with this one line :

" Trying Connection......Making Query"


no "processing results", no "getting data", no redirections, no nothing.

As if the compiler stops at the line, before/after the query has been made.

Because you are getting an exception, but you are catching that exception and printing the stack trace to STDERR. Like I said search your server logfiles.

You can check this article (<URL SNIPPED>HOW TO: SQL in JAVA) for the best practices on how to connect to SQLServer from Java. It also describes how pass SQL queries, pass parameters, call stored procedures etc.

Not really anything to do with it. There's not much help for it anyway, when the OP insists on doing this with scriptlets which the OP has already been warned about in no uncertain terms.

dude, where is the "dburl" string defined? Moreover as the statement object and other object variables you are declaring are local variables, so you should initialise them with null as soon as you declare them or the compiler will throw error at compile time. These are the language fundamentals you should keep in mind. Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.