• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Method threw 'java.lang.NullPointerException' exception. Cannot evaluate java.time.LocalDateTime.toS

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my android app with kotlin, i use a login interfece which I put Email and Password. After a success authetication, I want to stock this User with SharedPreferences. The User has as attribute: birthday,lastlogin and registrationdate of type of "LocalDateTime" and others of type String. The following function, is to set the user in sharedpreferences with keyvalue "lastUser" :

The problem is here as the following with the localdatetime:

 
Marshal
Posts: 14338
237
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you checked which of those fields are null?
 
Maha Sakka
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
only these attributes of type of  "LocalDateTime"
 
Junilu Lacar
Marshal
Posts: 14338
237
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Maha Sakka wrote:only these attributes of type of  "LocalDateTime"


Well then doesn't it make sense that if these fields are null then you'll get a NullPointerException if you try to access any methods on them, like toString()? Remember, toString() will be called if you try to display an object with print(), like if you write print(birthday) and birthday is null, then the NPE will be thrown as a result of trying to call birthday.toString() within the implementation of print()

I wouldn't be surprised if something similar to that is happening with Gson and calling the toJson(user) method.
 
Maha Sakka
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you didn't understand me, these attributes birthday,lastlogin and registrationdate is not null at the begining, otherwise when get the information with WS.
But, when i use the Gson library it becames empty field.
The following pictures take it when i debug the project
Capture-du-2019-10-23-14-10-21.png
[Thumbnail for Capture-du-2019-10-23-14-10-21.png]
after get the information from ws
Capture-du-2019-10-09-11-04-25.png
[Thumbnail for Capture-du-2019-10-09-11-04-25.png]
after using json library
 
Rancher
Posts: 4315
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, the first step is to set up a small test code that uses that json String and the Gson library and see where gson is going wrong (or, more likely, where that json is going wrong).
 
Maha Sakka
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the problem with "localdatetime" type with Json
 
Junilu Lacar
Marshal
Posts: 14338
237
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Maha Sakka wrote:The following pictures take it when i debug the project


The pictures are incomplete. You should just copy-paste the text directly into your reply.
 
Junilu Lacar
Marshal
Posts: 14338
237
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you calling that function? The user parameter is declared as a nullable UserData object. Are you sure the function isn't getting called with user == null?
 
Maha Sakka
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote: Are you sure the function isn't getting called with user == null?


I debug my project and the user is not null, the problem in localdatetime library, when i use Gson the attribute of type localdatetime gone and became empty
the problem is here

 
Dave Tolls
Rancher
Posts: 4315
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what is the value of "user" being passed into that call to toJson?
 
Maha Sakka
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:And what is the value of "user" being passed into that call to toJson?


user is of type UserData as the following model :
 
Junilu Lacar
Marshal
Posts: 14338
237
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you declared all those fields as val then they cannot be set to any other value than what you've initialized them to, null. You must use var instead.

Ignore that, I guess that just says it will be null by default if no value is provided when the class is instantiated.
 
Junilu Lacar
Marshal
Posts: 14338
237
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you declare a data class that way, you've essentially defined a Null Object

Again, ignore this comment. The "= null" specifies the default if a value is not explicitly provided to the constructor.
 
Junilu Lacar
Marshal
Posts: 14338
237
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems you're not clear on how to define Kotlin data classes (<-- click on that link to see search results). You might want to review some of the links that search produces.
 
Maha Sakka
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:It seems you're not clear on how to define Kotlin data classes (<-- click on that link to see search results). You might want to review some of the links that search produces.


No, but this class generated automatically by swagger from ws and i can't change it  
the description of My problem is as the following  :
when I get the user of type of UserData from the Ws, all the fileds even though of type of localdatetime are filled with value (all from ws)
But when i use Gson all the attributes of date will be empty  .
 
Junilu Lacar
Marshal
Posts: 14338
237
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Maha Sakka wrote:
when I get the user of type of UserData from the Ws, all the fileds even though of type of localdatetime are filled with value (all from ws)
But when i use Gson all the attributes of date will be empty  .


What is happening in between you getting the user data from the web service and the call to Gson.toJson(). Have you tried printing out the value of user just before you call .toJson(user)? What does it display?
 
Maha Sakka
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What is happening in between you getting the user data from the web service and the call to Gson.toJson(). Have you tried printing out the value of user just before you call .toJson(user)? What does it display?

the function above, when i debug my app, the value of user as following before use Gson library :

after the use of json library :
 
Dave Tolls
Rancher
Posts: 4315
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK.
So, using Gson to serialise this object:

using this code:

Gives this output:
{"ldt1":{"date":{"year":2019,"month":10,"day":24},"time":{"hour":14,"minute":35,"second":25,"nano":1080000}},"someName":"Foo","someInt":123}

So, what version of Gson do you have (mine is 2.6.2)?
 
Maha Sakka
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use kotlin as language , and the verion of Gson is 2.8.5
i want all the object parse it with json not only the date
 
Junilu Lacar
Marshal
Posts: 14338
237
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might try looking through some of the results of this search: GSon issues with LocalDateTime

The suggestion I've seen is to register a GSon TypeAdapter. See this one: https://www.javaguides.net/2018/10/gson-custom-serialization-and-deseriliazation-examples.html
 
Maha Sakka
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i didn't found any solution up to now  
 
Dave Tolls
Rancher
Posts: 4315
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kotlin won't make much difference as it's still the JVM under the hood, and it's the same Gson library.

And I would still do this as a simple standalone (like the bit of code I did above, but do it in Kotlin) and recreate the bug.
Also log the output when you do that (output it to the command line).  Don't debug.  That removes one possible point of failure.

if that doesn't recreate the issue then you know it isn't Gson, and you'll then have to log the values in your actual code, again not debug but actually log the values using a proper logger.

Until you can duplicate the issue in a simple standalone environment you are simply guessing at the problem.
 
These are the worst of times and these are the best of times. And this is the best tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!