Closed chrisime closed 8 years ago
Hi @chrisime
This exception is thrown when url is null on one of your pages. I think it's set to null by ResourceMapper of your theme.
Grain is not very helpful with identifying the resource on which url is null, hence I've added resource location printing when this type of error happens to 0.7.0-SNAPSHOT. Please update your application.properties to have the line: grain.version = 0.7.0-SNAPSHOT
Note, that \n (end of line character) is important at the end of application.properties.
This will help you understand what is the location of resource that has url = null. If everything will be fine I will backport this extended diagnostics into stable Grain version.
Victor
Hi @vlasenko
Thanks for your reply. Unfortunately, the error still happens in your snapshot version. The url where the NPE is thrown isn't shown. I double checked my urls and can't think of anything which is wrong.
Any ideas?
Cheers, Christian
Hi. The snapshot version was not meant to "fix this error", it will dump extended diagnostic info instead. The error is not in the Grain at all, it's in the theme code. In the stack trace from snapshot version there should be location of the resource with null URL. Please post dumped stack trace here and source code of your ResourceMapper
Closing due to not a Grain issue and inactivity
Gonna post the problem on the weekend, didn't have time. Please reopen. Thanks!
Okay, thanks for update. Looking forward to hear from you soon
Hi,
I got the following stacktrace now:
java.lang.RuntimeException: Wrong url 'null' for resource at null
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
The relevant parts of ResourceMapper.groovy
is (I modified your business theme):
...
private def customizeModels = { List resources ->
def posts = resources.findAll { it.layout == 'unternehmer' }
def isPageable = true
resources.inject([]) { List updatedResources, Map page ->
def applyPagination = { items, perPage, url, model = [:] ->
isPageable = posts.size() > perPage;
updatedResources += Paginator.paginate(items, 'posts', perPage, url, page + model)
}
switch (page.url) {
case '/':
case '/pioniere/':
applyPagination(posts, 4, page.url)
break
case ~/${site.pioniere_base_dir}.*/:
def post = posts.find { it.url == page.url }
def index = posts.indexOf(post)
def prev = index > 0 ? posts[index - 1] : null
def next = posts[index + 1]
updatedResources << (page + [prev_post: prev, next_post: next])
break
default:
updatedResources << page
}
updatedResources << [pageable: isPageable]
}
}
/**
* Customize site post URLs
*/
private def customizeUrls = { Map resource ->
String location = resource.location
def update = [:]
switch (location) {
case ~/\/pioniere\/posts\/.*/:
update.url = getPostUrl(site.pioniere_base_dir, location)
break
}
resource + update
}
...
Hi @chrisime It's difficult for me to track down where the resource with location = null is born. Can you just print the input of your resource mapper and output of your resource mapper and try to track down where the page with location = null is produced? If it's in the input to resource mapper - it's a bug inside Grain, but if it's in the output - then it's a bug inside resource mapper, since either 'location' or 'source'+'markup' property must be set on each resource produced by resource mapper.
def map = { resources ->
println 'Input pages with null location: ' + resources.findAll { !it.location }
...
def refinedResources = resources.findResults(filterPublished).collect {
customizeUrls <<
fillDates <<
resource
}.sort { -it.date.time }
def result = customizeModels << refinedResources
println 'Output pages with null location: ' + result.findAll { !it.location }
result
}
Please note, that the problem in your code is here:
updatedResources << [pageable: isPageable]
You are adding malformed resource with only one property here: [pageable: true/false] and it's location = null of course, because it's not set. You should add pageable property to existing pages instead
Hi @vlasenko,
so far 'input pages' doesn't give me any null locations. So it seems to be the output.
How can I get around adding malformed resources. I don't see what's wrong since in each case updatedResources
gets the right stuff.
Thanks!
Hi @chrisime,
I've pointed out what's wrong in your code already: https://github.com/sysgears/grain/issues/12#issuecomment-172384602
You are adding malformed resources in the line of your code pointed out in the comment above. Please see Resource representation of Grain documentation: http://sysgears.com/grain/docs/latest/#resource-representation
The Groovy map of invalid resource in your case is: {pageable: false} which is incorrect, because required keys 'location' and 'url' are absent in this map.
Hello @vlasenko,
thanks for the link, gonna try this. You can close the issue for now.
Cheers, Christian
With my current setup I get an NPE when I'm trying to generate html pages. grain doesn't tell me what's wrong about my configuration and/or urls.