-1

Hi i wrote this java webservice but i get a NullPointerException out of executeFetchQuery? What is causing this NullPointer. I thought the connection could not be made because of this error: Error 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;
    }

}

I think you need this

 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)
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Dragster
  • 13
  • 1
  • 7
  • 6
    If you've done even a little searching on solving a NullPointerException (NPE), you'll know that the most important bit of information that we need is the exception's associated stacktrace and some identification of the line that causes it, something that the stacktrace will tell you, and unfortunately neither of which you've posted here with your question. Please fix this so that we can help you. – Hovercraft Full Of Eels Apr 19 '15 at 01:32
  • Shoot you're not showing the correct class -- your NPE is being thrown at line 35 of the DishesSoap.java class: `DishesSoap.java:35` – Hovercraft Full Of Eels Apr 19 '15 at 02:00

2 Answers2

0

Sorry guys,

i was possitive that my mistake was in DbConnect. But here is my 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
    }
Dragster
  • 13
  • 1
  • 7
0
private DbConnect dishesDao;

public ArrayList<Dishes> getDishes() {
    //System.out.println("DishesSOAP: " + dishesDao.getDishes());
    return dishesDao.getDishes();  // line 35
}

dishesDao is null as it is never set, and yet here you try to call a method on it:

dishesDao.getDishes();

Don't do that. Assign a viable reference to dishesDao before trying to call methods on it.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373