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

Thread: Noobie equals() problem.

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question Noobie equals() problem.

    Alright, I'm not gonna' lie. This is probably a silly problem for all you veterans, but I'm very (VERY) fresh in the art of Java code-writing, so forgive the noobishness.

    I'm writing an equals()-method for my program which looks like this:

      public boolean equals(Object objekt) {
        Pasientjournal middlertidig = (Pasientjournal)objekt;
        if (this.getID() == middlertidig.getID())
          return true;
        else
          return false;
      }

    But whenever i try to actually compare anything i get tossed a:

    java.lang.ClassCastException: java.lang.Integer cannot be cast to Pasientjournal
    	at Pasientjournal.equals(Pasientjournal.java:35)
    ...
    ...
    ...

    Now, like i said, I'm rather fresh here, so could someone toss me a short explanation of what's wrong?

    Or maybe just toss me the correct version of my faulty code?

    ("middlertidig" is btw just my native tongue for "temporary")

    Thanks so much for the help guys!


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Noobie equals() problem.

    Pasientjournal middlertidig = (Pasientjournal)objekt;

    You are passing it an int or Integer apparently as a parameter to that method.

  3. The Following User Says Thank You to javapenguin For This Useful Post:

    ViRAL (November 12th, 2011)

  4. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Noobie equals() problem.

    Read the API for the equals method in Object. This method must be implemented carefully, and take into account things like object parameters that are null as well as object parameters that are not the same class type (this is the reason for your exception - trying to compare objects of two different classes - your first statement is a class cast, which will immediately throw an exception if the parameter object is of a different class type)

  5. The Following User Says Thank You to copeg For This Useful Post:

    ViRAL (November 12th, 2011)

  6. #4
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Noobie equals() problem.

    Quote Originally Posted by javapenguin View Post
    Pasientjournal middlertidig = (Pasientjournal)objekt;

    You are passing it an int or Integer apparently as a parameter to that method.
    So what you mean is that the equals()-method is getting an int (or Integer) as an argument?

    Something the lines of...

    public boolean equals(SomeNumberHere) {
    ...
    ...
    ...

    And this is why it doesn't work?
    Or did i misunderstand your answer?

    Thanks anyway mate.

    EDIT (to avoid making more posts than needed):

    Quote Originally Posted by copeg View Post
    Read the API for the equals method in Object. This method must be implemented carefully, and take into account things like object parameters that are null as well as object parameters that are not the same class type (this is the reason for your exception - trying to compare objects of two different classes - your first statement is a class cast, which will immediately throw an exception if the parameter object is of a different class type)
    So am I to understand that the line:

    Pasientjournal middlertidig = (Pasientjournal)objekt;

    Doesn't convert the object (named "objekt") into an object of Pasientjournal?
    Last edited by ViRAL; November 11th, 2011 at 05:58 PM.

  7. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Noobie equals() problem.

    Your function definition seems to accept Object as a parameter and exception shows that you are passing Integer.
    See it closely or post your code here so that we could have a look over it.
    But kindly before posting code read SSCCE

  8. The Following User Says Thank You to Mr.777 For This Useful Post:

    ViRAL (November 12th, 2011)

  9. #6
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Noobie equals() problem.

    I read the API carefully and managed to detect my own problem, so the code functional. For now at least.

    Thanks have been given to everyone who helped me!

  10. #7
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Noobie equals() problem.

    @ViRAL: You are always welcome to ask for help but you must need to know the rules. As these are really important to save your time and ours as well.
    You must read SSCCE as mentioned in my above post.
    Hope you will contribute here as a helping member for others to.
    Good Luck.

Similar Threads

  1. string==null or string.equals(null) problem
    By csharp100 in forum What's Wrong With My Code?
    Replies: 31
    Last Post: November 4th, 2011, 08:17 AM
  2. Using the .equals method
    By TenaciousE in forum Java Theory & Questions
    Replies: 2
    Last Post: October 23rd, 2011, 05:24 PM
  3. Noobie issues?
    By sukuiichuu in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 3rd, 2011, 05:10 AM
  4. [SOLVED] Replace String Method? Noobie :/
    By Usoda in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 18th, 2011, 10:58 AM
  5. Noobie to Java questions!! :D
    By chansey123 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 17th, 2010, 11:53 AM