[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

how to handle null values

Posted on 2006-04-24
7
Medium Priority
?
1,703 Views
Last Modified: 2012-05-05
null error when a null value is retrived from database  in jsp
0
Comment
Question by:cakurien
7 Comments
 
LVL 6

Expert Comment

by:avinthm
ID: 16523467
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
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 16523606
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
 

Expert Comment

by:mnshah9
ID: 16524382
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
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

 

Author Comment

by:cakurien
ID: 16525920

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

Author Comment

by:cakurien
ID: 16525936
all these sol dont work
0
 
LVL 23

Accepted Solution

by:
Siva Prasanna Kumar earned 1000 total points
ID: 16527416
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
 
LVL 6

Expert Comment

by:Mujtaba_Alam_Khan
ID: 16549018
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

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In current releases of Windows 10, MS removed the ability to format a volume with ReFS. That feature is moved to Windows 10 Pro for Workstations. The requisite files exist in Pro, but functionality is locked and/or otherwise unavailable. A $125 upgr…
Take advantage of one of the most useful technologies available - virtualization!
The video provides a quick and easy steps to migrate MBOX file to well known Outlook PST and Office 365. Besides this, it also supports and migrates more than 20 email clients of MBOX which include AppleMail, Opera, Thunderbird and SeaMonkey effortl…
In this video I will demonstrate how to set up Nine, which I now consider the best alternative email app to Touchdown.
Suggested Courses
Course of the Month9 days, 15 hours left to enroll

591 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