Page 2 of 2 FirstFirst 12
Results 21 to 26 of 26
  1. #21
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Sorry, but you;re using different variable names and the like, which often seem to represent different objects and classes.

    Java Code:
    SimpleDateFormat sdfC = new SimpleDateFormat("yyyy-MM-dd");
    String dateString = request.getParameter("date"+Integer.toString(j));
    Date myDate = sdfC.parse(dateString);
    Where is this code?
    Is it in the same spot you are doing this:
    Java Code:
    <c:out value="<%=dateString%>"/>
    ?
    Because we here simply cannot see enough of what you;re doing to say what's wrong.

    dateString in the first code is null...that is what the exception is saying. Java doesn't simply make things up.

    As for the formatting, it also doesn't simply change formats at will. You are doing something different each time. Something we cannot see here.

  2. #22
    UnfairBear is offline Member
    Join Date
    Jul 2011
    Posts
    29
    Rep Power
    0

    Default

    Alrighty. There's quite a lot of code in the first one and quite not a lot of extra code in the second page which is why I didn't post it before. I tried to post the appropriate parts but I guess I could be slipping up somewhere else.

    First page:
    Java Code:
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix = "c"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/sql"  prefix = "sql"%>
    <%@ page import="java.util.Calendar" %>
    <%@ page import="java.util.Date" %>
    <%@ page import="java.text.*" %>
    
    <html>
    
        <sql:setDataSource
           var = "CoolSchool"
           driver = "com.mysql.jdbc.Driver"
           url = "jdbc:mysql://localhost:3306/CoolSchool"
           scope = "session"
    	user = "root"
    	password = ""
        />
    
    <% String year = request.getParameter("year");
    String month = request.getParameter("month");
    String day = request.getParameter("day");
    String thedate = new String(day+"/"+month+"/"+year);
    
    Date myDate = new Date(thedate); 
    
    Calendar thatWeekCalendar = Calendar.getInstance();
    thatWeekCalendar.clear();
    thatWeekCalendar.setFirstDayOfWeek(Calendar.MONDAY);
    thatWeekCalendar.set(Calendar.YEAR, Integer.parseInt(request.getParameter("year")));
    thatWeekCalendar.set(Calendar.MONTH, Integer.parseInt(request.getParameter("month")));
    thatWeekCalendar.set(Calendar.DATE, Integer.parseInt(request.getParameter("day")));
    
    while(thatWeekCalendar.get(Calendar.DAY_OF_WEEK) != thatWeekCalendar.getFirstDayOfWeek())
    thatWeekCalendar.add(Calendar.DATE, -1);
    
    Date theDate = thatWeekCalendar.getTime();
    
    %>
    
    That day is a saturday/sunday. The following is the attendance from the previous week.
    
        <body>
            <table width="100%" border=1>
                <tr><td colspan=2>
                    <%@include file = "header.jsp" %>
                </td></tr>
    
                <tr>    
    		        <td width = "20%" valign=top>
    		            <%@include file = "attendanceNav.jsp" %>
    		        </td>
    		        <td width = "*">
    
    				<!-- get all students in the selected form -->
    				<sql:query var = "studentsInForm" scope = "session" dataSource = "${CoolSchool}">
    					SELECT * FROM student
    					where form = ?;
    					<sql:param value="${param.form}" />
    				</sql:query>
    
    				<%SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy");
    				String mon = sdf.format(theDate);%>
    				<table><tr>
    					<td>Name</td>
    					<td>Mon <c:out value="<%=mon%>"/></td>
    						<%thatWeekCalendar.add(Calendar.DATE, 1);
    						theDate = thatWeekCalendar.getTime();
    						String tue = sdf.format(theDate);%>
    					<td>Tue <c:out value="<%=tue%>"/></td>
    						<%thatWeekCalendar.add(Calendar.DATE, 1);
    						theDate = thatWeekCalendar.getTime();
    						String wed = sdf.format(theDate);%>
    					<td>Wed <c:out value="<%=wed%>"/></td>
    						<%thatWeekCalendar.add(Calendar.DATE, 1);
    						theDate = thatWeekCalendar.getTime();
    						String thu = sdf.format(theDate);%>
    					<td>Thu <c:out value="<%=thu%>"/></td>
    						<%thatWeekCalendar.add(Calendar.DATE, 1);
    						theDate = thatWeekCalendar.getTime();
    						String fri = sdf.format(theDate);%>
    					<td>Fri <c:out value="<%=fri%>"/></td>
    				      </tr>
    
    					<% int j = 0;
    					thatWeekCalendar.add(Calendar.DATE, -4);
    					theDate = thatWeekCalendar.getTime();%>
    
    					<!-- make form that will include a different checkbox for attendance, for each student for each day -->
    					<form method="post" action="enterFormClassAttendanceSuccess.jsp">
    						<c:forEach items="${studentsInForm.rows}" var="rowA">
    
    							<!-- For every student in the form, check if the student takes the selected subject (class) -->
    							<sql:query var = "studentIsInClass" scope = "session" dataSource = "${CoolSchool}">
    								SELECT * FROM student_subjects
    								where student = ?
    								and subject = ?;
    								<sql:param value="${rowA.student_id}" />
    								<sql:param value="${param.subject}" />
    							</sql:query>		
    
    							<!-- If that student is in the class...-->
    							<c:if test="${studentIsInClass.rowCount > 0}">
    
    								<!-- Loop through result set of one student -->
    								<c:forEach items="${studentIsInClass.rows}" var="rowB">
    
    									<tr><td><c:out value="${rowA.first_name} ${rowA.second_name}"/></td>
    
    									<!-- Do the following 5 times for each student, one for each day of the week -->
    									<c:forEach var="i" begin="0" end="4" step="1">
    
    										<!-- set strings for the student and date form element names. 'j' will increment after each checkbox/hidden combo -->
    										<% String presentStudentString = new String("presentStudent"+Integer.toString(j));%>
    										<% String dateString = new String("date"+Integer.toString(j));%>
    										<% String checkboxString = new String("checkbox"+Integer.toString(j));%>
    
    										<!-- check if that student has an attendance record for that day in that class -->
    										<sql:query var = "studentAttendance" scope = "session" dataSource = "${CoolSchool}">
    											SELECT * FROM student_class_register
    											where date = ?
    											and student = ?
    											and subject = ?;
    											<sql:dateParam value="<%=theDate%>" type="DATE" />
    											<sql:param value="${rowA.student_id}" />
    											<sql:param value="${param.subject}" />
    										</sql:query>
    										<!-- in this choose statement, only one checkbox will be made. The checkbox will hold the student's id -->
    										<c:choose>
    											
    											<c:when test="${studentAttendance.rowCount == 0}">
    												<td bgcolor="red"><input type="checkbox" name="<%=checkboxString%>"}"></td>
    											</c:when>
    
    											<c:otherwise>
    												<c:forEach items="${studentAttendance.rows}" var="rowC">
    													<c:choose>
    														<c:when test="${rowC.present == 'y'}">
    															<td><input type="checkbox" name="<%=checkboxString%>"" checked="yes"></td>
    														</c:when>
    														<c:otherwise>
    															<td><input type="checkbox" name="<%=checkboxString%>"}"></td>
    														</c:otherwise>
    													</c:choose>
    												</c:forEach>
    											</c:otherwise>
    										</c:choose>
    										<%SimpleDateFormat sdfB = new SimpleDateFormat("yyyy-MM-dd");
    										String passDate = sdfB.format(theDate);%>
    										<input type=hidden name="<%=presentStudentString%>" value="${rowA.student_id}">
    										<input type=hidden name="<%=dateString%>" value="<%=passDate%>">
    
    										<% j++;
    										thatWeekCalendar.add(Calendar.DATE, 1);
    										theDate = thatWeekCalendar.getTime();%>
    
    
    
    									</c:forEach>
    								    
    									<%thatWeekCalendar.add(Calendar.DATE, -5);
    									theDate = thatWeekCalendar.getTime();%>
    
    								</c:forEach>
    
    							</c:if>
    						</c:forEach>
    						<tr><td><input type="hidden" name="j" value="<%=j%>">
    							<input type="hidden" name="subject" value="${param.subject}">
    							<input type="submit" value="Enter"></td></tr>
    					</form>
    				</table>
    		        </td>
    		    
                </tr>
    
            </table>
        </body>
        
    </html>
    All the dates and student IDs are passed to the next page, which so far is quite short since I'm just trying to figure this bit out:
    Java Code:
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix = "c"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/sql"  prefix = "sql"%>
    <%@ page import="java.util.Date,java.text.*" %>
    
    
    <html>
    
        <sql:setDataSource
           var = "CoolSchool"
           driver = "com.mysql.jdbc.Driver"
           url = "jdbc:mysql://localhost:3306/CoolSchool"
           scope = "session"
    	user = "root"
    	password = ""
        />
    
    
    
        <body>
            <table width="100%" border=1>
                <tr><td colspan=2>
                    <%@include file = "header.jsp" %>
                </td></tr>
    
                <tr>    
    		        <td width = "20%" valign=top>
    		            <%@include file = "attendanceNav.jsp" %>
    		        </td>
    		        <td width = "*">
    
    				<% int k = 0;%>
    				<c:forEach var="i" begin="0" end="${param.j}" step="1">
    
    					<% String student = request.getParameter("presentStudent"+Integer.toString(k));
    
    					
    					String dateString = request.getParameter("date"+Integer.toString(k));	
    					SimpleDateFormat sdfC = new SimpleDateFormat("yyyy-MM-dd");
    					Date myDate = sdfC.parse(dateString); %>
    
    					<c:out value="<%=dateString%>"/></br>
    
    					<%k++;%>
    				</c:forEach>
    		            
    		        </td>
    		    
                </tr>
    
            </table>
        </body>
        
    </html>

  3. #23
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    JSPs are a sod to debug, which is why I despair when I hear of people being taught them before being taught about servlets...almost nothing of that lot up there should be in a JSP. Oh well.

    You'll need to print out the complete set of parameters you are receiving, and also each value of k, then compare them.

    In fact, just spotted it. You increment j in the first page at the end of your loop. So if you have 5 students, student0, student1 etc...j ends up being 5. In the second page your foreach therefore attempts to extract date5...which doesn't exist.

  4. #24
    UnfairBear is offline Member
    Join Date
    Jul 2011
    Posts
    29
    Rep Power
    0

    Default

    Actually, if you look again you'll see that there are three loops that j gets incremented in. The inner one is

    <c:forEach var="i" begin="0" end="4" step="1"> (Which loops through the 5 days in the weeks)

    the next one one out is

    <c:forEach items="${studentIsInClass.rows}" var="rowB"> (Which is just looping through a single record and does't really make a difference to j)

    and the outmost one is

    <c:forEach items="${studentsInForm.rows}" var="rowA"> (Which loops through all the students)

    There are actually 40 date and student elements, I know, I've printed date39 =P

    I'll try out your suggestion about printing everything. Thanks.

  5. #25
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Yes, but you increment at the end of the loop.
    Thus j will always be 1 greater than the maximum index in your "array" of dates...and foreach (I believe) runs to the end value, not to less than the end value.

  6. #26
    UnfairBear is offline Member
    Join Date
    Jul 2011
    Posts
    29
    Rep Power
    0

    Default

    ... *cough*. Yep, I just figured that out. Now I feel very stupid >.<

    Thanks so much for the help!

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Add Date Problem
    By UnfairBear in forum New To Java
    Replies: 2
    Last Post: 08-14-2011, 02:10 PM
  2. Add Date Problem
    By UnfairBear in forum Java Applets
    Replies: 0
    Last Post: 08-14-2011, 10:56 AM
  3. Add Date Problem
    By UnfairBear in forum Java Servlet
    Replies: 0
    Last Post: 08-13-2011, 09:33 PM
  4. sql date problem
    By realosso in forum New To Java
    Replies: 3
    Last Post: 06-04-2010, 04:42 AM
  5. date problem
    By karlkwanny in forum JDBC
    Replies: 1
    Last Post: 07-30-2009, 10:50 AM

Posting Permissions

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