how to check if an object already exists

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oll3i
    Contributor
    • Mar 2007
    • 679

    #46
    Good Morning :)
    Code:
    public boolean equals(Object o) {
        if (!(o instanceof Klient)) return false;
        Klient k = (Klient) o;
        System.out.println(nazwa);
        System.out.println(k.nazwa);
        System.out.println(pieniadze);
        System.out.println(k.pieniadze);
        return  nazwa.equals(k.nazwa) && pieniadze == k.pieniadze;
      }
    
    public static void main(String[] args) {
        
        Klient k1 = new Klient("Jan",200);
        Klient k2 = new Klient ("Jan",200);
        
        System.out.print(k1.equals(k2));
    }
    }
    prints out
    Jan
    Jan
    200.0
    200.0
    true

    is that what you meant

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #47
      Originally posted by oll3i
      Good Morning :)

      is that what you meant
      Yep, what happens now if you use the List.contains(. ..) method after you've
      inserted one of those Klients? The List should call your equals() method for
      the actual comparisons (which in turn show you what actually is compared).

      kind regards,

      Jos

      Comment

      • oll3i
        Contributor
        • Mar 2007
        • 679

        #48
        now it finds a client in a list

        ...but i get nullpointerexce ption :( and eclipse doesnt say the line

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #49
          Originally posted by oll3i
          now it finds a client in a list

          ...but i get nullpointerexce ption :( and eclipse doesnt say the line
          Do you have a 'null' element in your List? Print the entire list (a List can do
          it itself):
          Code:
          System.out.println(yourList);
          ... and see for yourself.

          kind regards,

          Jos

          Comment

          • oll3i
            Contributor
            • Mar 2007
            • 679

            #50
            the client is in a list the nullointerexcep tion is somewhere else but after i click a button

            Comment

            • oll3i
              Contributor
              • Mar 2007
              • 679

              #51
              please clear me that out so i learn for the future how a list can call the equals method when i defined it for a client ?

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #52
                Originally posted by oll3i
                please clear me that out so i learn for the future how a list can call the equals method when i defined it for a client ?
                This is what method overriding is all about. The Object class (the top of the
                class hierarchy) defines an equals() method. The List calls an equals() method.
                You have overridden Object's equals() method so *your* equals() method will
                be called by the List because your Klient extends the Object class (implicitly)
                and your class defines its own equals() method.

                Your NullPointerExce ption is an entirely different problem though ...

                kind regards,

                Jos

                Comment

                • oll3i
                  Contributor
                  • Mar 2007
                  • 679

                  #53
                  so how wd that look in use?

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #54
                    Originally posted by oll3i
                    so how wd that look in use?
                    Sorry, I don't understand your question.

                    kind regards,

                    Jos

                    Comment

                    • oll3i
                      Contributor
                      • Mar 2007
                      • 679

                      #55
                      cd you give me now the example how i shd use that in my case with my list and equals method
                      please

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #56
                        Originally posted by oll3i
                        cd you give me now the example how i shd use that in my case with my list and equals method please
                        You don't have to do anything: method overriding is an intrinsic part of the Java
                        language. You have implemented your own equals() method and therefore
                        (automatically) overridden the equals() method in the Object class. You've
                        seen it happen yourself: the List calls your equals() method instead of the
                        one defined in the Object class.

                        kind regards,

                        Jos

                        Comment

                        • oll3i
                          Contributor
                          • Mar 2007
                          • 679

                          #57
                          when the list calls the equal method?

                          Comment

                          • prometheuzz
                            Recognized Expert New Member
                            • Apr 2007
                            • 197

                            #58
                            Originally posted by oll3i
                            when the list calls the equal method?
                            Whenever you invoke the contains(...) and indexOf(...) methods from the List, for example.
                            Please take the time to read this tutorial:

                            it explains in great detail how to override the equals(...) (and hashCode()!) method(s).

                            Comment

                            • oll3i
                              Contributor
                              • Mar 2007
                              • 679

                              #59
                              cd you look at my nullpointerexce ption post with nullpointerexce ption in the title
                              please

                              Comment

                              • JosAH
                                Recognized Expert MVP
                                • Mar 2007
                                • 11453

                                #60
                                Originally posted by oll3i
                                cd you look at my nullpointerexce ption post with nullpointerexce ption in the title
                                please
                                I just did; please follow up my advice I gave you in your other thread and then
                                come back here when your don't understand what the Exception stacktrace has
                                to tell you.

                                kind regards,

                                Jos

                                Comment

                                Working...