• Post Reply Bookmark Topic Watch Topic
  • New Topic

Replacing null string values in array with blank strings  RSS feed

 
Randy Maddocks
Ranch Hand
Posts: 182
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I am trying to replace string elements in an Array containing null values with blank strings. The reason is to avoid a nullpointerexception while iterating through the Array when it comes to a null value. The values from this Array are being displayed in a primefaces form. Either I am misunderstanding how to properly iterate over an Array, or I am doing something wrong in my code (as attached). I am using NetBeans 8.0.2, JDK 1.7_0_71, Primefaces 5.0, JBoss EAP 6.21. If anyone can help it would be greatly appreciated!

 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What problems are you having? Do you get an error? Can you post the stack trace?
 
Tim Cooke
Marshal
Posts: 4044
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The better solution would be to prevent a null ever getting into your array.
 
Randy Maddocks
Ranch Hand
Posts: 182
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Knute,

Many thanks for your quick reply!

I get an NPE as soon as it reaches the first line of code where it tries to set a value that is null. Below is the stack trace (my apologies that it is so long), and the line of code where it is reporting the NPE:

11:22:18,709 WARNING [javax.enterprise.resource.webcontainer.jsf.lifecycle] (http-/127.0.0.1:8443-4) #{foreignPartyController.onSearchButtonClick()}: java.lang.NullPointerException: javax.faces.FacesException: #{foreignPartyController.onSearchButtonClick()}: java.lang.NullPointerException
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:117) [jsf-impl-2.1.19-redhat-2.jar:2.1.19-redhat-2]
at javax.faces.component.UICommand.broadcast(UICommand.java:315) [jboss-jsf-api_2.1_spec-2.1.19.1.Final-redhat-1.jar:2.1.19.1.Final-redhat-1]
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:786) [jboss-jsf-api_2.1_spec-2.1.19.1.Final-redhat-1.jar:2.1.19.1.Final-redhat-1]
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1251) [jboss-jsf-api_2.1_spec-2.1.19.1.Final-redhat-1.jar:2.1.19.1.Final-redhat-1]
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) [jsf-impl-2.1.19-redhat-2.jar:2.1.19-redhat-2]
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) [jsf-impl-2.1.19-redhat-2.jar:2.1.19-redhat-2]
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) [jsf-impl-2.1.19-redhat-2.jar:2.1.19-redhat-2]
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) [jboss-jsf-api_2.1_spec-2.1.19.1.Final-redhat-1.jar:2.1.19.1.Final-redhat-1]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:295) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:149) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:499) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.3.0.Final-redhat-14.jar:7.3.0.Final-redhat-14]
at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.3.0.Final-redhat-14.jar:7.3.0.Final-redhat-14]
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:169) [jboss-as-web-7.3.0.Final-redhat-14.jar:7.3.0.Final-redhat-14]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:145) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:97) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:102) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:336) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:653) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:920) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_71]
Caused by: javax.faces.el.EvaluationException: java.lang.NullPointerException
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101) [jboss-jsf-api_2.1_spec-2.1.19.1.Final-redhat-1.jar:2.1.19.1.Final-redhat-1]
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:101) [jsf-impl-2.1.19-redhat-2.jar:2.1.19-redhat-2]
... 23 more
Caused by: java.lang.NullPointerException
at ca.empire.fatca.entity.ForeignParty.setUpdatedBy(ForeignParty.java:527) [classes:]
at ca.empire.fatca.entity.ForeignPartyController.getSearchCriteriaData(ForeignPartyController.java:1686) [classes:]
at ca.empire.fatca.entity.ForeignPartyController.onSearchButtonClick(ForeignPartyController.java:2082) [classes:]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_71]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_71]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_71]
at java.lang.reflect.Method.invoke(Method.java:606) [rt.jar:1.7.0_71]
at org.apache.el.parser.AstValue.invoke(AstValue.java:258) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40) [weld-core-1.1.16.Final-redhat-1.jar:1.1.16.Final-redhat-1]
at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50) [weld-core-1.1.16.Final-redhat-1.jar:1.1.16.Final-redhat-1]
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) [jsf-impl-2.1.19-redhat-2.jar:2.1.19-redhat-2]
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87) [jboss-jsf-api_2.1_spec-2.1.19.1.Final-redhat-1.jar:2.1.19.1.Final-redhat-1]
... 24 more


