Broken null check
I am new to Java
I have a doubt in coding standards
String strObj= swingobject.getText();
case 1
if(strObj==null && strObj.equals(""){ some exception is thrown}
case 2
if(strObj !=null && !strOb.equals(""){ some code is written}
though the code works there is a violation in coding standard. how it can be changed as per coding standard
Please help me
suja_k wrote:
I was told it is a violation of coding standard.In Java it's an accepted coding standard. It's called the null guard. The purpose is to avoid null pointer exceptions.
It works because of two Java language feature. The first is that || and && are short-cut logical operators. This means that if the left side is evaluated and that's enought to establish the logical value of the whole expression, then the right side won't be evaluated. The second feature is that expressions are always evaluated from left-to-right.
Now look at this.
if (strObj==null && strObj.equals(""))If the left side evaluates to true you know strObj is null. But since true on the left side requires the && operator to evaluate also the right side to be able to determine whether the whole expression is false or true, also the right side will be evaluated. This leads to a null pointer exception and your null guard didn't work. It works if you change to || because if the left side is true the whole expression is known to be true which means the right side won't have to be evaluated to establish that.
In the special case of equals there's a little trick you can use to replace the null guard. You "turn it around" like,
if ("".equals(strObj))This won't cause a null pointer exception even if strObj is null.
As a sidenote. Personally I'm not very fond of the null guard. In my view one can as well do,
if (strObj !=null)
if (strObj.equals(""))
Similar Messages
-
Primary key and relevant not null check constraints....
Hi ,
There are some constraints of primary key type and not null check constraints on columns which constitute each primary key....
Should I/Do I have to drop them....????
Do they burden the db at the time of data validation....????
Thanks...
SimHi,
>>There are some constraints of primary key type and not null check constraints on columns which constitute each primary key..
In fact, a column that constitutes a primary key, by default cannot accept NULL values. In this case, defines a PK column as NOT NULL would not be necessary.
LEGATTI@ORACLE10> create table x (id number constraint pk_x primary key);
Table created.
LEGATTI@ORACLE10> desc x
Name Null? Type
ID NOT NULL NUMBER
LEGATTI@ORACLE10> select constraint_name,constraint_type,table_name,search_condition from user_constraints where table_name='X';
CONSTRAINT_NAME C TABLE_NAME SEARCH_CONDITION
PK_X P X
LEGATTI@ORACLE10> create table y (id number not null constraint pk_y primary key);
Table created.
LEGATTI@ORACLE10> desc y
Name Null? Type
ID NOT NULL NUMBER
LEGATTI@ORACLE10> select constraint_name,constraint_type,table_name,search_condition from user_constraints where table_name='Y';
CONSTRAINT_NAME C TABLE_NAME SEARCH_CONDITION
SYS_C006327381 C Y "ID" IS NOT NULL
PK_Y P Y
LEGATTI@ORACLE10> alter table y drop constraint SYS_C006327381;
Table altered.
LEGATTI@ORACLE10> desc y
Name Null? Type
ID NOT NULL NUMBER
LEGATTI@ORACLE10> insert into y values (NULL);
insert into y values (NULL)
ERROR at line 1:
ORA-01400: cannot insert NULL into ("LEGATTI"."Y"."ID")
LEGATTI@ORACLE10> insert into y values (1);
1 row created.
LEGATTI@ORACLE10> insert into y values (1);
insert into y values (1)
ERROR at line 1:
ORA-00001: unique constraint (LEGATTI.PK_Y) violated
>>Should I/Do I have to drop them....????
I don't see any problem, otherwise, drop the NOT NULL constraint is the same with alter the column table like below:
LEGATTI@ORACLE10> create table z (id number not null constraint pk_z primary key);
Table created.
LEGATTI@ORACLE10> select constraint_name,constraint_type,table_name,search_condition from user_constraints where table_name='Z';
CONSTRAINT_NAME C TABLE_NAME SEARCH_CONDITION
SYS_C006328420 C Z "ID" IS NOT NULL
PK_Z P Z
LEGATTI@ORACLE10> desc z
Name Null? Type
ID NOT NULL NUMBER
LEGATTI@ORACLE10> alter table z modify id NULL;
Table altered.
LEGATTI@ORACLE10> select constraint_name,constraint_type,table_name,search_condition from user_constraints where table_name='Z';
CONSTRAINT_NAME C TABLE_NAME SEARCH_CONDITION
PK_Z P Z
LEGATTI@ORACLE10> desc z
Name Null? Type
ID NOT NULL NUMBERCheers
Legatti -
Null check for "ora:readFile()" function
Hi,
I want to check whether a particular file is present in a given folder in our local server.In case of success scenario the "ora:readFile()" function is working fine but if the file is not present in the same folder then,this function is getting failed with system exception ,reason:-message can't be null.
Can't we add a null check for this and continue the flow further?
Regards,
N.DasHi,
You should first try list operation in file adapter to check for the file then use the readfile() conditionally if file is found.
Try the function to mention the path as
ora:readFile(concat('file:',bpws:getVariableData('Invoke1_FileListing_OutputVariable','filelist','/ns1:filelist/ns1:file/ns1:directory'),'/',bpws:getVariableData('Invoke1_FileListing_OutputVariable','filelist','/ns1:filelist/ns1:file[1]/ns1:filename'))).
This will get you the content in base64encoded format. You can then decode it to get the text format data.
Thanks,
Durga
- It is considered good etiquette to reward answerers with points (as "helpful" - 5 pts - or "correct" - 10pts).* -
Table joins, templates, null-checks, layer confusion and localization
Hello everyone, this is my first post on this network.
I am designing a website that does several things, one of which is to display latest news items on the home page.
The news items are stored in the database as a table with a basic structure: id, poster_id, postDate, subject, body
where id is the primary key, poster_id is a foreign key to the users table, postDate is the date at which posted and the rest is obvious.
I want to display these items with a basic template:
subject by author at postDate
body
<hr />
When a user changes his/her username, the updated username will be shown on the news listing. This is the reason why I'm using a foreign key.
I've defined poster_id such that it can be set as NULL, meaning it was not created by a registered user, but a guest. Don't ask me why I'd want a guest to post a news item :P
When it comes to getting the news posts from the database and resolving the poster_id to a username i just use a join. However, when poster_id is NULL i obviously don't get a username for that post but a NULL. The word "Guest" has to come in when the poster_id is null. So in my template you'd think i could just put a null check as I can't do it in sql (as far as i know anyway).
Now, I'm using localization on the site and the word "Guest" is different depending on the locale. So you'd think i could just use a null check and a locale->get('guest_username') and that will return the word "Guest" in the right language.
If I want to view news on different pages, and more importantly, in different forms such as just author: subject, I come across a problem. I want to avoid having to repeat that null check code on every template that views news posts. Complicating things, I want anywhere that display a username to do the null check and return the locale'd guest username if it is a null.
Where do I put this null check? The locale object is part of the model layer, because other model objects use it.
Is this the presentation or the model layer's responsibility? I am relatively new to this design patterns thing, but I'm getting there :) I've thought about using wrapping classes but I'm just not sure at this point and I need smart people's opinion :)
I hope I was able to explain my problem properly and I thank anyone in advance for taking the time to read and give their insight on such a problem. Thank you :)antirealm wrote:
Hello everyone, this is my first post on this network.
I am designing a website that does several things, one of which is to display latest news items on the home page.
The news items are stored in the database as a table with a basic structure: id, poster_id, postDate, subject, body
where id is the primary key, poster_id is a foreign key to the users table, postDate is the date at which posted and the rest is obvious.
I want to display these items with a basic template:
subject by author at postDate
body
<hr />
When a user changes his/her username, the updated username will be shown on the news listing. This is the reason why I'm using a foreign key.
I've defined poster_id such that it can be set as NULL, meaning it was not created by a registered user, but a guest. Don't ask me why I'd want a guest to post a news item :PI think this is a fundamental mistake. A better design, IMO, would be to have an explicit GUEST or anonymous poster ID, but not null.
When it comes to getting the news posts from the database and resolving the poster_id to a username i just use a join. However, when poster_id is NULL i obviously don't get a username for that post but a NULL. The word "Guest" has to come in when the poster_id is null. So in my template you'd think i could just put a null check as I can't do it in sql (as far as i know anyway).See above.
Now, I'm using localization on the site and the word "Guest" is different depending on the locale. So you'd think i could just use a null check and a locale->get('guest_username') and that will return the word "Guest" in the right language.Localization should be a UI issue. It should be all sorted out by the time you get to the persistence layer.
If I want to view news on different pages, and more importantly, in different forms such as just author: subject, I come across a problem. I want to avoid having to repeat that null check code on every template that views news posts. Complicating things, I want anywhere that display a username to do the null check and return the locale'd guest username if it is a null.If it's hard to do, it's worth re-thinking. Don't use null when there are sensible non-null alternatives.
Where do I put this null check? The locale object is part of the model layer, because other model objects use it.
Is this the presentation or the model layer's responsibility? I am relatively new to this design patterns thing, but I'm getting there :) I've thought about using wrapping classes but I'm just not sure at this point and I need smart people's opinion :)L10N and I18N issues should be the responsibility of the presentation layer, IMO.
% -
Dreamweaver 8 - Problematic Broken Link Check
On a regular basis I use Dreamweaver 8's broken link checker
to convert
relative links to absolute throughout single HTML files.
Once upon a time, I could use the results pane to quickly
replace all
of the broken links. Ever since installing Dreamweaver 8
(upgraded from
MX) I've had the following problem with the link checker:
1. It seems to find all of the broken links correctly,
however, when I
try to change them into absolute, it doesn't actually make
the changes.
2. Sometimes it even makes incorrect changes to the path of
the link,
such as changing the image link to a .html link randomly (I
have no
idea how this happens!)
3. When it asks if I'd like to replace the other references
to the same
file, and I respond yes, it does nothing.
After researching this topic somewhat, I have found temporary
solution,
but it's driving me crazy and I'm looking for a long-term
resolution
for this issue.
If I delete my WinFileCache-xxxxxx.dat file (under User >
Application
Data > Macromedia > Dreamweaver 8 > Configuration)
before opening
Dreamweaver, the problem is resolved until the end of that
specific
session. As soon as I close Dreamweaver and reopen it, the
problem
returns.
I understand that by deleting the WinFileCache file I am
basically
starting with a clean slate, which is why this method is
temporarily
effective. However, this is a daily problem for me and I'm
sick of
having to do it every single time I use the program....
SO...if any of you have any thoughts, suggestions, or similar
complaints, please share - I'm desperate to find a long-term
solution!
Thanks!> On a regular basis I use Dreamweaver 8's broken link
checker to convert
> relative links to absolute throughout single HTML files.
Why?
> 1. It seems to find all of the broken links correctly,
however, when I
> try to change them into absolute, it doesn't actually
make the changes.
Give me an example of what it finds and what you want to
change it to,
please.
> 2. Sometimes it even makes incorrect changes to the path
of the link,
> such as changing the image link to a .html link randomly
(I have no
> idea how this happens!)
I would have to see this happen to believe it.
> SO...if any of you have any thoughts, suggestions, or
similar
> complaints, please share - I'm desperate to find a
long-term solution!
The real question is - why are you regularly getting broken
links? The only
reason I can think of is that your local site is actually on
a remote,
shared network drive, and there is intermittant connectivity
to it due to
some recurring network anomaly. Can you tell me how your site
is defined?
Murray --- ICQ 71997575
Adobe Community Expert
(If you *MUST* email me, don't LAUGH when you do so!)
==================
http://www.dreamweavermx-templates.com
- Template Triage!
http://www.projectseven.com/go
- DW FAQs, Tutorials & Resources
http://www.dwfaq.com - DW FAQs,
Tutorials & Resources
http://www.macromedia.com/support/search/
- Macromedia (MM) Technotes
==================
"pooley2" <[email protected]> wrote in
message
news:[email protected]...
> On a regular basis I use Dreamweaver 8's broken link
checker to convert
> relative links to absolute throughout single HTML files.
>
> Once upon a time, I could use the results pane to
quickly replace all
> of the broken links. Ever since installing Dreamweaver 8
(upgraded from
> MX) I've had the following problem with the link
checker:
>
> 1. It seems to find all of the broken links correctly,
however, when I
> try to change them into absolute, it doesn't actually
make the changes.
>
> 2. Sometimes it even makes incorrect changes to the path
of the link,
> such as changing the image link to a .html link randomly
(I have no
> idea how this happens!)
>
> 3. When it asks if I'd like to replace the other
references to the same
> file, and I respond yes, it does nothing.
>
> After researching this topic somewhat, I have found
temporary solution,
> but it's driving me crazy and I'm looking for a
long-term resolution
> for this issue.
>
> If I delete my WinFileCache-xxxxxx.dat file (under User
> Application
> Data > Macromedia > Dreamweaver 8 >
Configuration) before opening
> Dreamweaver, the problem is resolved until the end of
that specific
> session. As soon as I close Dreamweaver and reopen it,
the problem
> returns.
>
> I understand that by deleting the WinFileCache file I am
basically
> starting with a clean slate, which is why this method is
temporarily
> effective. However, this is a daily problem for me and
I'm sick of
> having to do it every single time I use the program....
>
> SO...if any of you have any thoughts, suggestions, or
similar
> complaints, please share - I'm desperate to find a
long-term solution!
>
> Thanks!
> -
Revision: 17935
Revision: 17935
Author: [email protected]
Date: 2010-09-29 13:07:07 -0700 (Wed, 29 Sep 2010)
Log Message:
Addition to fix for FM-936: Add null check when casting to NetStreamTimeTrait, since DVR elements will have a TimeTrait of a different type.
Ticket Links:
http://bugs.adobe.com/jira/browse/FM-936
Modified Paths:
osmf/trunk/framework/OSMF/org/osmf/net/NetStreamSeekTrait.asI had saved one copy as an .asp but then resaved it to .xhtml
I'm assuming this is a static web site. Rename (F2) all your pages with .html or .htm extension.
.xhtml is not a valid extension.
Nancy O. -
[svn:osmf:] 10707: Fixing FM-113 by adding a null check.
Revision: 10707
Author: [email protected]
Date: 2009-09-29 18:26:03 -0700 (Tue, 29 Sep 2009)
Log Message:
Fixing FM-113 by adding a null check.
Ticket Links:
http://bugs.adobe.com/jira/browse/FM-113
Modified Paths:
osmf/trunk/framework/MediaFramework/org/openvideoplayer/composition/ParallelSeekableTrait .asFirst, if this is your only backup, you must make another full backup to a locally-attached external hard drive. One backup is never enough to be safe, and backing up over a network is less reliable than backing up locally.
Hold down the option key and select Verify Backups from the Time Machine menu in the menu bar (not the Dock icon.) This operation may take several hours. If the menu-bar icon (a clock that runs backwards) isn't showing, check Show Time Machine in menu bar in the Time Machine preference pane. -
I have a basic question on good programming practice.
If i have a method that returns an object which will be used
by the callee method.
Is it better to check for the null return value in the called method or putting the check in the callee method.Please let me know if both are same or if in some process we can optimize the performance.It would depend on the situation. What's your situation like?
For some methods, it's legitimate to return null. For such a method, the caller will need to accept a null return value. Very often, this is not a problem a no null check is needed. If the caller wants to call a method in the returned object -- this is considered poor practice, talking to a stranger or breaking the Law of Demeter -- but if, then it needs to do a null check first.
Other methods are specified not to return null. For such methods, you may place an assert statement either inside the method before returning, or in the caller after the call, or both. Then, once your program is debugged and running fine, you optimize performance by disabling assertions. If the method is called from many locations, you may prefer to have the assert statement inside the method. On the other hand, now you will understand why some people frown on having multiple return statements in a method. :-)
This is definitely not all there is to say. I hope it helped a bit anyway. -
[svn:fx-trunk] 7763: Simple null-check to fix SDK-21677.
Revision: 7763
Author: [email protected]
Date: 2009-06-11 15:45:36 -0700 (Thu, 11 Jun 2009)
Log Message:
Simple null-check to fix SDK-21677.
Cleanup ASDoc entries for ArrayList for sub-task SDK-21591
QA: yes
Docs: no
Checkintests: pass
Ticket Links:
http://bugs.adobe.com/jira/browse/SDK-21677
http://bugs.adobe.com/jira/browse/SDK-21591
Modified Paths:
flex/sdk/trunk/frameworks/projects/flex4/src/spark/components/List.as
flex/sdk/trunk/frameworks/projects/framework/src/mx/collections/ArrayList.as -
I'm using Javascript to set the actions. I need to hide a text field if the value of a field in another sub-form is null.
- tried checking the value of the other field for null - doesn't work
- tried setting a variable str2 where I know the value of the other field is available then checking that variable when I initialize the text field - doesn't work
What am I missing?Hi.
Try this in the originating sub form referring to the text field (X).
if (this.rawValue = 1)
X.presence = "visible";
else if (this.rawValue = null)
X.presence = "hidden"; -
Null check for a bind variable in view criteria
Hi, Greetings.
I am using Jdeveloper 11.1.2.0.
I have a view object, LocationsView, for locations table of the XE database.
I want to apply a condition, ((:bind_var is null) or (:bind_var is not null and location_id = :bind_var), in this LocationsView.
That is, I want to apply the condition only if the bind variable value is not null.
How can I construct this condition using a declarative view criteria?
Any help and hints on this regard is greatly appreciated.
Regards,
Guru KHi,
Thanks for the update and it is working perfectly fine.
Now, how can I set the "ignore null values" option as checked if I am constructing the criteria programmatically using view criteria row and view criteria item at runtime.
Thanks and regards,
Guru K -
PreparedStatement use, null check
I'm using PreparedStatements in a number of queries that probably don't benefit from it and wondering if the problems I'm having are worth the extra I/O efficiency and security benefits. Thoughts?
Additionally, I'm having trouble knowing for sure if my resultsets from a PS are empty. Here's some sample code:
string sql = "SELECT * FROM atable WHERE id = ?";
PreparedStatement ps = c.prepareStatement(sql);
ps.setString(1, ID);
ResultSet rs = ps.executeQuery();
if(rs == null)
rs.next();
x = rs.getString("acolumn");
I know rs.next() executes because of some debug statements I've worked into the real code, but at getString("acolumn") I get "Exhausted Resultset." When I run the query by hand, it's an empty result. So what should I be using to check for null or 0 row count?I'm using PreparedStatements in a number of queries
that probably don't benefit from it and wondering if
the problems I'm having are worth the extra I/O
efficiency and security benefits. Thoughts?
Additionally, I'm having trouble knowing for sure if
my resultsets from a PS are empty. Here's some
sample code:
string sql = "SELECT * FROM atable WHERE id = ?";
PreparedStatement ps = c.prepareStatement(sql);
ps.setString(1, ID);
ResultSet rs = ps.executeQuery();
if(rs == null)
rs.next();
x = rs.getString("acolumn");
I know rs.next() executes because of some debug
statements I've worked into the real code, but at
getString("acolumn") I get "Exhausted Resultset."
When I run the query by hand, it's an empty result.
. So what should I be using to check for null or 0
row count?Hi,
Your code should read:
ResultSet rs = ps.executeQuery();
if (rs.next()) {
// do whatever
}next returns a boolean that says if there was a next or not.
/Kaj -
Strange behaviour with shorthand null check
Today I had a strange occurance of a NPE with that code:
SomeType type = (person.getType() != null ? TYPE1 : _ TYPE_2);
When person is not null but person.getType() returns null, I always faced a NullPointerException. Changing the code to
SomeType type = (person.getType() == null ? TYPE2 : _ TYPE_1);
solved the problem and worked. I'd like to know if anyone here can explain this strange behaviour.
Regards
PantheraOnca
Edited by: user4174452 on 05.07.2012 22:22Of course, your right. Wrote the original code in the office and just wanted to explain the "obscurity" by pseudo code.
The original would not have been more explanatory, I think.....
(TYPE_1 and TYPE_2 were originally enums and SomeType a suitable class)
The point is that the check threw a NPE in the first case and worked in the second. You might reproduce this with arbitrary code. Will provide the Java-Version tomorrow.... -
Is there a way to check for null on a guid inside web dynpro?
Here's the pseudo code of what I want to do:
String id = wdContext.currentABCElement().getGuid();
if(id == null)
//Do Insert on the Entity
else
//Do Update on the Entity
Thanx,
MaheshI solved it using the following to check if the object is new or existing:
wdContext.currentXYZElement().modelObject().getKey().isLocalKey()
Thanx,
Mahesh -
HP link is broken for checking tested pc's with Windows 10
When you go here http://support.hp.com/us-en/product/HP-ENVY-Phoenix-810-400-Desktop-PC-series/7477706/model/7485498/document/c04758308/ Click on "Alert" top right corner and then "View Article" And then click the link here http://www.hp.com/windows10upgradenow The link is broke and you cannot see what systems HP tested drivers for with Windows 10.
yeah I also noticed that. I went on chat support and asked for a case manager to call me back so I can complain about it because was st*p*d as a rock, didn't understand anything I told him although I forwarded him the exact link to try and see for himself.. hopefully it will be fixed soon as I want to see if my laptop is eligible or not.
Maybe you are looking for
-
I just downloaded iTunes from the website, and I even have a friend that has the update saoftware. I tried both, but my iTunes wont open, even with an error message. It's beginning to bug me, since I wish to change many of the songs on my iPod, but I
-
Using a varialble in a table PL/SQL
Good day how will I execute a PLSQL with a tablename as a variable? example: a = ename b = emp select a from b ? something like these? I want to use a variable for the column name and the table name is this possible in APEX 4? thanks
-
If I am using my Apple TV without hdmi cords do I always lose hdcp playback? If not, which adapters or cords must I purchase so my Apple TV still works opitmallly?
-
Something in Disc Drive?
Hello. I have a Macbook 4,1 for over a year now and I have been having some problems with the drive. I had it in my backpack and when I got it out in class, I noticed something blue in the drive. I didn't have anything that could remove it right then
-
OS Migration from Windows 2003 cluster to Windows 2008 R2 MSCS
We want to go for windows 2008 R2 server ( 64-Bit) MSCS by migrating from windows 2003 Cluster ( 32- Bit) . We are running on ECC 6.0 and oracle 10.2.0.2 DB. Is it possible ? Is it traditional System copy if so how to migrate the cluster ? Regards