十万火急求助:javabean总是抛出NullPointerException异常

PK5261 2004-09-10 03:39:46
一共3个java文件: dbxml.java从xml中获得数据库连接信息 测试通过了的
dbcon.java中创建dbxml.class的实例,并用实例的getXXX方法获得Class、username、password、url信息 并有一个方法coninit 根据获得的连接信息创建一个connection对象返回。
sqlExecute.java中创建dbcon的实例调用coninit方法获得连接, 一个executequery(sql)方法创建statement和resultset 并执行sql后返回resultset。

如果我是在java文件中执行数据库查询的话一点问题都没有 比如test.java
在public static void main(String args[])中创建sqlExecute实例 并调用executequery方法进行查询 便可返回正确的resultset 但是如果在JSP中 无论是用<jsp:useBean id="dbcon" class="datacon.sqlExecute"/>还是直接将datacon.sqlExecute import到页面中然后创建实例
在执行到dbcon.executequery(sql)的时候总是抛出NullPointerException异常。

我找了好久都没找出原因来,后来修改了一下dbcon.java 让它不从dbxml中来获得连接信息而是直接将连接信息写出来,然后在jsp中就不会出现以上问题,请问到底是哪里出错了? 谢谢!!!!!
...全文
205 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
PK5261 2004-09-10
  • 打赏
  • 举报
回复
而且如果是在另一个class文件中创建这个class的实例对xml进行读取 同样的文件名参数“dbcon.xml” 确是默认路径为tomcat的ROOT/ 越来越搞不明白了 请高人指点
PK5261 2004-09-10
  • 打赏
  • 举报
回复
基本找出问题在哪里了 是对xml文件进行读取的时候出的问题以至于返回了null
我以前为了调试方便 直接把eclipse的project输出建在tomcat的web-inf/classes/下面
tomcat在D盘 eclipse在F盘 对xml进行读取的时候提示在f:/eclipse下找不到该文件
这里我就想不通了 读取xml的class文件明明是在D盘 它怎么会默认路径是f:/eclipse呢
开始我以为是project的问题 把这个工程删除掉了 但是它的默认路径怎么还是f:/eclipse呢? 我的环境变量没有一个是设置的f:/eclipse
PK5261 2004-09-10
  • 打赏
  • 举报
回复
sqlExcute.java:

package datacon;
import java.sql.*;

public class sqlExcute {
Connection con;
Statement stmt;
ResultSet rs;
datacon dc=new datacon();
public sqlExcute()
{

try{
con=dc.coninit();
}
catch(Exception e)
{

}
}
public ResultSet executeQuerys(String sql)
{
try
{
stmt=con.createStatement();
rs=stmt.executeQuery(sql);
return rs;
}
catch(SQLException e)
{

}

return rs;
}

public int executeUpdates(String sql) throws Exception
{int count=-1;
try
{
stmt=con.createStatement();
count=stmt.executeUpdate(sql);
}
catch(SQLException e)
{
throw new Exception("executeQuerry Exception");
}

return count;
}

public void conclose() throws Exception
{
try{
dc.conclose();
}
catch(Exception e)
{
throw e;
}
}

}


dbcon.xml:

<?xml version="1.0" encoding="gb2312"?>
<dbcopy>
<source>
<class>sun.jdbc.odbc.JdbcOdbcDriver</class>

<url>jdbc:odbc:office</url>

<user>sa</user>

<password>2482753</password>
</source>
</dbcopy>



JSP代码
<jsp:useBean id="condata" class="datacon.sqlExcute"/>
<%
try{
condata.executequerys(sql);
}
catch(Exception e)
{
out.println(e);
}
就会显示nullpointerexception

