how to handle null values

Posted on 2006-04-24
Last Modified: 2012-05-05
null error when a null value is retrived from database  in jsp
Question by:cakurien
    LVL 6

    Expert Comment

    Hi ,

    you can check the variable for null.
    for ex : if u have an object variable someValue then u can check for null as below

    if(someValue == null)
       System.out.println("null value in my variable");
       System.out.println("do my operations");

    LVL 10

    Expert Comment

    Hi cakurien,

    As you might be knowing, using JSP you can write Java code within HTML
    you can use scriptlet tags to embed Java within HTML

        String yourVariable = someObject.getValue() ;
        if (yourVariable == null){
               //do null processing
              //do not null processing


    Expert Comment

    Hi  cakurien,

    You can write validation, if value is null then display blank string. You are doing some operation on string which you retrieve from database, before processing that string put condition for null value checking


    String s1 = resultSet.getString("")
    if (s1 != null) {
    // do some operation
    } else {
    // do some thing like display blank string

    If you need more clarification on answer please write your question in detail

    ~ Maulik ~


    Author Comment


    <%@ page language="java" import="java.util.*,java.sql.*  " pageEncoding="UTF-8"%>
    <%Statement st = null;
                      ResultSet rs = null, rs1 = null;
                      Connection con = null;
                      String un = null, pw = null;
                      try {
                            con = DriverManager.getConnection("jdbc:odbc:LocalServer");
                            st = con.createStatement();
                            rs=st.executeQuery("select max(id)te from store");
                            int te =rs.getInt("te");
                            //int ss = 1 + rs.getInt(1);
                            int ss = 1 + te ;
                            String ss1 = request.getParameter("C1");
                            Double ss2 = new Double(request.getParameter("T1"));
                            Double ss3 = new Double(request.getParameter("T2"));
                            double ss5= ss2.doubleValue()* ss3.doubleValue();
                            //st.executeUpdate("Insert into hai values('"+ ss + "','" + ss1 + "' )");
                            st.executeUpdate("Insert into store(id,bname,price,qty,amt) values('" + ss + "','" + ss1      + "','" + ss2      + "','" + ss3      + "','" + ss5      +"' )");
                             rs=st.executeQuery("select * from store where id ='" + ss +"'");      
                      } catch (SQLException e) {
                            out.println("ooooooo" + e);

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

                <TABLE  align="center" border="1" cellspacing="" bgcolor="#ffffff" width="100%" height="50px" cellpadding="" title="">
                <TR><TD valign="MIDDLE" align="CENTER" colspan="5" bgcolor="#ffffff" bordercolor="#00ffff">
                <H1>Books u bought</H1></TD></TR>
                            <TH>Book Name</TH>
                            <TD><%= rs.getInt("id")%></TD>
                            <TD><%= rs.getString("bname")%></TD>
                            <TD><%= rs.getInt("price")%></TD>
                            <% String tt= rs.getString("qty");%>
                            <%if(tt!=null)%> here theERROR EMPTY STRING COMES



    Author Comment

    all these sol dont work
    LVL 23

    Accepted Solution

    can you put forward the exception thrown by the JSP so that we can help clearly.

    Because i think that may not be the case any way just do a null hcel for all these as follows

    <%if(rs.getInt("id")!=null )%>
    <TD>id:<%= rs.getInt("id")%></TD>
    <%if(rs.getString("bname")!=null )%>
    <TD>bname:<%= rs.getString("bname")%></TD>
    <%if(rs.getInt("price")!=null )%>
    <TD>price:<%= rs.getInt("price")%></TD>
    <%if(rs.getString("qty")!=null )%>                  

    Now you will clearly know which parameter was null as that would not be displayed.

    Get back if there is some problem.

    LVL 7

    Expert Comment

    the problem is simple you Table is outside the Query Block..

    It should Never be NULL be of the Insert before the Select...

    st.executeUpdate("Insert into store(id,bname,price,qty,amt) values('" + ss + "','" + ss1     + "','" + ss2     + "','" + ss3     + "','" + ss5     +"' )"); //INSERT

    rs=st.executeQuery("select * from store where id ='" + ss +"'");     //SELECT

    Cant be null.. unless 'ss' is null.. then it wouldnt be insert..

    But try:

    the Table in the query Block..

    >> rs=st.executeQuery("select * from store where id ='" + ss +"'");      

    After this...

    or else try:

    <%Statement st = null;
                   ResultSet rs = null, rs1 = null;
                   Connection con = null;
                   String un = null, pw = null;
                   int ID = 0;
                   String Bname = null;
                   int Price = 0;
                   String Qty = null;
                   rs=st.executeQuery("select * from store where id ='" + ss +"'");  
                   ID = rs.getInt("id");
                   Bname = rs.getString("bname");
                   Price = rs.getInt("price");
                   Qty = rs.getString("qty");
    //This is where the Query Block end..
     } catch (SQLException e) {
                        out.println("ooooooo" + e);


    <%if(ID !=null ) {%>
    <TD>id:<%= ID%></TD>
    <TD>bname:<%= Bname%></TD>
    <TD>price:<%= Price%></TD>                
    <TD>qty:<%= Qty }%></TD>

    I believe that if the Id is null the rest are null.. it is silly not to display the rest if the ID is NOT null.. even if Bname or the rest are null..

    Hope this Helps

    -Muj ;-D


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now