Note that there are some explanatory texts on larger screens.

plurals
  1. POright way of testing for optional field values
    primarykey
    data
    text
    <p>While writing a method in my app which uses <code>playframework</code>,I need to get user input for <code>Address</code> fields and search db for a matching one.If I can't find a matching address ,I have to create a new Address.</p> <p>Here only <code>addressLine1</code> and <code>country</code> are the required fields. A user can ignore the addressLine2.</p> <p>while taking input from the html form ,the textfields for optional fields return an empty string.So,to test the creation of Address ,I decided to create a <code>Map&lt;String,String&gt;</code> to be passed to the <code>POST</code> method.. I tried</p> <pre><code>addrparams = new Map&lt;String,String&gt;(); addrparams.put("addressline1","clayton st"); addrparams.put("country","US"); </code></pre> <p>This caused nullpointer exception when the jpql query tried to bind values fro the map for the missing options fields.</p> <pre><code>String query="select distinct a from Address a where a.addressLine1=:addressline1 and a.addressLine2=:addressline2 and a.country=:country"; Address address = Address.find(query).bind("addressline1",addressline1).bind("addressline2",addressline2).bind("country", country).first(); .. </code></pre> <p>I solved this by putting empty strings for all optional fields</p> <pre><code>addrparams = new Map&lt;String,String&gt;(); addrparams.put("addressline1","clayton st"); addrparams.put("addressline2",""); addrparams.put("country","US"); </code></pre> <p>I hope this is the correct way to do this..If someone can point out better approach at testing such situations,it would help me a lot</p> <p>The Address class</p> <pre><code>@Entity public class Address extends Model { @Required String addressLine1; String addressLine2; @Required String country; ... } </code></pre> <p><strong>update:</strong> The stacktrace is <a href="http://pastebin.com/RnyRgdL9" rel="nofollow">here</a></p> <p>The Account.java:207 where nullptr exception occurs,is this line</p> <pre><code>Address address = Address.find(query).bind("addressline1",addressline1).bind("addressline2",addressline2).bind("country", country).first(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload