您好我写了这个java webservice但是我从executeFetchQuery中得到了一个NullPointerException?造成这个NullPointer的原因是什么。我认为由于此错误无法建立连接:错误executeFetchQuery:java.lang.NullPointerException
package nl.jorisdek.dinnerspinner.business;
import java.sql.*;
import java.util.ArrayList;
import javax.sql.DataSource;
import javax.annotation.Resource;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
/**
*
* @author Joris
*/
@Stateless
@LocalBean
public class DbConnect {
@Resource(name = "jdbc/dinnerspinner")
private DataSource ds;
private Statement st;
private PreparedStatement pst;
public DbConnect(){
}
public void executeModifyQuery(String sql) {
try{
Connection con = ds.getConnection();
con.createStatement().execute(sql);
con.close();
} catch(Exception ex) {
System.out.println("Error: "+ ex);
}
}
public ResultSet executeFetchQuery(String sql) {
ResultSet rs = null;
try {
Connection con = ds.getConnection();
rs = con.createStatement().executeQuery(sql);
con.close();
}
catch(Exception ex) {
System.out.println("Error executeFetchQuery: "+ ex);
}
System.out.println("rs: "+rs);
return rs;
}
public ArrayList<Dishes> getDishes() {
//Map json = new HashMap();
ArrayList<Dishes> dishes = new ArrayList<>();
String sql = "SELECT * FROM dishes";
ResultSet rs = executeFetchQuery(sql);
//ResultSetMetaData metaData = rs.getMetaData();
try {
while(rs.next()){
Dishes dish = new Dishes();
dish.setId(rs.getInt("id"));
dish.setNaam(rs.getString("naam"));
dish.setWinkel(rs.getString("winkel"));
dish.setIngredienten(rs.getString("ingredienten"));
dish.setExtra(rs.getString("extra"));
dish.setKosten(rs.getDouble("kosten"));
dish.setBereiding(rs.getString("bereiding"));
dish.setGezond(rs.getBoolean("gezond"));
dish.setGoedkoop(rs.getBoolean("goedkoop"));
dish.setSimpel(rs.getBoolean("simpel"));
dish.setSnel(rs.getBoolean("snel"));
dishes.add(dish);
}
} catch(Exception ex) {
System.out.println("Error getDishes: "+ ex);
}
return dishes;
}
}
我认为你需要这个
java.lang.NullPointerException
at nl.jorisdek.dinnerspinner.DishesSoap.getDishes(DishesSoap.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
答案 0 :(得分:0)
对不起伙计们,
我认为我的错误在于DbConnect。但这是我的DishesSoap。
package nl.jorisdek.dinnerspinner;
import nl.jorisdek.dinnerspinner.business.*;
import java.util.ArrayList;
import javax.ejb.EJB;
import javax.jws.WebService;
@WebService
public class DishesSoap {
private DbConnect dishesDao;
public ArrayList<Dishes> getDishes() {
//System.out.println("DishesSOAP: " + dishesDao.getDishes());
return dishesDao.getDishes(); // line 35
}
答案 1 :(得分:0)
private DbConnect dishesDao;
public ArrayList<Dishes> getDishes() {
//System.out.println("DishesSOAP: " + dishesDao.getDishes());
return dishesDao.getDishes(); // line 35
}
dishesDao是null,因为它从未设置过,但是在这里你尝试调用一个方法:
dishesDao.getDishes();
不要那样做。在尝试调用方法
之前,为之前的指定一个可行的参考。