jQuery in Action, 2nd edition
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
JavaRanch » Java Forums » Java » Servlets
Bookmark "help with jsp" Watch "help with jsp" New topic

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");
}catch (Exception e) {
System.out.println("Error " +e);

<title>Project Updates</title>
<link rel="stylesheet" type="text/css" href="../css/newProject_input.css">
<body alink="blue" vlink="blue">
<table width="100%">
String projId = request.getParameter("project");
String projName, projReason, projStatus, projStartDate, projEndDate, projPMFirst;
Connection dbConn = null;
dbConn = connPool.getConnection();
Statement stmt = dbConn.createStatement();
String getAllProjects = "Select * From projects Where projects_id = " + projId;
ResultSet rs = stmt.executeQuery(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>");
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");
}catch (Exception e) {
<td><input type="text" name="project_name" size="20" value="<%=projName%>"></td>
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
Does it say where the error occurs?
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

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.
It is sorta covered in the JavaRanch Style Guide.
subject: help with jsp