473,574 Members | 2,988 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to check if an object already exists

oll3i
679 Contributor
how to check if an object already exists and return reference to that object
Apr 5 '07
59 32743
oll3i
679 Contributor
i get nullpointerexce ption
Apr 7 '07 #11
oll3i
679 Contributor
i changed it to what You suggested but it doesnt change that it doesnt find the klient in the list
the client is added to the list when a button is clicked then that client adds to the cart but before he adds to the cart i need to check if it isnt a client that already exists if it is then return him with klient = klienci.get(ind ex);<< but it doesnt find that client

there is probably a better solution for that but unfortunately i can not think of one right one
Apr 7 '07 #12
JosAH
11,448 Recognized Expert MVP
Does your Klient class override the equals() and hashCode() methods?

kind regards,

Jos
Apr 7 '07 #13
oll3i
679 Contributor
no it doesnt shd it?
Apr 7 '07 #14
JosAH
11,448 Recognized Expert MVP
no it doesnt shd it?
Yes it should. Read all about it in the Object.equals() and Object.hashCode ()
methods documentation.

By default equality is considered reference equality and you want value equality,
e.g. if two distinct objects contain identical member values you want to consider
the two distinct objects as equal.

kind regards,

Jos
Apr 7 '07 #15
oll3i
679 Contributor
if i define an equal method for klient (client in english) how shd i use it to compare if that client already exists cos i created a list of clients and i need to find that client in a list or shd i do in in some other way
Apr 7 '07 #16
hirak1984
316 Contributor
can I see how you have defined the object variable?

Did you set it to null , initially?

i get nullpointerexce ption
Apr 7 '07 #17
JosAH
11,448 Recognized Expert MVP
if i define an equal method for klient (client in english) how shd i use it to compare if that client already exists cos i created a list of clients and i need to find that client in a list or shd i do in in some other way
That's the convenient part of it all: the List does it for you when you call a
List.contains() of List.indexOf() method. Have a look at the contains() method:
(this is from the 1.6 JDK)
Expand|Select|Wrap|Line Numbers
  1.     public boolean contains(Object o) {
  2.     Iterator<E> e = iterator();
  3.     if (o==null) {
  4.         while (e.hasNext())
  5.         if (e.next()==null)
  6.             return true;
  7.     } else {
  8.         while (e.hasNext())
  9.         if (o.equals(e.next()))
  10.             return true;
  11.     }
  12.     return false;
  13.     }
See how it uses the equals() method when it needs to find an object.

kind regards,

Jos
Apr 7 '07 #18
oll3i
679 Contributor
if i define method equals for klient how i can compare if that klient(client in english) already exists i created a list of clients client doesnt inherits from any other class so i can not compare it to a class it inherits from
Apr 7 '07 #19
JosAH
11,448 Recognized Expert MVP
if i define method equals for klient how i can compare if that klient(client in english) already exists i created a list of clients client doesnt inherits from any other class so i can not compare it to a class it inherits from
Your Klient class equals() method just needs to compare your 'this' Klient
with another Klient object:
Expand|Select|Wrap|Line Numbers
  1. public class Klient {
  2.    .
  3.    .
  4.    public boolean equals(Object obj) {
  5.       if (obj == null || !(obj instanceof Klient)) return false;
  6.       Klient that= (Klient)obj;
  7.       // compare 'this' Klient with 'that' Klient and return true or false;
  8.       return ...;
  9.    }
Don't forget to implement the hashCode() method too. The two methods always
come in pairs, if you implement one you have to implement the other one too.

kind regards,

Jos
Apr 7 '07 #20

Sign in to post your reply or Sign up for a free account.

Similar topics

6
2458
by: lawrence | last post by:
How dangerous or stupid is it for an object to have a reference to the object which contains it? If I have a class called $controllerForAll which has an arrray of all the objects that exist, what happens if one of those objects, when it is created, takes a reference to the object that contains it? Do bad things happen? class McShow {
2
11850
by: Jonathan | last post by:
I am looking for a simple way to check if a database table exists. I keep getting advice to use "Try.. Catch" and other error handling methods, but I obviously don't want to have to display an error message and stop the process every time someone loads the script after the table is created because that would mean the page could only ever run...
2
6012
by: Mike | last post by:
I´ve got a number of SPAN elements named "mySpan1", "mySpan2", "mySpan3" etc, and want to set their "style.display" to "inline". This works (only needs to work on IE5.5+): for (var x = 1; x < 20; x++) { document.all('mySpan'+x).style.display = "inline"; } But I don´t know how many SPAN elements there are, so I need to set x to a
9
7241
by: Carl Fenley | last post by:
I am successfully adding stored procedures to an Access database. However, I need to be able to check if the stored procedure of the same name already exists. Is there a way to do this other than waiting for the OleDbException caused when adding one that already exists? Here is the code snippet: Private Sub CreateStoredProcedures()
4
3005
by: jes | last post by:
hi, i have an open & delete btn. onclick of open as visio drawing opens in visio & onclick of delete the drawing gets deleted from the filesystem. The problem is i am unable to perform these actions if the file is already opened in another instance of visio. how do i check if it's already opened and then throw an error msg? thanks
14
46233
by: John Salerno | last post by:
What is the best way to check if a file already exists in the current directory? I saw os.path.isfile(), but I'm not sure if that does more than what I need. I just want to check if a file of a certain name exists before the user creates a new file of that name. Thanks.
3
11030
by: byeung | last post by:
Hi, I am trying to check if a particular record already exists in an Access database through Excel vba code. Through code obtained at another forum, I got the following: *********************************************************************** Sub TheButton()
2
35072
by: Larry | last post by:
Thanks in advance. what is the C# equivalent of the VB.net IsNothing(object) function? just trying to check to see if an object instance already exists. does someone have an example? *** Sent via Developersdex http://www.developersdex.com ***
0
6446
by: bharathreddy | last post by:
This article will explain you how to check weather a column already exists in a table before you add the column to the table using alter command. Using the system tables you can check to see weather a column already belongs to a specific table. SYSCOLUMNS is the system table which stores all the table columns...
0
8048
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8232
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7811
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8097
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6451
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5300
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3740
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3749
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2241
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.