java.lang.NumberFormatException: null

[nobr]I have a calendar program. As the year to be entered was a textfield, i converted it to a select statement. so that the user can select the year from the selection box. But i am getting a NumberFormatException.

This is my code: CalendarPage.jsp

<%@ page import="java.util.List"%>

<%@page import="java.sql.*, java.util.*, java.text.*" %>

<%@page import="java.sql.Connection, java.sql.DriverManager,java.sql.ResultSet,java.sql.SQLException" %>

<html>

<head>

<title>Print a month page.</title>

<meta name="version">

<script>

var lastId ="";

var curId ="";

function selectDate(f)

{

if(document.forms[0].lastDateId !=null)

{

lastId = document.forms[0].lastDateId.value;

}

if(lastId != document.forms[0].todayId.value)

{

if(document.getElementById(lastId).title =="")

{

document.getElementById(lastId).style.backgroundColor='white';

}

}

if(f.id != document.forms[0].todayId.value)

{

document.getElementById(f.id).style.backgroundColor='blue';

}

document.forms[0].lastDateId.value=f.id;

curId = f.id;

document.getElementById("descr").style.visibility='visible';

document.forms[0].descText.value = f.title;

}

function setTitle()

{

document.getElementById(curId).title=document.forms[0].descText.value;

if(document.forms[0].descText.value =="" && curId != document.forms[0].todayId.value)

{

document.getElementById(curId).style.backgroundColor='white';

}

else

{

if(document.forms[0].list.value =="" )

{

document.forms[0].list.value = curId+":"+document.getElementById(curId).title;

}

else

{

list = replaceList(document.forms[0].list.value,curId);

list = list+","+curId+":"+document.getElementById(curId).title;

document.forms[0].list.value = list;

}

}

document.getElementById("descr").style.visibility='hidden';

}

function replaceList(list,id)

{

arr = list.split(",");

newlist ="";

for(i in arr)

{

if(arr[i].match(id) ==null)

{

if(newlist =="")

{

newlist = arr[i];

}

else

{

newlist +=","+arr[i]+",";

}

}

}

return newlist

}

function showCalendar()

{

location.href="CalendarPage.jsp?year="+document.forms[0].year.value;

}

</script>

</head>

<body bgcolor="#c6d9e4">

<%

Connection con =null;

response.setContentType("text/html");

try

{

String driverName ="com.mysql.jdbc.Driver";

Class.forName(driverName);

String serverName =" ";

String mydatabase =" ";

String url ="jdbc:mysql://" + serverName +"/" + mydatabase;

String username =" ";

String password =" ";

con = DriverManager.getConnection(url, username, password);

Statement stmt =null;

ResultSet rset =null;

PreparedStatement PREPstmt1;

stmt = con.createStatement();

String year = request.getParameter("year");

String date="",descr="";

String list = request.getParameter("list");

if(list !=null)

{

String dateDescrList[] = list.split(",");

for(int i=0;i<dateDescrList.length;i++)

{

String listObj = dateDescrList[i];

if(!listObj.equals("") || !listObj.trim().equals(""))

{

date= year+"-"+listObj.substring(0,listObj.indexOf(":"));

descr = listObj.substring(listObj.indexOf(":")+1);

}

String query ="insert into Holiday(HolidayDate, Description) VALUES (?, ?)";

PREPstmt1=con.prepareStatement(query);

PREPstmt1.setString(1,date);

PREPstmt1.setString(2,descr);

PREPstmt1.executeUpdate();

}

}

}

catch(Exception e)

{

System.err.println("Exception: " + e.getMessage());

}

finally

{

try

{

if(con !=null)

con.close();

}

catch(SQLException e)

{

}

}

%>

<%

boolean yyok =false;

int yy = 0, mm = 0,ALflag=0;

String yyString = request.getParameter("year");

if (yyString !=null && yyString.length() > 0)

{

try

{

yy = Integer.parseInt(yyString);

yyok =true;

}

catch (NumberFormatException e)

{

out.println("Year " + yyString +" invalid");

}

}

Calendar c = Calendar.getInstance( );

if (!yyok)yy = c.get(Calendar.YEAR);

mm = c.get(Calendar.MONTH);

String todayId ="";

