wood burning stoves 2.0*
The moose likes Servlets and the fly likes help with jsp 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 » Servlets
Bookmark "help with jsp" Watch "help with jsp" New topic
Author

help with jsp

Jeff Sunder
Ranch Hand

Joined: Jun 26, 2001
Posts: 47
I am writing this jsp and I am getting an error stating:
Error: The variable "projName" may be accessed here before having been definitely assigned a value.

I don't understand why I am getting this error. Can any one shed some light on this? Here is the code:
<%@ page import="java.sql.*"%>
<%
ConnectionPool connPool = null;
String jdbcDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String dbURL = "jdbc dbc:ProjectsDb";
int rowCount = 1;
try {
connPool = new ConnectionPool(jdbcDriver, dbURL, "projects", "projects");
connPool.setInitialConnections(5);
connPool.setIncrementalConnections(5);
connPool.setMaxConnections(15);
connPool.setTestTable("Projects");
connPool.createPool();
}catch (Exception e) {
System.out.println("Error " +e);
}

%>
<html>
<head>
<title>Project Updates</title>
<link rel="stylesheet" type="text/css" href="../css/newProject_input.css">
</head>
<body alink="blue" vlink="blue">
<table width="100%">
<%
String projId = request.getParameter("project");
String projName, projReason, projStatus, projStartDate, projEndDate, projPMFirst;
try{
Connection dbConn = null;
dbConn = connPool.getConnection();
Statement stmt = dbConn.createStatement();
String getAllProjects = "Select * From projects Where projects_id = " + projId;
ResultSet rs = stmt.executeQuery(getAllProjects);
out.println(getAllProjects);
while (rs.next()) {
/*out.println("<tr><td align=\"middle\">"+rowCount+".</td><td><a href=\"ProjectAdj.jsp?project="+rs.getString("projects_id")+"\">"+rs.getString("project_name")+"</a></td><td>"+rs.getString("project_reason")+"</td><t d align=\"middle\">"+rs.getString("status")+"</td>"+
"<td>"+rs.getString("submitter_first_name")+" "+rs.getString("submitter_last_name")+"</td>"+
"<td>"+rs.getString("department")+"</td><td>"+rs.getString("pl_first_name")+" "+rs.getString("pl_last_name")+"</td></tr>");
rowCount++;*/
projName = rs.getString("project_name");
projReason = rs.getString("project_reason");
projStatus = rs.getString("status");
projStartDate = rs.getString("start_date");
projEndDate = rs.getString("projected_end_date");
projPMFirst = rs.getString("pm_first_name");
out.print(projName+projReason);
}
}catch (Exception e) {
out.println(e.getMessage());
}
%>
<tr>
<td><input type="text" name="project_name" size="20" value="<%=projName%>"></td>
</tr>
</table>
</body>
</html>
I also ran into an error when I tried to assign the db results into the String variables outside of the while statement. The error was
[Microsoft][ODBC Driver Manager] Invalid cursor state
.
[This message has been edited by Jeff Sunder (edited August 07, 2001).]
anup vachali
Ranch Hand

Joined: Oct 17, 2000
Posts: 54
Jeff,
Does it say where the error occurs?
Anup
Sunetra Saha
Ranch Hand

Joined: Feb 25, 2001
Posts: 77
******Error: The variable "projName" may be accessed here before having been definitely assigned a value.*****
This error is due to the fact that projName, a local variable is assigned a value only while(rs.next()). If the result set does not return any rows - you would be using the value of projName before assigning it a value.
Try initializing all those local variables to null when you declare them
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

To answer your first problem, Java doesn't initialise variables for you, so you have assign them explicitly before you try to read from them.
When you set up all of you strings, initialise them to null (no point creating useless objects)
String projName = null;
The only other place projName is assigned is inside a block that may not be executed, therefore when it get read at the bottom it may or may not have had a value assigned to it. If you assign it a null value when you create it, then it definitely has a value.
Dave.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help with jsp
 
Similar Threads
IllegalStateException error
Is there any one who can help me...Please?
Connection Pooling
using and passing a multidimensional array to jsp
reading data from jsp page to another jsp page using request.getParameter