Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help with jsp

 
Jeff Sunder
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff,
Does it say where the error occurs?
Anup
 
Sunetra Saha
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
******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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic