开发者

troubleshooting a NullPointerException in grails

开发者 https://www.devze.com 2023-01-28 23:04 出处:网络
preface note: I\'m just starting to learn Grails, so I\'m sure there are many other problems and room for optimization.

preface note: I'm just starting to learn Grails, so I'm sure there are many other problems and room for optimization.

I've got two domains, a parent (Collection) and child (Event), in a one-to-many mapping. I'm trying to code an integration test for the deletion of children. Prior to the code in question, I've successfully created a parent and three children. The point where I'm having problems is getting a single child in preparation to delete it. The first line of my sample code is only there because of my rudimentary attempt to troubleshoot.

// lines 95-100 of my EventIntegrationTests.groovy file
// delete a single event
assertEquals("2nd Event", event2.title)     // passes
def foundEvent = Event.get(event2.id)       // no apparent problems
assertEquals("2nd Event", foundEvent.title) // FAILS (line #98)
foundEvent.delete()
assertFalse Event.exists(foundEvent.id)

The error message I'm getting is:

 Cannot get property 'title' on null object 

 java.lang.NullPointerException: Cannot get property 'title' on null object
         at edu.learninggrails.EventIntegrationTests.testEventsDelete(EventIntegrationTests.groovy:98)

What should my next troubleshooting steps be? (Since the first assertEquals passes, event2 is clearly not null, so at this point I have no idea how to troubleshoot 开发者_StackOverflow社区the failure of the second assertEquals.)


This is not evident from the code: did you persist event2 by calling save()? Get will try to retrieve it from the persistent storage (the in-memory database for example) and if the event wasn't saved, the retrieved instance will be null.

If you did save it, did the save go through OK? Calling event.save() will return false if there was something wrong while saving the item (like a validation error). Lastly, you might try calling event.save(flush:true) in case the Hibernate session doesn't handle this case as you might expect (I'm not entirely sure about this one, but it can't hurt to try).


Try to print or inspect the event2.id on line 97 and check if you actually have an id, if so check if you actually get an Event object on line 97.


I dont think you saved the parent and its children successfully. after you save, you should make sure that every object that was persisted has a non null id, in your test.

What you are seeing is you created the event2 with a title, but didnt save it. It passes the first assertion because you created it. When you do the get, null is returned because your save failed.

in general for DAO integration tests i do the following

  1. Setup -- create all objects Ill use in the test.
  2. Save -- assert that all ids on saved objects are NOT null.
  3. Clear the hibernate session -- this is important because if you don't do it, objects can be in the session from the previous operations. In your real world scenario, you are probably going to start with a find, i.e. an empty session. In other words, you are not going to start with anything in the session. If you are you need to adjust this rule so that the session in the test, when you start the actual testing part, is the same as the session of the code in the wild
  4. Load the objects on which you want to operate and do what you need to do.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号