%>

<form method=get action="CalendarPage.jsp">

Enter Year : <select name="year">

<%

for(int i=2000;i<=2015;i++)

{

if(i==Integer.parseInt(yyString))

{

%>

<OPTION SELECTED= <%=i%> > <%=i%> </option>

<%

}

else

{

%>

<OPTION VALUE= <%=i%> > <%=i%> </option>

%>

<%

}

}

%>

</select>

<input type="button" value="Display" onClick="showCalendar()">

<br><br>

<div id="descr" style="visibility:hidden">Enter Description : <input type="text" name="descText" value=""> <input type="button" value="ok" onClick="setTitle()"></div>

<br>

<%

String driverName ="com.mysql.jdbc.Driver";

Class.forName(driverName);

String serverName =" ";

String mydatabase =" ";

String url ="jdbc:mysql://" + serverName +"/" + mydatabase;

String username ="";

String password ="";

con = DriverManager.getConnection(url, username, password);

Statement stmt =null;

ResultSet rset =null;

PreparedStatement PREPstmt1;

stmt = con.createStatement();

String year = request.getParameter("year");

rset=stmt.executeQuery("select YEAR(HolidayDate) from Holiday where YEAR(HolidayDate)='"+year+"'");

int cnt=0;

while(rset.next())

{

cnt++;

if(cnt>1)

break;

}

if(cnt>0)

{

stmt.executeUpdate("delete from Holiday");

}

%>

<%!

String[] months ={

"January","February","March",

"April","May","June",

"July","August","September",

"October","November","December"

};

int dom[] ={

31, 28, 31, 30,

31, 30, 31, 31,

30, 31, 30, 31

};

%>

<%

int leadGap = 0;

%>

<table border="0" width="100%" align="Left">

<%

GregorianCalendar calendar =null;

for(int j=0;j<12;j++)

{

calendar =new GregorianCalendar(yy, j, 1);

if(j==0 || j%3==0)

{

%>

<tr>

<%

}

%>

<td halign="top" >

<table border="1" width="33%" align="Left"><tr align="right">

<th colspan=7>

<%= months[j] %>

<%= yy %>

</th>

</tr>

<tr>

<td>Sun<td>Mon<td>Tue<td>Wed<td>Thu<td>Fri<td>Sat

</tr>

<%

leadGap = calendar.get(Calendar.DAY_OF_WEEK)-1;

int daysInMonth = dom[j];

if (calendar.isLeapYear(calendar.get(Calendar.YEAR)) && j == 1)

++daysInMonth;

out.println("<tr>");

out.println(" ");

for (int i = 0; i < leadGap; i++)

{

out.print("<td> </td>");

}

for (int i = 1; i <= daysInMonth; i++)

{

out.println("<td>");

int dayOfWeek = (leadGap + i) % 7;

GregorianCalendar today =new GregorianCalendar();

if(today.get(Calendar.DATE) == i && today.get(Calendar.MONTH) == j)

{

todayId = i+"-"+(j+1);

%>

<div id="<%=((j+1)+"-"+i)%>" onClick="selectDate(this)" style="background-color:#cccc00" title="" ><a href="#" style="text-decoration:none;color:808080"><%=i %> </a></div>

<%

}

else

{

if(dayOfWeek <= 1)

{

String str = year+"-"+i+"-"+(j+1);

if(dayOfWeek == 0)

{

String query ="insert into Holiday VALUES ('" + str +"','Saturday','Y')";

stmt=con.createStatement();

stmt.executeUpdate(query);

}

else

{

String query ="insert into Holiday VALUES ('" + str +"','Sunday','Y')";

stmt=con.createStatement();

stmt.executeUpdate(query);

}

%>

<div id="<%=((j+1)+"-"+i)%>" onClick="selectDate(this)" style="background-color:#cccccc" title=""> <a href="#" style="text-decoration:none;color:808080"><%=i %> </a></div>

<%

}

else

{

%>

<div id="<%=((j+1)+"-"+i)%>" onClick="selectDate(this)" style="background-color:white" title="" ><a href="#" style="text-decoration:none;color:808080"><%=i %> </a></div>

<%

}

}

out.println("</td>");

if(calendar.getActualMaximum(Calendar.DAY_OF_MONTH) == i)

{

out.println("</td></tr></table>");

}

elseif ((leadGap + i) % 7 == 0)

{

out.println("</tr>");

out.println("<tr>");

}

}

%>

</td>

<%

if((j+1)%3==0)

{

%>

</tr>

<%

}

}