The entity object in the code snippet below is an instance of an Entity class, in which the getter and setter for the above value (updatedBy) are contained.

Line 527 referenced in the stack trace is the setter method for this particular value in the Entity class.

Line 1686 listed in the stack trace (shown in bold above in the stack trace) is as follows:



That line of code doesn't tell you much, I know, but if it helps, I know if I was to comment out that line it would simply produce an NPE error wherever it reaches the next null value in the iteration of the array. I am thinking if I can somehow put in logic to tell it to simply replace any null values with blank strings it will prevent the NPE from happening. Or maybe that is the wrong approach? Thanks again for your help.
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does ForeignParty.java:527 look like?
 
Randy Maddocks
Ranch Hand
Posts: 182
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like this:



Interestingly enough, if I remove the toUpperCase() it goes on to the next null value and does not NPE on this updatedBy value. But I don't see how adding something as common as toUpperCase() should make a difference? Although I have heard that you should not "tweak" setter methods.
 
Randy Maddocks
Ranch Hand
Posts: 182
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just thought of something Knute - it is because java cannot, or does not, put the value null in uppercase? The reason I use toUpperCase() is because strings written to an Oracle database have to be capitalized. To be honest, I tried using a primefaces converter tag and the accompanying code in the backing bean but didn't very far - maybe I didn't give it enough of chance. But the nice thing about using toUpperCase() on the string setter values is I know it will cover all of them no matter what instance of the Entity class I use when I call those setters. Thanks again for your help!
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're still getting a null passed in. Do you know what is causing the nulls in the first place?
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
strings written to an Oracle database have to be capitalized

Well, not every Oracle database. Maybe that's a restriction on your particular DB.
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this code:

inside of the this while loop?
 
Randy Maddocks
Ranch Hand
Posts: 182
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I didn't mean to imply every Oracle database has to have strings in CAPS, just our business area has set that as a requirement.

If I check the database those values are in fact null. That's fine, we know there will be some null values in any given record.

Yes, that code is inside the while loop. I think part of the problem is I have the while loop, and then inside that I have nested the for loop - I never was good with nesting loops. But that logic could be part of the problem.
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a null is a legitimate value, then you have to program around it.


I'm assuming that this.updatedBy shouldn't be null, but if you're going to write back to the DB, can the DB updatedBy field be ""? Will that cause problems? If so, then you're going to have to test for null everywhere this.updatedBy is used.

Can you see why people say that nulls are evil?
 
Randy Maddocks
Ranch Hand
Posts: 182
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you see why people say that nulls are evil?


I have had a proverbial love/hate relationship with nulls. On good days they are my friend, they allow me to quickly identify gaps in my code...on bad days, such as with NPEs, I tear my hair out trying to nail down the cause of an NPE, or why a value is null when I know darn well it shouldn't be.

I hadn't thought of adding that IF statement to the setter, but I will definitely try it.

On the topic of Setter methods, what are your thoughts on what seems to be a popular suggestion to not put too much into a Setter method? Is it as risky or just plain old inefficient as some claim it is?
 
Randy Maddocks
Ranch Hand
Posts: 182
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are on to something - I put in your logic in the setter and the stack trace has now moved to the next line of code with a value that is null. I am just updating the rest of the handful of setter methods and will see what happens.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nulls are like wasps when you are in the kitchen with honey. You can kill them by swatting them. You can keep a very close eye on them. You can put a bowl over them so they can't get out to cause trouble, or you can let them sting you where it really hurts
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree that you shouldn't put too much code in a setter, but you have to be sure the data is valid.
 
Randy Maddocks
Ranch Hand
Posts: 182
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie

Campbell Ritchie

Nulls are like wasps when you are in the kitchen with honey. You can kill them by swatting them. You can keep a very close eye on them. You can put a bowl over them so they can't get out to cause trouble, or you can let them sting you where it really hurts


In other words, handle them with care, or at the very least, respect what they are capable of causing is not handled properly!

Knute Snortum

I agree that you shouldn't put too much code in a setter, but you have to be sure the data is valid.


I agree - I would rather be 110% sure the data is valid, especially where business decisions will be made based on the data. Still working away at those setters, but I really appreciate the help!
 
