how to handle null values

null error when a null value is retrived from database  in jsp
cakurienAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

avinthmCommented:
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");
else
   System.out.println("do my operations");



cheers
0
Sys_ProgCommented:
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

Example
<%
    String yourVariable = someObject.getValue() ;
    if (yourVariable == null){
           //do null processing
    }else{
          //do not null processing
    }
%>

Cheers!
0
mnshah9Commented:
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 ~


0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

cakurienAuthor Commented:

<%@ 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 {
                        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                        con = DriverManager.getConnection("jdbc:odbc:LocalServer");
                        st = con.createStatement();
                        rs=st.executeQuery("select max(id)te from store");
                        
                        rs.next();
                        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 +"'");      
                        //out.println(rs.getString("bname"));
                        rs.next();
                  } catch (SQLException e) {
                        out.println("ooooooo" + e);
                        e.printStackTrace();
                  }
                  
            %>

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

  </head>
 
  <body>
            <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>
             
            <TR>
                        <TH>Id</TH>
                        <TH>Book Name</TH>
                        <TH>Price</TH>
                        <TH>Quantity</TH>
                        <TH>Total</TH>
                  </TR>
                  <TR>
                        <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
                         
                               <TD><%=tt%></TD>
                                     

                        
                        <TD></TD>
                  </TR>
                  
            </TABLE>

      </body>
</html>
0
cakurienAuthor Commented:
all these sol dont work
0
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
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 )%>                  
<TD>qty:<%=rs.getString("qty")%></TD>

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

Get back if there is some problem.

Bye.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mujtaba_Alam_KhanCommented:
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..
IMPORTANT NOTE: ALWAYS CLOSE THE QUERY... rs.close();
 } catch (SQLException e) {
                    out.println("ooooooo" + e);
                    e.printStackTrace();
}


Table:

<%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


0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.