%>

<input type="hidden" name="lastDateId" value="<%=todayId%>">

<input type="hidden" name="todayId" value="<%=todayId%>">

<input type="hidden" name="list" value="">

<input type="submit" value="Submit">

</form>

</body>

</table>

</html>

Here is the error:

org.apache.jasper.JasperException: null

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:370)

org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)

org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)

javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

root cause

java.lang.NumberFormatException: null

java.lang.Integer.parseInt(Unknown Source)

java.lang.Integer.parseInt(Unknown Source)

org.apache.jsp.color.CalendarPage_jsp._jspService(org.apache.jsp.color.CalendarPage_jsp:262)

org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)

javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)

org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)

org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)

javax.servlet.http.HttpServlet.service(HttpServlet.java:802)[/nobr]

[19748 byte] By [venkateshpa] at [2007-10-2 15:24:39]
# 1

The error is reporting a null value.

Check that String yyString = request.getParameter("year"); is actually getting a value assigned.

if yyString is empty and you try and parse it then its going to throw an error.

GIven the fact that you were previously loadng 0000-00-00 into your database there is a very good chance this is the case.

angrycata at 2007-7-13 14:38:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
But before executing the code, i had deleted all the data in the database. But still am getting the same null error.
venkateshpa at 2007-7-13 14:38:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

This problem is nothing to do with the databse

String year = request.getParameter("year");

Are you sure you are passing the year to your application from the javaScript?

Test it with

out.println(year);

Make sure this variable (year) is properly populated.

If it is not it is a problem with the code CALLING this page, perhaps

location.href="CalendarPage.jsp?year="+document.forms[0].year.value;

or similar

angrycata at 2007-7-13 14:38:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Sorry, it was the lineString yyString = request.getParameter("year");that is the problem, you still need to check that the year is being passed into the page ok.
angrycata at 2007-7-13 14:38:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
Sorry, it was the lineString yyString = request.getParameter("year");that is the problem, you still need to check that the year is being passed into the page ok.Test it without.println(yyString);
angrycata at 2007-7-13 14:38:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

[nobr]String yyString = String.valueOf(c.get(Calendar.YEAR));

String STyear = request.getParameter("year");

if(STyear != null)

{

yyString=STyear;

}

I changed the code like this. It worked out.

But 0000-00-00 is getting into the database rather than the exact date of the saturdays and sundays.

In the database, i am getting the first 2 dates like,2006-01-01 sunday ,2006-01-07 as saturday. All the other dates in the year are getting in as 0000-00-00. what could be the error.

Here is the code :

<%@ page import="java.util.List"%>

<%@page import="java.sql.*, java.util.*, java.text.*" %>

<%@page import="java.sql.Connection, java.sql.DriverManager,java.sql.ResultSet,java.sql.SQLException" %>

<html>

<head>

<title>Print a month page.</title>

<meta name="version">

<script>

var lastId = "";

var curId = "";

function selectDate(f)

{

if(document.forms[0].lastDateId != null)

{

lastId = document.forms[0].lastDateId.value;

}

if(lastId != document.forms[0].todayId.value)

{

if(document.getElementById(lastId).title == "")

{

document.getElementById(lastId).style.backgroundColor='white';

}

}

if(f.id != document.forms[0].todayId.value)

{

document.getElementById(f.id).style.backgroundColor='blue';

}

document.forms[0].lastDateId.value=f.id;

curId = f.id;

document.getElementById("descr").style.visibility='visible';

document.forms[0].descText.value = f.title;

}

function setTitle()

{

document.getElementById(curId).title=document.forms[0].descText.value;

if(document.forms[0].descText.value == "" && curId != document.forms[0].todayId.value)

{

document.getElementById(curId).style.backgroundColor='white';

}

else

{

if(document.forms[0].list.value == "" )

{

document.forms[0].list.value = curId+":"+document.getElementById(curId).title;

}

else

{

list = replaceList(document.forms[0].list.value,curId);

list = list+","+curId+":"+document.getElementById(curId).title;

document.forms[0].list.value = list;

}

}

document.getElementById("descr").style.visibility='hidden';

}

