473,699 Members | 2,890 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 32830
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
2460
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
11856
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 once which of course not the solution I was looking for. I simply want to know how I can check...
2
6030
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
7250
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
3011
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
46240
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
11044
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
35085
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
6452
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 information, from this column you can check weather a specific column exists in a specific table. ...
0
8705
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8623
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9196
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8896
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5879
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4390
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2362
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.