java - javax.sql.datasource getconnection returns null -


can please show me how fix code below not throw error?

the following line of code giving me null pointer exception:

return datasource.getconnection(); 

note datasource instance of javax.sql.datasource specified in web.xml, , works fine when called other code.

here actual method in dataaccessobject.java null pointer occurring:

protected static connection getconnection(){     try {      return datasource.getconnection();  //   } catch (sqlexception e) {      throw new runtimeexception(e);   } } 

the preceding method being called line of code:

connection = getconnection();   

which located in following method in class called coursesummarydao follows:

public list<coursesummary> findall(long sid) {     linkedlist<coursesummary> coursesummaries = new linkedlist<coursesummary>();     resultset rs = null;     preparedstatement statement = null;     connection connection = null;     try {         connection = getconnection(); //         string sql = "select * coursetotals spid=?";         statement = connection.preparestatement(sql);         statement.setlong(1, sid);         rs = statement.executequery();         //for every row, call read method extract column          //values , place them in coursesummary instance         while (rs.next()) {             coursesummary coursesummary = read("findall", rs);             coursesummaries.add(coursesummary);         }         return coursesummaries;      }catch (sqlexception e) {         throw new runtimeexception(e);      }       {         close(rs, statement, connection);     }  }   

to recreate simply, created following testcoursesummaries class:

public class testcoursesummaries {     public static void main(string[] args) {     long id = new long(1002);     coursesummarydao mycsdao = new coursesummarydao();     list<coursesummary> coursesummaries = mycsdao.findall(id);     for(int = 0;i<coursesummaries.size();i++){     system.out.println("type, numunits are: "+coursesummaries.get(i).getcoursetype()+","+coursesummaries.get(i).getnumunits());     } } }   

edit:

to address justdanyul's question, enclosing code calls in application, , underlying dataaccessobject code extended 2 dao objects in calling code:

here code in application triggers error. see there 2 classes each extended dataaccessobject. perhaps conflicting each other, causing second 1 not database connection?

protected void doget(httpservletrequest req, httpservletresponse resp)  throws servletexception, ioexception {      string idstring = req.getparameter("id");    long id = new long(idstring);    thisobj thisobj = new thisdao().find(id);    req.setattribute("thisobj", thisobj);    thoseobjdao mythosedao = new thoseobjdao();    list<thoseobj> thoseobjects = mythoseobjdao.findall(id);    req.setattribute("thoseobjects", thoseobjects);    jsp.forward(req, resp); 

}

and here code dataaccessobject class extended 2 dao classes in calling code:

public class dataaccessobject { private static datasource datasource; private static object idlock = new object();  public static void setdatasource(datasource datasource) {    dataaccessobject.datasource = datasource; }  protected static connection getconnection() {    try {return datasource.getconnection();}    catch (sqlexception e) {throw new runtimeexception(e);} }  protected static void close(statement statement, connection connection) {    close(null, statement, connection); }  protected static void close(resultset rs, statement statement, connection connection) {    try {       if (rs != null) rs.close();       if (statement != null) statement.close();       if (connection != null) connection.close();    } catch (sqlexception e) {throw new runtimeexception(e);} }  protected static long getuniqueid() {    resultset rs = null;    preparedstatement statement = null;    connection connection = null;    try {       connection = getconnection();       synchronized (idlock) {          statement = connection.preparestatement("select next_value sequence");          rs = statement.executequery();          rs.first();          long id = rs.getlong(1);          statement.close();          statement = connection.preparestatement("update sequence set next_value = ?");          statement.setlong(1, id + 1);          statement.executeupdate();          statement.close();          return new long(id);       }    }    catch (sqlexception e) {throw new runtimeexception(e);}    finally{close(rs, statement, connection);} } }   

the data source created in web.xml, follows:

<resource-ref>     <description>datasource</description>     <res-ref-name>datasource</res-ref-name>     <res-type>javax.sql.datasource</res-type>     <res-auth>container</res-auth> </resource-ref> 

i suspect code "runs fine" in, code running in application server. example posting, runs static void main() method, wont resources has been defined in web.xml.

i guessing use jdni setup initial datasource. , using like

@resource(name="jdbc/mydb") private datasource datasource; 

to set connection. right?

edit:

after seeing code, seems data source newer initialised @ all. putting element web.xml not alone. need configure datasource, know, specify driver, username, password, uri etc etc etc.

i'm guessing find() method of dao works, isn't using datasource. have shown far, doesn't insigunate have initialised datasource @ all.

just give idea, liked tutorial on how tomcat , jdni. (or better, use spring-jdbc).

http://www.mkyong.com/tomcat/how-to-configure-mysql-datasource-in-tomcat-6/


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -