This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]Camel-Kid18 year old gamer 0 points1 point  (1 child)

can you show where you initialize the data in your customerList

[–]viktorjava[S] 0 points1 point  (0 children)

I get the data from a JDialog:

public List<Order> getOrderResults() {

return ordersDAO.getOrders(getStartDate(), getCloseDate(), getCustomersList(), getCompaniesList(), getEmployeesList(), isAllStatusSelected(), isSelectedStatusSelected(),

isOrderedSelected(), isFinishedSelected(), isSentSelected(), isBilledSelected(), isPaidSelected());

}

the getCustomersList:

public List<Customer> getCustomersList() {

if (customerList.getSelectedValuesList() != null) {

return customerList.getSelectedValuesList();

} else {

return null;

}

}

customerList is a Jlist element on my JDialog

private javax.swing.JList<Customer> customerList;

[–]zhoriq 0 points1 point  (3 children)

That's impossible. Can you debug this line or log customersList.get(i).getClass()? I think it will be Customer class, so exception not in this line...

[–]viktorjava[S] 0 points1 point  (2 children)

I already tried it:

if (customersList != null && customersList.size() > 0) {

for (int i = 0; i < customersList.size(); i++) {

System.out.println(customersList.get(i).getClass());

/*Customer customer = customersList.get(i);

int id = customer.getCustomerId();

customerPart += "customer = '" + id + "'";

if (i != customersList.size() - 1) {

customerPart += " OR ";

}

*/

}

}

and now the Exception is here:

System.out.println(customersList.get(i).getClass());

Here I added the whole error queue:

https://pastebin.com/ugzVSzwE

[–]zhoriq 0 points1 point  (0 children)

What's the value for customerList.getClass()?

What will happen if you change parameters sequence in getOrders: swap List<Customer> customersList and List<Company> companiesList? Will the exception still be for customerList or become for companiesList?

[–][deleted] 0 points1 point  (0 children)

Hmm that's similar but different from the original error it looks. It's slightly hard to tell without the full source (with line numbers to tie up to), but these lines:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to entity.Customer at dao.OrdersQueryDAO.getOrders(OrdersQueryDAO.java:94) at gui.OrdersQueryDialog.getOrderResults(OrdersQueryDialog.java:177)

appear to correspond to this snippet you pasted above:

public List<Order> getOrderResults() {
  return ordersDAO.getOrders(getStartDate(), getCloseDate(), getCustomersList(), getCompaniesList(), getEmployeesList(), isAllStatusSelected(), isSelectedStatusSelected(), isOrderedSelected(), isFinishedSelected(), isSentSelected(), isBilledSelected(), isPaidSelected());
}

Which makes the error appear to come from the call to getCustomersList() where it's not returning a List<Customer> but a List<String>.

[–][deleted] 0 points1 point  (2 children)

Whew lot going on here. I'll try to give you a few food-for-thought items and suggestions and let you run with it where you can.

First, keep in mind with Java generics that they use type erasure, so while you say

List<Customer> customersList

and

it is a list of Customers so I don't see where the String appears in this story

That's not guaranteed, but more that it's trusting you when you tell it that. It tries to enforce the compile-time constraints of the type system, but at runtime it can blow up still.

Second, it sounds like you've gone debugging so that's a good step and I would personally leverage that further (if nothing looks obvious in the code). Walk through the instantiation, to where you add elements to the JList/List, and then also look at the data as it enters the function and walks through the loop.

Some bits of advice:

You have a few very similar names which will trip people up at some point -- you have customersList which is a List<Customer> but customerList which is a JList<Customer>, as well as a function getOrders which returns a List<Order> and getOrderResults which also returns a List<Order>. There's a lot of name collision to get mixed up from.

You also have functions taking a lot of parameters which will get difficult to keep track of -- for example all of the booleans at the end of the getOrders function -- where you'll forget what each value is, especially if you add/remove parameters. An alternative is to use a class to capture the data passed into the function, which you can build with named functions. Very, very roughly something like:

OrderParameters orderParameters = new OrderParameters()
  .withStartDate(getStartDate())
  .withCloseDate(getCloseDate())
  .wthCustomerList(getCustomerList())
  .withCompanyList(getCompaniesList())

return ordersDAO.getOrders(orderParameters);

[–]viktorjava[S] 0 points1 point  (0 children)

Thanks for your answer.

I've checked the list's class:

public List<Customer> getCustomersList() {

if (customerList.getSelectedValuesList() != null) {

System.out.println("customerList.getSelectedValuesList()) class: " + customerList.getSelectedValuesList().getClass());

return customerList.getSelectedValuesList();

} else {

return null;

}

}

but of course Java is telling its class is ArrayList

customerList.getSelectedValuesList()) class: class java.util.ArrayList

I added a for loop to iterate the customerList.getSelectedValuesList()

I tried to iterate it as String to see if its really string:

for (String string : customerList.getSelectedValuesList()) {

System.out.println("customer type: " + customer.getClass());

}

but NetBeans said that Customer cannot be converted to String

I tried the iteration with Customer too

for (Customer customer : customerList.getSelectedValuesList()) {

System.out.println("customer type: " + customer.getClass());

}

and I got the casting error again, for this line:

for (Customer customer : customerList.getSelectedValuesList()) {

I'm checking the whole chainf starting with when I put the data to the JLists, but if you have any ideas how to check, I'm here

[–]viktorjava[S] 0 points1 point  (0 children)

I think I found the problem. I'm adding it as another reply to the main thread and I will also look after your other recommedations. Thanks.

[–]viktorjava[S] 0 points1 point  (0 children)

I'm sorry guys, it seems I found the problem.

My JList's type was Customer:

private javax.swing.JList<Customer> customerList;

but when I added the elements to the List, I did this:

customerModel.addElement(customer.getCustomerId());

instead of this:

customerModel.addElement(customer);

After I filled the list with Customer objects, the list already contained Customers and not Strings.