function replaceList(list,id)

{

arr = list.split(",");

newlist = "";

for(i in arr)

{

if(arr[i].match(id) == null)

{

if(newlist == "")

{

newlist = arr[i];

}

else

{

newlist += ","+arr[i]+",";

}

}

}

return newlist

}

function showCalendar()

{

location.href="CalendarPage.jsp?year="+document.forms[0].year.value;

}

</script>

</head>

<body bgcolor="#c6d9e4">

<%

Connection con = null;

response.setContentType("text/html");

try

{

String driverName = "com.mysql.jdbc.Driver";

Class.forName(driverName);

String serverName = "192.168.10.5";

String mydatabase = "Trainees";

String url = "jdbc:mysql://" + serverName + "/" + mydatabase;

String username = "venkat";

String password = "venkat";

con = DriverManager.getConnection(url, username, password);

Statement stmt = null;

ResultSet rset = null;

PreparedStatement PREPstmt1;

stmt = con.createStatement();

String year = request.getParameter("year");

String date="",descr="";

String list = request.getParameter("list");

if(list != null)

{

String dateDescrList[] = list.split(",");

for(int i=0;i<dateDescrList.length;i++)

{

String listObj = dateDescrList[i];

if(!listObj.equals("") || !listObj.trim().equals(""))

{

date= year+"-"+listObj.substring(0,listObj.indexOf(":"));

descr = listObj.substring(listObj.indexOf(":")+1);

}

String query = "insert into Holiday(HolidayDate, Description) VALUES (?, ?)";

PREPstmt1=con.prepareStatement(query);

PREPstmt1.setString(1,date);

PREPstmt1.setString(2,descr);

PREPstmt1.executeUpdate();

}

}

}

catch(Exception e)

{

System.err.println("Exception: " + e.getMessage());

}

finally

{

try

{

if(con != null)

con.close();

}

catch(SQLException e)

{

}

}

%>

<%

Calendar c = Calendar.getInstance( );

boolean yyok = false;

int yy = 0, mm = 0,ALflag=0;

String yyString = String.valueOf(c.get(Calendar.YEAR));

String STyear = request.getParameter("year");

if(STyear != null)

{

yyString=STyear;

}

if (yyString != null && yyString.length() > 0)

{

try

{

yy = Integer.parseInt(yyString);

yyok = true;

}

catch (NumberFormatException e)

{

out.println("Year " + yyString + " invalid");

}

}

if (!yyok)yy = c.get(Calendar.YEAR);

mm = c.get(Calendar.MONTH);

String todayId = "";

%>

<form method=get action="CalendarPage.jsp">

Enter Year : <select name="year">

<%

for(int i=2000;i<=2015;i++)

{

if(i==Integer.parseInt(yyString))

{

%>

<OPTION SELECTED= <%=i%> > <%=i%> </option>

<%

}

else

{

%>

<OPTION VALUE= <%=i%> > <%=i%> </option>

%>

<%

}

}

%>

</select>

<input type="button" value="Display" onClick="showCalendar()">

<br><br>

<div id="descr" style="visibility:hidden">Enter Description : <input type="text" name="descText" value=""> <input type="button" value="ok" onClick="setTitle()"></div>

<br>

<%

String driverName = "com.mysql.jdbc.Driver";

Class.forName(driverName);

String serverName = "192.168.10.5";

String mydatabase = "Trainees";

String url = "jdbc:mysql://" + serverName + "/" + mydatabase;

String username = "venkat";

String password = "venkat";

con = DriverManager.getConnection(url, username, password);

Statement stmt = null;

ResultSet rset = null;

PreparedStatement PREPstmt1;

stmt = con.createStatement();

String year = request.getParameter("year");

rset=stmt.executeQuery("select YEAR(HolidayDate) from Holiday where YEAR(HolidayDate)='"+year+"'");

int cnt=0;

while(rset.next())

{

cnt++;

if(cnt>1)

break;

}

if(cnt>0)

