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 2 of 2

Thread: Exception in thread Main

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

    Default Exception in thread Main

    package week4;

    import java.util.HashMap;

    //the class is set as final before release for sharing or use,
    //so that others cannot overwrite
    public final class myClass {

    //set the data to be used by others to be private and final so that it cannot be overwritten.
    private static HashMap<Integer, String> hm;

    //But to allow others to use, need to set as public
    public myClass() {
    myClass.hm = new HashMap <Integer, String>();
    }

    public myClass gethm() {
    return (myClass) hm.clone();

    }

    public static void main(String [] args)
    {
    myClass a = new myClass();
    a.gethm();
    }


    }

    /*Exception in thread "main" java.lang.ClassCastException: java.util.HashMap cannot be cast to week4.myClass
    at week4.myClass.gethm(myClass.java:18)
    at week4.myClass.main(myClass.java:25)
    */

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Exception in thread Main

    " java.lang.ClassCastException: java.util.HashMap cannot be cast to week4.myClass
    The error message says what the problem is. A HashMap is not a myClass.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: May 25th, 2014, 05:36 AM
  2. Exception in my code:Exception in thread "main" java.lang.NullPointerException
    By vickychawla.chawla in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 25th, 2014, 05:30 AM
  3. Exception in thread "main" java.lang.NoSuchMethodError: main?
    By Sakharam01 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 10th, 2013, 08:06 AM
  4. Exception In Thread Main ArrayIndexOutOfBoundsException
    By parkie in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 16th, 2013, 02:27 PM
  5. Replies: 2
    Last Post: March 26th, 2010, 11:22 AM