Results 1 to 7 of 7
  1. #1
    scottbak is offline Member
    Join Date
    Apr 2010
    Posts
    1
    Rep Power
    0

    Default JDBC SQL Service Error: java.lang.NullPointerException

    Here is my code, what needs to change for this to work? I am using SQL 2008 and JDBC 2.0 drivers from Microsoft. when I run the stored procedure it pulls results just fine. (see code below for sp and jsp)
    Stored Procedure code:
    Create Table #Reservations (Checkin datetime not null,
    checkout datetime not null)

    Insert Into #Reservations (Checkin,Checkout)
    select distinct checkin,checkout from bookings

    Create Table #myTable ( ag_year int,
    ag_month smallint,
    ag_day varchar(10),
    ag_message varchar(10),
    ag_bgcolor varchar(10),
    ag_action varchar(10),
    ag_boxit varchar(10),
    ag_fgcolor varchar(10),
    ag_bgimg varchar(10),
    ag_html varchar(10),
    resid int )

    Declare @Day int,@ID int ,@Start datetime ,@END datetime

    DECLARE mydates CURSOR
    READ_ONLY
    FOR
    Select Checkin,Checkout
    FROM #Reservations

    OPEN mydates

    FETCH NEXT FROM mydates INTO @start,@end

    WHILE (@@FETCH_STATUS <> -1)
    BEGIN
    IF (@@FETCH_STATUS <> -2)
    BEGIN
    Select @Day = 0
    While dateadd(dd,@day,@start) <= @end
    Begin
    insert #myTable (ag_year, ag_month, ag_day, ag_message, ag_bgcolor,
    ag_action, ag_boxit, ag_fgcolor, ag_bgimg, ag_html,resid)

    Select year(dateadd(dd, @day, @start)),month(dateadd(dd, @day,@start)),
    day(dateadd(dd, @day, @start)), 'Reserved', 'red', null, 'true', null, null,
    null,@ID
    Select @day = @day + 1
    END

    END

    FETCH NEXT FROM mydates INTO @Start,@END
    END

    CLOSE mydates
    DEALLOCATE mydates

    select * from #myTable

    drop table #Reservations
    drop table #mytable;
    ----------------------------------------------------------------------------------------------------------
    Web page (file.jsp) code:

    <%@ page contentType="text/javascript" %>
    <% // The above JSP code set the generated page to be recognized as a javascript source. %>
    <% java.sql.Connection conn=null;
    try {
    // Establish database connection
    javax.naming.Context ctx = new javax.naming.InitialContext();
    javax.sql.DataSource ds = (javax.sql.DataSource)ctx.lookup("jdbc/bookingsSQL");
    conn = ds.getConnection();
    // Execute SQL and get a result set
    java.sql.CallableStatement cs = conn.prepareCall("{call BookingInput}");
    cs.execute();

    java.sql.ResultSet rs = cs.getResultSet();

    while (rs.next())

    //// Loop through the result set to generate multiple fAddEvent() functions
    %>

    fAddEvent(<%=rs.getString("ag_year")%>,<%=rs.getSt ring("ag_month")%>,<%=rs.getString("ag_day")%>,
    '<%=rs.getString("ag_message")%>','<%=rs.getString ("ag_action")%>','<%=rs.getString("ag_bgcolor")%>' ,'<%=rs.getString("ag_fgcolor")%>','<%=rs.getStrin g("ag_bgimg")%>',<%=rs.getString("ag_boxit")%>,'<% =rs.getString("ag_html")%>');

    <%
    // Close db connection and error handling
    rs.close();
    cs.close();
    } catch (Exception e) {
    System.out.println("Service Error: "+e);
    } finally {
    if (conn!=null)
    try { conn.close(); } catch (Exception ignore) {}
    }
    %>
    Last edited by scottbak; 04-05-2010 at 03:20 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Where do you get your NPE?
    But I will say, take out all the code from this JSP and use a Servlet and a database access layer...it won't fix your problem, but it will be easier to debug and read.

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

    Default

    First of all ,You should not embed the Database code inside JSP.
    next thing is you should not put empty catch block.Put printStackTrace to trace out the exeception.
    Ramya:cool:

  4. #4
    tom.yu's Avatar
    tom.yu is offline Member
    Join Date
    Apr 2010
    Posts
    1
    Rep Power
    0

    Default

    You are best to debug mode with a check again, should not transfer the parameter is correct

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    Where do you get your NPE?
    As Tolls remind you, can you post your complete error message here to see.

  6. #6
    Bharati is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    plz help....
    im nt understanding What is wrong wid this code.....
    its showing an error no data bt d database which m using z nt empty

    Java Code:
    import java.sql.*;
    
    
    public class  AuthorsInfo 
    
    {
    public static void main(String args[])
    
    
           { 
                   try
    
                  {
    
    
        		  
         		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  
                
            Connection conn =DriverManager.getConnection("jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};Db=C:/Javajdk1.6.0_01/bin/Db.mdb;");
         		
    		
                    Statement stat = conn.createStatement();        
         		ResultSet rs = stat.executeQuery("SELECT* FROM authors WHERE city='Dehradun'");
        		System.out.println("Authorid\tFirst NAme\t Last Name\t City");
    
    		while(rs.next())
    			{
    				String id=rs.getString("au_id");
    				String lname=rs.getString("au_id");
    				String fname=rs.getString("au_fname");
    				String city=rs.getString("city");
    
    
    				if(fname.length()<=7)
    				System.out.println(fname+"\t\t");
    
    				else
    
    					System.out.println(fname+"\t");
    
    					if (lname.length()<=7)
    				System.out.println(fname+"\t\t");
    
    				else
    
    
    					System.out.println(fname+"\t");
    
    					System.out.println(city);
    
    			}
    				conn.close();
    		
    			}
    					catch (Exception ex)
    				{
    					System.out.println("Error occured");
    					System.out.println("Error:"+ex);
    
    				}
      }
    	}
    Last edited by Eranga; 04-12-2010 at 08:05 PM. Reason: code tags added

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    First of all, please do not post your questions on others threads, if your question is not relevant with the original. Please start a new thread in appropriate sub-forum next time.

    And also please use code tags when you posting again.

    Regarding your question, what happen? Did you comes with any errors?

Similar Threads

  1. java.lang.NullPointerException Error
    By Manfizy in forum NetBeans
    Replies: 3
    Last Post: 02-24-2011, 07:27 AM
  2. Please solve my error occured java.lang.NullPointerException
    By Viruthagiri in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-24-2010, 01:08 PM
  3. Please solve my java.lang.NullPointerException error.?
    By Viruthagiri in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-23-2010, 10:30 PM
  4. Replies: 1
    Last Post: 12-05-2009, 08:28 AM
  5. Replies: 2
    Last Post: 07-02-2007, 03:29 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
  •