Randy Maddocks
Ranch Hand
Posts: 182
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That worked! So nice to see the returned search results!!

I have only been a member of this site for a little while, but before becoming one I had had many a problem solved over the years from consulting the various forums on this site. This is truly a wonderful site. Thanks again for replying so quick and for your help. I had nightmarish thoughts of having to go through multiple lines of code trying to put in some "hokey" logic to try and resolve this. Thankfully, your suggestion has saved the day. Cheers!
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randy Maddocks wrote: . . . handle them with care, . . .
Not at all. Beat them into submission

If you have a wasp, you would never let it think it is boss. When you have nulls, make sure they know who is boss. And make sure it's you.
 
Randy Maddocks
Ranch Hand
Posts: 182
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie

Not at all. Beat them into submission

If you have a wasp, you would never let it think it is boss. When you have nulls, make sure they know who is boss. And make sure it's you.


Words of wisdom to live by on the subject of nulls...thanks Campbell!!
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • Swat them. If there is somewhere you might have a null, don't let it in. If somebody tries to pass you a null, throw an Exception. If you have an array, fill it so there aren't any nulls in it. Initialise every instance field in a constructor so there is no risk of finding a null.
  • Watch them. If there are places where nulls are inevitable (e.g. leaf nodes in trees), make sure you always check for them. That is where if (obj == null)... is really useful.
  • Put a bowl over them. You might not be able to get rid of all the nulls in an array, but if the array is hidden away inside an ArrayList, you can keep a bowl over the nulls because the List can stop you accessing that part of the array where there are nulls. But remember you can add null to most Lists, so you need to take account of that.
  • Let them sting you where it really hurts? Not bl**dy likely!
  • Maybe nulls are like nettles: and how do you handle a nettle?
     
    Randy Maddocks
    Ranch Hand
    Posts: 182
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Maybe nulls are like nettles: and how do you handle a nettle?


    If it's the same plant I am thinking of (and there can't be too many), handle carefully. You can cook it for it's nutrients; but when carelessly handled it stings! Nettles

    Really appreciate the tips! Duly noted!
     
    Campbell Ritchie
    Marshal
    Posts: 56541
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Grasp the nettle firmly. Or here. It only stings if handled gently.
     
    Randy Maddocks
    Ranch Hand
    Posts: 182
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Here in Canada this year, specifically in my area, wild parsnip has spread like wildfire. Nasty stuff - if skin comes in contact with it, and then skin is exposed to sun it burns. Or worse yet, to quote said article:

    If the sap gets into the eyes, it may cause temporary or permanent blindness.


    Not the kind of plant you bring home to mom......
     
    Darryl Burke
    Bartender
    Posts: 5167
    11
    Java Netbeans IDE Opera
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Knute Snortum wrote:
    If you're on Java 8 you can shorten that to
     
    Randy Maddocks
    Ranch Hand
    Posts: 182
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I am using Java 7, but very cool. Sort of a twist on



    Thanks Darryl!
     
    Jesper de Jong
    Java Cowboy
    Sheriff
    Posts: 16060
    88
    Android IntelliJ IDE Java Scala Spring
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Darryl Burke wrote:If you're on Java 8 you can shorten that to

    That should be:

    You have to use Optional.ofNullable - Optional.of throws a NullPointerException if you pass it null.
     
    Randy Maddocks
    Ranch Hand
    Posts: 182
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Duly noted, thanks Jesper!
     
    Campbell Ritchie
    Marshal
    Posts: 56541
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Wild parsnip, or its close relative Heracleum montezzagianum(=giant hogweed), which is invasive here in UK, cause just as much damage if grasped firmly. Quite different from nettles.

    At least nettles are native to this country.
     
    Darryl Burke
    Bartender
    Posts: 5167
    11
    Java Netbeans IDE Opera
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jesper de Jong wrote:You have to use Optional.ofNullable - Optional.of throws a NullPointerException if you pass it null.


    Thanks Jesper, I hadn't noticed that.
     
    Randy Maddocks
    Ranch Hand
    Posts: 182
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie

    At least nettles are native to this country.


    Yes, seems more and more now countries are seeing invasive species, and dealing with them is a task in itself. In Canada zebra mussels are another invasive species, smothering a lot of native plant life and causing a lot of grief for local agencies trying to contain it.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!