如果在test.java中
package datacon;
import java.sql.*;
/**
* @author administrator
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public class test {

public static void main(String[] args) {
sqlExcute se=new sqlExcute();
ResultSet rs=null;
try{

rs=se.executeQuerys("select count(*)+35 as num from bbsboard");
rs.next();
System.out.println(rs.getInt("num"));

se.conclose();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
就会正确执行 一点错都没有
%>
SJLPY 2004-09-10
  • 打赏
  • 举报
回复
public dbxml(String filename)

{

xmlfile=filename;

}


把 这里的xmlfile改成this.xmlfile试试,注意下边用到xmlfile的地方也要改成this.xmlfile,不知是不是这的原因
PK5261 2004-09-10
  • 打赏
  • 举报
回复
datacon.java:

package datacon;
import java.sql.*;
public class datacon {

Connection con;
dbxml xmlparser=new dbxml("dbcon.xml");
ConnPara srcPara=xmlparser.getSource();





public Connection coninit() throws Exception
{

try{

Class.forName(srcPara.getDbclass()).newInstance();
con=DriverManager.getConnection( srcPara.geturl(),srcPara.getusername(),srcPara.getpassword);

}
catch(ClassNotFoundException e)
{
throw new Exception(e.getMessage());
}
catch(SQLException e)
{
throw new Exception(e.getMessage());
}
catch(InstantiationException e)
{
throw new Exception(e.getMessage());
}
catch(IllegalAccessException e)
{
throw new Exception(e.getMessage());
}
return con;
}


public void conclose() throws Exception
{
try
{

con.close();
}
catch(SQLException e)
{
throw new Exception("close Exception");
}
}
public Connection returncon()
{
return con;
}
}
PK5261 2004-09-10
  • 打赏
  • 举报
回复
ConnPara.java:

package datacon;


public class ConnPara {
String dbClass=null;

String url=null;
String username=null;

String password=null;

public ConnPara() { }

public ConnPara(String pdbClass,String purl,String pusername,String ppassword)

{

dbClass=pdbClass;

url=purl;

username=pusername;

password=ppassword;

}

public String getDbClass(){return dbClass;}

public String getUrl(){return url;}

public String getUsername(){return username;}

public String getPassword(){return password;}

public void setDbClass(String str){ dbClass=str;}

public void setUrl(String str){ url=str;}

public void setUsername(String str){username=str;}

public void setPassword(String str){password=str;}

}
PK5261 2004-09-10
  • 打赏
  • 举报
回复
dbxml.java:

package datacon;
import javax.xml.parsers.*;
import org.w3c.dom.*;

import java.io.*;

public class dbxml {
static String xmlfile;

public dbxml(String filename)

{

xmlfile=filename;

}

public static Element loadDocument()

{

try

{
DocumentBuilderFactory dcfactory=DocumentBuilderFactory.newInstance(); //工厂

DocumentBuilder db=dcfactory.newDocumentBuilder(); //文档构造器

Document doc=db.parse(xmlfile);//构造的文档

Element root=doc.getDocumentElement();//根元素

return root;

}
catch( ParserConfigurationException e)
{

}
catch(IOException e)
{

}
// catch(SAXException e)
//{

// }
catch(Exception e)
{

}

return null;

}

public ConnPara getSource()

{

Element root=loadDocument();

if( root==null) { return null; }

NodeList nodes=root.getElementsByTagName("source");

if(nodes.getLength()>0)

{

Node node=nodes.item(0);

String connclass=getChildElementValue(node,"class");

String url=getChildElementValue(node,"url");

String username=getChildElementValue(node,"user");
String password=getChildElementValue(node,"password");

return new ConnPara(connclass,url,username,password);

}

return null;

}


private String getChildElementValue(Node node,String subTagName)
{

String returnString = "";

if(node != null)

{

NodeList children = node.getChildNodes();

for(int innerLoop = 0; innerLoop < children.getLength(); innerLoop++)

{

Node child = children.item(innerLoop);

if(child == null || child.getNodeName() == null || !child.getNodeName().equals(subTagName))

continue;

Node grandChild = child.getFirstChild();

if(grandChild.getNodeValue() != null)

return grandChild.getNodeValue();

}

}

return returnString;

}



}
simon810610 2004-09-10
  • 打赏
  • 举报
回复
up
vampirewp 2004-09-10
  • 打赏
  • 举报
回复
你能不能把相关的代码贴出来,好让大家都来查一查原因
PK5261 2004-09-10
  • 打赏
  • 举报
回复
但是我从另一个class中用相同的方法都可以正确执行啊 但是用到jsp中就不行了
vampirewp 2004-09-10
  • 打赏
  • 举报
回复
应该还是你dbxml.java这个类从XML读取信息有问题,以至于没有创建数据库连接,你把dbxml.java读出来的信息输出来,看是否是正确的?
jiankou2001 2004-09-10
  • 打赏
  • 举报
回复
空指针!!
好好看看你的sql执行的地方!!

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