HI,

I have a class Customer and Vehicle. Fist a customer needs to be added to the application. when a vehicle is being added the customer id needs to be added also

so i wrote the code as below

@Override
    public void addVehicle( String make, String model, String year, String reg, String vin, String miles,String cid) {

        Vehicle v = new Vehicle();
        //v.setVehicleid(id);
        v.setMake(make);
        v.setModel(model);
        v.setVehicleyear(Integer.parseInt(year));
        v.setRegno(reg);
        v.setVin(vin);
        v.setMiles(Integer.parseInt(miles));
        Customer c;

        try{
            **c = customerService.findCustomer(cid);**
            v.setCusid(c);
        }
            catch(Exception e)
        {
        System.out.printf("error " + e.toString());
        }

        entityManager.persist(v);
    }

the line which is bold throws an null point exceptoin. i searched for the reason it says if there is no record in the db it will throw a nullpointer exception. but i have a record in the db.
what could be the reason ?

@PermitAll
    public Customer findCustomer(String customerID) {
        return entityManager.find(Customer.class, customerID); 
    }

appreciate a reply
thanks

ehm .. yeah, you have a record in your DB. do you have a record with that customerID? add a print statement in your findCustomer method, just to print your customerID, so you can verify it is what you think it is.

Maybe the variable customerService is null at that point - if you posted the exact complete exception messgage then it would probably be clearer, or try printing customerService there to confirm.

yes i have a record in the database, i think the CustomerService is null
y is that

@Stateless
public class VehicleService implements VehicleServiceRemote {

    //@EJB
    CustomerService customerService;

    @PersistenceContext(unitName = "autodealerPU")
    private EntityManager entityManager;

    @Override
    public void addVehicle( String make, String model, String year, String reg, String vin, String miles,String cid) {

        Vehicle v = new Vehicle();
        //v.setVehicleid(id);
        v.setMake(make);
        v.setModel(model);
        v.setVehicleyear(Integer.parseInt(year));
        v.setRegno(reg);
        v.setVin(vin);
        v.setMiles(Integer.parseInt(miles));

        Customer c;

        try{
            c = customerService.findCustomer(cid);
            v.setCusid(c);
        }
        catch(Exception e)
        {
             System.out.printf("error " + e.toString());
        }
        entityManager.persist(v);
    }

attached is a screen shot also

 @PermitAll
    public Customer findCustomer(String customerID) {
        return entityManager.find(Customer.class, customerID); 
    }

Edited 3 Years Ago by anisha.silva: edit

If that's all the code then you declare customerService on line 5, but nowhere do you give it a value, so it's still null (uninitialised).

so you'll need to inject or instantiate it. if it's null, it 's just like an empty box. you can't get anything out of it, untill there's actually something in there.

when i did this
c = entityManager.find(Customer.class,cid);

also return null

the Vehicle Entity class is as follow

@OneToMany(mappedBy = "vehicleid")
    private Collection<Appointment> appointmentCollection;
    @JoinColumn(name = "CUSID", referencedColumnName = "CUSID")
    @ManyToOne
    private Customer cusid;

why is that

appreciate a reply
thanks

Edited 3 Years Ago by anisha.silva: edit

Hey thaks everyone for the help
i found the answer

public void addVehicle( String make, String model, String year, String reg, String vin, String miles,String cid) {

        Vehicle v = new Vehicle();
        //v.setVehicleid(id);
        v.setMake(make);
        v.setModel(model);
        v.setVehicleyear(Integer.parseInt(year));
        v.setRegno(reg);
        v.setVin(vin);
        v.setMiles(Integer.parseInt(miles));        

        Customer c    = entityManager.find(Customer.class, Integer.parseInt(cid));
        v.setCusid(c);

        entityManager.persist(v);
    }

in the find method i had send in the wrong type for the primary key. the primary is an integer, so have to send an interger parameter to the find method

thanks for the help

This question has already been answered. Start a new discussion instead.