I am trying to write a simple query using JPQ to retrieve all the rows in a table. I believe I have set up my entity up correctly as well as my persistence unit as the tables were successfully created on deployment of the module. My issue is that when I try to retrieve anything from the table it does not work.
The code in my EJB to retrieve the rows looks like this:
@Override
public List<Car> getAllCars() throws Exception
{
return entityManager.createNamedQuery(Car.GET_ALL_QUERY_NAME).getResultList();
}
The code that calls that looks like this:
try
{/*
String[] test = new String[7];
test[0] = "test";
test[1] = "test";
test[2] = "test";
test[3] = "test";
test[4] = "test";
test[5] = "test";
test[6] = "test";
main.getCarScreen().getModel().addRow(test);
System.out.println("test");*/
List<Car> cars = carRepo.getAllCars();
for(Car car : cars)
{//"VIN", "Model Number", "Model Name", "Make", "Thumbnail", "Description", "URL"
String[] data = new String[7];
data[0] = car.getVIN();
data[1] = car.getModelNo();
data[2] = car.getModel();
data[3] = car.getMake();
data[4] = car.getThumbnail();
data[5] = car.getDesc();
data[6] = car.getPrevURL();
main.getCarScreen().getModel().addRow(data);
}
}
catch(Exception ex)
{
main.getCarScreen().outputAddError(ex.getMessage());
System.out.println("failed");
System.out.println(ex.getMessage());
}
I have tested and I know that the carRepo.getAllCars();
is the bit that is failing.
The result of the System.out.println(ex.getMessage());
is just a simple: null
The beginning of my car entity class looks like this:
@Entity
@Table(name = "CAR")
@NamedQueries({@NamedQuery(name = Car.GET_ALL_QUERY_NAME, query = "SELECT c FROM Car AS c")})
public class Car implements Serializable
{
public static final String GET_ALL_QUERY_NAME = "Car.getAll";
I that there is data in the table:
The fact that the exception message is a simple null makes me believe that the code is working but there is nothing to retrieve in the table. When I check the table data however my test data is still there.
Can anyone see why this is failing to retrieve any rows?
[UPDATE]
So I changed the carRepo.getAllCars() to ex.printStackTrace(); and it is indeed a null pointer exception. Stack trace:
java.lang.NullPointerException
at CarSales.CarSales.displayAllCars(CarSales.java:55)
at CarSales.CarSales.run(CarSales.java:35)
at CarSales.Main.main(Main.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.glassfish.appclient.client.acc.AppClientContainer.launch(AppClientContainer.java:446)
at org.glassfish.appclient.client.AppClientFacade.launch(AppClientFacade.java:183)
at org.glassfish.appclient.client.AppClientGroupFacade.main(AppClientGroupFacade.java:65)
CarSales.java:55 is the line
List<Car> cars = carRepo.getAllCars()
I am pretty new to JPQL and I have no idea what could be causing this.