0

since I m trying to test a stateful EJB i got the "java.lang.ClassCastException: java.lang.String cannot be cast to com.tutorial.stateful.Book" error.

my client code looks as follow

 import com.tutorial.stateful.LibraryStatefulSessionBeanRemote;
import com.tutorial.stateful.Book;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class EJBTesterStateful {

    BufferedReader brConsoleReader = null; 
   Properties props;
   InitialContext ctx;
   {
      props = new Properties();
      try {
         props.load(new FileInputStream("jndi.properties"));
      } catch (IOException ex) {
         ex.printStackTrace();
      }
      try {
         ctx = new InitialContext(props);            
      } catch (NamingException ex) {
         ex.printStackTrace();
      }
      brConsoleReader = 
      new BufferedReader(new InputStreamReader(System.in));
   }

   public static void main(String[] args) {

      EJBTesterStateful ejbTester = new EJBTesterStateful();

      ejbTester.testStatefulEjb();
   }

   private void showGUI(){
      System.out.println("**********************");
      System.out.println("Welcome to Book Store");
      System.out.println("**********************");
      System.out.print("Options \n1. Add Book\n2. Exit \nEnter Choice: ");
   }

   private void testStatefulEjb(){

      try {
         int choice = 1; 

         LibraryStatefulSessionBeanRemote libraryBean =
         (LibraryStatefulSessionBeanRemote)ctx.lookup("ejb:/EjbComponent//LibraryStatefulSessionBean!com.tutorial.stateful.LibraryStatefulSessionBeanRemote?stateful");

         while (choice != 2) {
            String bookName;
            showGUI();
            String strChoice = brConsoleReader.readLine();
            choice = Integer.parseInt(strChoice);
            if (choice == 1) {
               System.out.print("Enter book name: ");
               bookName = brConsoleReader.readLine();
               Book book = new Book();
               book.setName(bookName);
               libraryBean.addBook(book.toString());          
            } else if (choice == 2) {
               break;
            }
         }

         List<Book> booksList = libraryBean.getBooks();

       System.out.println("Book(s) entered so far: " + booksList.size());
        int i = 0;

        // Exception line-trigger         
        for(Book book:booksList){
         System.out.println((i+1)+". " + book.getName());
         }

         LibraryStatefulSessionBeanRemote libraryBean1 = 
            (LibraryStatefulSessionBeanRemote)ctx.lookup("ejb:/EjbComponent//LibraryStatefulSessionBean!com.tutorial.stateful.LibraryStatefulSessionBeanRemote?stateful");
         List<String> booksList1 = libraryBean1.getBooks();
         System.out.println(
            "***Using second lookup to get library stateful object***");
         System.out.println(
            "Book(s) entered so far: " + booksList1.size());
         for ( i = 0; i < booksList1.size(); ++i) {
            System.out.println((i+1)+". " + booksList1.get(i));
         }       
      } catch (Exception e) {
         System.out.println(e.getMessage());
         e.printStackTrace();
      }finally {
         try {
            if(brConsoleReader !=null){
               brConsoleReader.close();
            }
         } catch (IOException ex) {
            System.out.println(ex.getMessage());
         }
      }
   }

}

 the statefull ejb class looks like:

    @Stateful
public class LibraryStatefulSessionBean implements LibraryStatefulSessionBeanRemote{

       List<String> bookShelf;    

   public LibraryStatefulSessionBean(){
      bookShelf = new ArrayList<String>();
   }

   public void addBook(String bookName) {
      bookShelf.add(bookName);
   }    

   public List<String> getBooks() {
      return bookShelf;
   }
}

is ther any idea to solve this casting issue.

Many Thanks



thanks, your responses were helpful to avoid the casting problem after some changes in stateful Bean class:

1
  • 1
    please provide a stack trace also. Commented Apr 22, 2016 at 11:28

2 Answers 2

1

getBooks returns a List of Strings not Books

List<String> booksList = libraryBean.getBooks(); // as you've already done for booksList1
for (String book: booksList){
     ....
}
0

If you see the following error message

java.lang.String cannot be cast to com.tutorial.stateful.Book

it is very clear that you are assigning an object which is a String to an object which is a Book. This is impossible because there is no relationship in between these two classes. So, a String class cannot be cast to a Book type.

It will raise a ClassCastException.

The problem occurs in this line of code

List<Book> booksList = libraryBean.getBooks(); //this line will throw an exception, because it returns a List<String> not List<Book>.

// Exception line-trigger         
for(Book book: booksList){
    System.out.println((i+1) + ". " + book.getName());
}

because

public List<String> getBooks() { //returns a List<String> not List<Book>.
    return bookShelf;
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.