I'm a newbie here, and hopefully I can explain this thoroughly.
Working through Grails in Action book, Second Edition using the Groovy Grails Tool Suite - GGTS (aka Spring Tool Suite - STS).
GGTS release 3.6.4. Grails version 2.4.4
I'm still on Chapter 1. By this time, I've added several 'quotes' to my database. When I do a "println Quote.count()" through the Grails Console I see I have 4 quotes.
I try to run my random GSP and receive the following error:
Line | Method
->> 7 | doCall in C:/Users/donahujc/Documents/workspace-ggts-3.6.4.RELEASE/qotd/grails-app/views/quote/random.gsp
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Caused by NullPointerException: Cannot get property 'content' on null object**
->> 7 | doCall in C__Users_donahujc_Documents_workspace_ggts_3_6_4_RELEASE_qotd_grails_app_views_quote_random_gsp$_run_closure2_closure4
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 10 | run in C__Users_donahujc_Documents_workspace_ggts_3_6_4_RELEASE_qotd_grails_app_views_quote_random_gsp
| 198 | doFilter in PageFragmentCachingFilter.java
| 63 | doFilter in AbstractFilter.java
| 1142 | runWorker in java.util.concurrent.ThreadPoolExecutor
| 617 | run in java.util.concurrent.ThreadPoolExecutor$Worker
^ 745 | run . . . in java.lang.Thread
What this is telling me is that my call is pointing to nothing. So, I go into the DBConsole back end, and sure enough, my Quote table (which contains Content and Author) isn't there.
How's that possible, when my DataSource.groovy file was changed to the following:
// environment specific settings
environments {
development {
dataSource {
dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
url = "jdbc:h2:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
I changed "create-drop" to "update" and removed reference to memory (mem:).
I know the data is there, because I can use the Grails Console to query it.
The complicated part is that I can't go through the book all at once. So I've had to close and re-launch GGTS multiple times over several days. I thought that re-running the app would re-initialize the table, but that doesn't seem to be the case.
How can I get this table initialized? I tried adding a new quote. (Somehow my index went from quote #4 to quote #33.) But still no table for my GSP to pull from.
I'm just at a loss at how to get this table (and the data that's in there somewhere) initialized. This is something I'm going to have consistent problems with, because I'll be constantly closing/re-opening GGTS.
Help
EDIT: (Adding more of my code)
Quote Controller:
def random(){
def allQuotes = Quote.list()
def randomQuote
if (allQuotes.size() > 0){
def randomIdx = new Random().nextInt(allQuotes.size())
}else {
randomQuote = new Quote(author:"Anonymous",
content:"Real Programmers double peace out on quiche")
}
[quote:randomQuote]
}
Random.gsp
<html>
<head>
<title>Random Quote</title>
</head>
<body>
<div id="quote">
<q>${quote.content}</q>
<p>${quote.author}</p>
</div>
</body>
</html>
Quote.groovy
class Quote {
String author
String content
Date created = new Date()
static constraints = {
}
}
Everything has worked fine until now. I know my data is in the DB because I can query it from the Grails Console. But DB console doesn't even show my Quote table :(