{

stmt.executeUpdate("delete from Holiday");

}

%>

<%!

String[] months = {

"January", "February", "March",

"April","May", "June",

"July", "August","September",

"October", "November", "December"

};

int dom[] = {

31, 28, 31, 30,

31, 30, 31, 31,

30, 31, 30, 31

};

%>

<%

int leadGap = 0;

%>

<table border="0" width="100%" align="Left">

<%

GregorianCalendar calendar =null;

for(int j=0;j<12;j++)

{

calendar = new GregorianCalendar(yy, j, 1);

if(j==0 || j%3==0)

{

%>

<tr>

<%

}

%>

<td halign="top" >

<table border="1" width="33%" align="Left"><tr align="right">

<th colspan=7>

<%= months[j] %>

<%= yy %>

</th>

</tr>

<tr>

<td>Sun<td>Mon<td>Tue<td>Wed<td>Thu<td>Fri<td>Sat

</tr>

<%

leadGap = calendar.get(Calendar.DAY_OF_WEEK)-1;

int daysInMonth = dom[j];

if (calendar.isLeapYear(calendar.get(Calendar.YEAR)) && j == 1)

++daysInMonth;

out.println("<tr>");

out.println(" ");

for (int i = 0; i < leadGap; i++)

{

out.print("<td> </td>");

}

for (int i = 1; i <= daysInMonth; i++)

{

out.println("<td>");

int dayOfWeek = (leadGap + i) % 7;

GregorianCalendar today = new GregorianCalendar();

if(today.get(Calendar.DATE) == i && today.get(Calendar.MONTH) == j)

{

todayId = i+"-"+(j+1);

%>

<div id="<%=((j+1)+"-"+i)%>" onClick="selectDate(this)" style="background-color:#cccc00" title="" ><a href="#" style="text-decoration:none;color:808080"><%=i %> </a></div>

<%

}

else

{

if(dayOfWeek <= 1)

{

String str = year+"-"+i+"-"+(j+1);

System.out.println(str);

if(dayOfWeek == 0)

{

String query = "insert into Holiday VALUES ('" + str + "','Saturday','Y')";

stmt=con.createStatement();

stmt.executeUpdate(query);

}

else

{

String query = "insert into Holiday VALUES ('" + str + "','Sunday','Y')";

stmt=con.createStatement();

stmt.executeUpdate(query);

}

%>

<div id="<%=((j+1)+"-"+i)%>" onClick="selectDate(this)" style="background-color:#cccccc" title=""> <a href="#" style="text-decoration:none;color:808080"><%=i %> </a></div>

<%

}

else

{

%>

<div id="<%=((j+1)+"-"+i)%>" onClick="selectDate(this)" style="background-color:white" title="" ><a href="#" style="text-decoration:none;color:808080"><%=i %> </a></div>

<%

}

}

out.println("</td>");

if(calendar.getActualMaximum(Calendar.DAY_OF_MONTH) == i)

{

out.println("</td></tr></table>");

}

else if ((leadGap + i) % 7 == 0)

{

out.println("</tr>");

out.println("<tr>");

}

}

%>

</td>

<%

if((j+1)%3==0)

{

%>

</tr>

<%

}

}

%>

<input type="hidden" name="lastDateId" value="<%=todayId%>">

<input type="hidden" name="todayId" value="<%=todayId%>">

<input type="hidden" name="list" value="">

<input type="submit" value="Submit">

</form>

</body>

</table>

</html>

What i really want is, if the user is entering for the first time, the user selects a year and clicks the display button. If the year is selected for the first time, then all the saturdays and sundays of that year should be inserted in the database.,else if user coming for second time and entering the same year again, the saturdays and sundays should not be inserted in the database.

what shall i do? please help me with the code.[/nobr]

venkateshpa at 2007-7-13 14:38:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

You will have to write the code yourself, this is a unique program....

You have a couple of options, if you have a database table of users you could add a flag indicating whether they have already populated data, and check the status of this flag (a boolean value) prior to update, or prevent duplicate values being entered, i.e. if user and date already exist, dont update.

The first method would run faster, the second would probably be more reliable.

This is no harder to code than what youve already written.

angrycata at 2007-7-13 14:38:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...