Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null value set for missing fields #116

Closed
raphkr opened this issue Sep 14, 2016 · 10 comments
Closed

Null value set for missing fields #116

raphkr opened this issue Sep 14, 2016 · 10 comments
Assignees
Labels
Milestone

Comments

@raphkr
Copy link

raphkr commented Sep 14, 2016

I have fields in my code which are missing from the CSV i am trying to parse. In my parser settings I have set the default null value to be an empty string. However for the missing fields, the value being set is null.

I would like to know if this is the desired behaviour or am i doing something wrong??

@jbax
Copy link
Member

jbax commented Sep 15, 2016

Can you please provide some sample code that reproduces this issue you're having?
Thank you!

@raphkr
Copy link
Author

raphkr commented Sep 15, 2016

//TSV parser settings defined
TsvParserSettings parserSettings = new TsvParserSettings();
parserSettings.setNullValue("");
...
...
TsvParser parser = new TsvParser(parserSettings);

//Model class for CSV
@parsed(field="A")
private String a;
@parsed(field="B")
private String b;
@parsed(field="C")
private String c;

My CSV has feilds:
A\tB

So here is my scenario:
If I leave a column value (eg. A) empty then an empty string is set for that as the default null value. However if the field as defined in the model is not present in the CSV(i.e C), then the parser seems to completely ignore that and does not set the default null value as an empty string.

Let me know if you need anything more from me.

Thanks for the prompt reply!!! 👍

@jbax
Copy link
Member

jbax commented Sep 15, 2016

Thank you. The parser behaves like this by design": if nothing is parsed then you will get null. If you actually parse a column, but the value is null, then you get the nullValue.

Having said that, you can set a value for defaultNullRead in the annotations:


@Parsed(field="A", defaultNullRead="")
private String a;
@Parsed(field="B", defaultNullRead="N/A")
private String b;
@Parsed(field="C", defaultNullRead="etc")
private String c;

Hope this helps.

@jbax jbax closed this as completed Sep 15, 2016
@jbax jbax added the question label Sep 15, 2016
@raphkr
Copy link
Author

raphkr commented Sep 15, 2016

Thanks Alot.

@raphkr
Copy link
Author

raphkr commented Sep 16, 2016

Hey. I tried using the default null value parameter, however that too didnt seem to do the job.

@jbax
Copy link
Member

jbax commented Sep 16, 2016

To be fixed with version 2.2.2 (planned to be released on Monday)

@jbax jbax reopened this Sep 16, 2016
@jbax jbax added bug and removed question labels Sep 16, 2016
@jbax jbax self-assigned this Sep 16, 2016
@jbax jbax added this to the 2.2.2 milestone Sep 16, 2016
jbax added a commit that referenced this issue Sep 19, 2016
…notated attributes.

- Better handling of primitive values by assigning a default primitive value instead of null (which would throw a NPE) when nothing is parsed for the field.
@jbax
Copy link
Member

jbax commented Sep 19, 2016

Fixed. Version 2.2.2 was just released and includes this adjustment.
Thank you for using our parsers!

@jbax jbax closed this as completed Sep 19, 2016
@raphkr
Copy link
Author

raphkr commented Sep 21, 2016

Thanks.

@binhvinhmai
Copy link

Hey the issue still exists in Univocity where if the column doesn't exist, the attribute in the POJO is still set to null - defaultNullRead="" doesn't seem to be working, and so if nothing is parsed, the value is left as a null value. Has this been corrected? Sorry to bring up an old issue but I'm still facing this issue that raphkr was facing in Univocity-Parsers 2.4.1.

@jbax
Copy link
Member

jbax commented Jul 14, 2017

@binhvinhmai the unit test I originally created for this issue doesn't reveal any issues. Do you mind sharing the code you are using (or some simplified version of it) that reproduces the issue you are having?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants