Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: ClassCastException ArrayList Cannot be cast to package.name.ClassNAME can some one Help to fix?

  1. #1
    Junior Member
    Join Date
    Feb 2019
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ClassCastException ArrayList Cannot be cast to package.name.ClassNAME can some one Help to fix?

    hello guys im working on Books Library Sprint .
    trying to filter by ISBN number but then when im running Junit Test the Result : ArrayList Cannot be cast to library.PickPeriod(Package Name)

    would be greatfull to have some help please !
    i know there is something missing after or before the filter..

    	public BooksReturnCode pickBook(long isbn, int readerId, LocalDate pickDate) {
    		Book book = getBookItem(isbn);
    		Reader reader = getReader(readerId);
    		if(book == null) return BooksReturnCode.NO_BOOK_ITEM;
    		if(!readers.containsKey(readerId)) return BooksReturnCode.NO_READER;
    			if(book.getAmount()==book.getAmountInUse())
    			return BooksReturnCode.NO_BOOK_EXEMPLARS;
     
    		List<PickRecord> pickRecords = readerRecord.getOrDefault(readerId, new ArrayList<>());
    		PickRecord record = (PickRecord) pickRecords.stream()
    				.filter(d->d.getIsbn()==isbn)
    				.collect(Collectors.toList());
     
    		if(record!=null&&record.getReturnDate()==null)
    			return BooksReturnCode.READER_READS_IT;
     
     
    		record = new PickRecord(isbn, readerId, pickDate);
     
    		addBookRecord(record);
    		addReaderRecord(record);
    		addRecords(record);
     
     
    		return BooksReturnCode.OK;
    	}
    Last edited by MelkWeg; February 6th, 2019 at 08:31 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ClassCastException ArrayList Cannot be cast to package.name.ClassNAME can some one Help to fix?

    ArrayList Cannot be cast to library.PickPeriod(Package Name)
    An object of type ArrayList can not be cast to the type mentioned in the error message.

    What statement was flagged as causing that exception?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2019
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ClassCastException ArrayList Cannot be cast to package.name.ClassNAME can some one Help to fix?

    Quote Originally Posted by Norm View Post
    An object of type ArrayList can not be cast to the type mentioned in the error message.

    What statement was flagged as causing that exception?
    the Casting (PickPeriod) before the start of the stream...
    i know for sure the problem is in the Syntax of :

    List<PickRecord> pickRecords = readerRecord.getOrDefault(readerId, new ArrayList<>());
    PickRecord record = pickRecords.stream()
    .filter(d->d.getIsbn()==isbn)
    .collect(Collectors.toList());
    when im trying to filter , its just making a ERROR in Syntax. and all the code blowing in Red.

  4. #4
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: ClassCastException ArrayList Cannot be cast to package.name.ClassNAME can some one Help to fix?

    I don't see a PickPeriod anywhere in there.

    Regards,
    Jim

  5. #5
    Junior Member
    Join Date
    Feb 2019
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ClassCastException ArrayList Cannot be cast to package.name.ClassNAME can some one Help to fix?

    Quote Originally Posted by jim829 View Post
    I don't see a PickPeriod anywhere in there.

    Regards,
    Jim
    Sorry its PickRecord which is a Class , i edit the code. can take a look now , in my opinion i just missing something with the Filtering.. i can explain what i should get but the Sprint got alot of Class will be hard to explain with Out seeing the Whole class , i just curios if there is a alternative way for me to search if not with Filter.. Btw if you really up to Help can send you PM ty for answers Guys!

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ClassCastException ArrayList Cannot be cast to package.name.ClassNAME can some one Help to fix?

    Can you make a small, complete program that compiles and executes to show the problem?
    Do not post the whole of your project. Post just a small program for testing.

    Sorry its PickRecord
    Please don't post things that are not from the program. Post only text from the javac or java programs.
    Don't make up error message texts.

      PickRecord record = (PickRecord) pickRecords.stream()
    				.filter(d->d.getIsbn()==isbn)
    				.collect(Collectors.toList());
    What value does the collect method return?
    What is the data type to the left of the =?
    They need to match.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2019
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ClassCastException ArrayList Cannot be cast to package.name.ClassNAME can some one Help to fix?

    Quote Originally Posted by Norm View Post
    Can you make a small, complete program that compiles and executes to show the problem?
    Do not post the whole of your project. Post just a small program for testing.
    it will be a Problem i dont really have idea how to because all of the Setters/getters etc.. i can post link to mine Project .. and tell you exactly which Class/Lines not working ..if its ok let me know please when youll see whole Class of the Methods youll undrstood what i supposed to do .. i can even Show the UML if its helps . everything which will help to pass mine last JUnit test :\

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ClassCastException ArrayList Cannot be cast to package.name.ClassNAME can some one Help to fix?

      PickRecord record = (PickRecord) pickRecords.stream()
    				.filter(d->d.getIsbn()==isbn)
    				.collect(Collectors.toList());
    What value/data type does the collect method return?
    What is the data type to the left of the =?
    They need to match.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Feb 2019
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ClassCastException ArrayList Cannot be cast to package.name.ClassNAME can some one Help to fix?

    Quote Originally Posted by Norm View Post
      PickRecord record = (PickRecord) pickRecords.stream()
    				.filter(d->d.getIsbn()==isbn)
    				.collect(Collectors.toList());
    What value/data type does the collect method return?
    What is the data type to the left of the =?
    They need to match.
    d.getIsbn() should be match to the isbn of the Method Parm methodName(long isbn..) . if im getting you right , im kind of new with Streams..
    im tryin to return if there is a Reader who reads the book , so no one else could "pick" the Book till the reader will Return it to Library .

    Edit : to be more Clearly , i have made List of Records . for readers , so im tryin with that record of Readers to check if some of the Readers Already Reading the BOOK .
    Last edited by MelkWeg; February 6th, 2019 at 09:14 PM.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ClassCastException ArrayList Cannot be cast to package.name.ClassNAME can some one Help to fix?

    Look at the API doc for collect. What data type does it return?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Feb 2019
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ClassCastException ArrayList Cannot be cast to package.name.ClassNAME can some one Help to fix?

    Quote Originally Posted by Norm View Post
    Look at the API doc for collect. What data type does it return?
    oh , it will sound Stupid but im sorry i barely get you.. if you think there is a better way to change the method you can give me a Hint ill try to .. , i still think with out seeing mine Project you wouldnt know how to help .. or you can guess cause you seems like a good programer ..

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ClassCastException ArrayList Cannot be cast to package.name.ClassNAME can some one Help to fix?

    All methods are defined as void or as a datatype that they return. What is the definition for the collect method? What does it return?
    When assigning the value returned by a method, the datatypes must match.
    Datatype var = someMethod();
    The someMethod method must be defined to return a value of Datatype.

    To see what is being returned, remove the cast and assign the returned value to a variable of type Object. Then print out the class of that variable:
    Object obj = .... collect...
    System.out.println(obj.getClass());  // show obj's class

    I'm done for today. Back tomorrow.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Feb 2019
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ClassCastException ArrayList Cannot be cast to package.name.ClassNAME can some one Help to fix?

    Quote Originally Posted by Norm View Post
    All methods are defined as void or as a datatype that they return. What is the definition for the collect method? What does it return?
    When assigning the value returned by a method, the datatypes must match.
    Datatype var = someMethod();
    The someMethod method must be defined to return a value of Datatype.

    To see what is being returned, remove the cast and assign the returned value to a variable of type Object. Then print out the class of that variable:
    Object obj = .... collect...
    System.out.println(obj.getClass());  // show obj's class

    I'm done for today. Back tomorrow.
    java.util.ArrayList .
    it is kind what im tryin to return , if there is a Reader who reads it i wanna return Enum COnstant. if there is No Reader who reads it i wanna return Empty ArrayList to avoid Null .
    ty for your answering ill be glad to get help tomorrow to find way to fix it ! have a nice day/night

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ClassCastException ArrayList Cannot be cast to package.name.ClassNAME can some one Help to fix?

    java.util.ArrayList .
    Ok there is an ArrayList returned. The assignment statement needs to save that to a List variable.
    What value do you want from that (possibly empty) list?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Feb 2019
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ClassCastException ArrayList Cannot be cast to package.name.ClassNAME can some one Help to fix?

    Quote Originally Posted by Norm View Post
    Ok there is an ArrayList returned. The assignment statement needs to save that to a List variable.
    What value do you want from that (possibly empty) list?
    i have change my Code , JUnit test Passed . but now its not seems like professional Code because there is
    yellow color when im running the Coverage.

    List<PickRecord> pickRecords =
    readerRecord.getOrDefault(readerId, new ArrayList<>());

    //1 of 2 branchess missed// if(pickRecords!=null)
    //1 of 6 branchess missed// if(pickRecords.stream().anyMatch(p->p.getIsbn()==isbn && p.getReturnDate()==null))
    return BooksReturnCode.READER_READS_IT;
    can i have your opinion? thank you again .

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ClassCastException ArrayList Cannot be cast to package.name.ClassNAME can some one Help to fix?

    there is yellow color
    Sorry, I do not understand what your problem is now.

    Is there any message when you move the cursor over the color?
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Feb 2019
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ClassCastException ArrayList Cannot be cast to package.name.ClassNAME can some one Help to fix?

    Quote Originally Posted by Norm View Post
    Sorry, I do not understand what your problem is now.

    Is there any message when you move the cursor over the color?
    when you running Coverage there is Green Color on the Code.
    that means its 100% ok , if there is yellow color it means there is some thing missed as i mentioned
    in the Quote i write near to the if statement.
    there is missed Branches have no idea what that means to be honest..

    and of course if the code got breaking points so it will be Red Color . that means the Test not Passed.

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ClassCastException ArrayList Cannot be cast to package.name.ClassNAME can some one Help to fix?

    there is missed Branches
    Sorry, I do not know what is meant by a Branch.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. cast with variable for classname
    By Chaaka in forum Object Oriented Programming
    Replies: 2
    Last Post: May 28th, 2018, 06:30 PM
  2. how to import/ access classes created in one package in another package
    By ydandurkar in forum What's Wrong With My Code?
    Replies: 8
    Last Post: May 11th, 2014, 11:12 AM
  3. HashSet cannot be cast to java.util.ArrayList
    By vector_ever in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 7th, 2013, 03:11 PM
  4. Replies: 2
    Last Post: November 12th, 2012, 03:24 PM
  5. ClassCastException
    By kamalivy in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 20th, 2010, 10:57 PM