Script Must Not be Null or Empty

I’ve just started learning Spring / Spring Boot so I’m at that stage where I’m making all the stupid beginner mistakes. In the past I used to catalogue the problems I hit, especially when learning a new technology. A few people told me they found it really useful so I’ve decided to start again. This particular issue I ran up against didn’t take long to figure out but the message on the exception isn’t as clear as it could be so I thought it was worth documenting.

I added a SQL script to my new Spring Boot project in order to get a bit of test data into the database while I’m learning. It only had a single line of SQL in the file and I was sure the syntax was correct. Unfortunately, when I started the application I got the following stack trace (reduced for clarity):

        ...snip...
Caused by: org.springframework.jdbc.datasource.init.UncategorizedScriptException: Failed to execute database script from resource [URL [file:/...snip.../data.sql]]; nested exception is java.lang.IllegalArgumentException: 'script' must not be null or empty
	at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:514) ~[spring-jdbc-4.3.9.RELEASE.jar:4.3.9.RELEASE]
	at org.springframework.jdbc.datasource.init.ResourceDatabasePopulator.populate(ResourceDatabasePopulator.java:238) ~[spring-jdbc-4.3.9.RELEASE.jar:4.3.9.RELEASE]
	at org.springframework.jdbc.datasource.init.DatabasePopulatorUtils.execute(DatabasePopulatorUtils.java:48) ~[spring-jdbc-4.3.9.RELEASE.jar:4.3.9.RELEASE]
	at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.runScripts(DataSourceInitializer.java:192) ~[spring-boot-autoconfigure-1.5.4.RELEASE.jar:1.5.4.RELEASE]
	...snip...
	... 92 common frames omitted
Caused by: java.lang.IllegalArgumentException: 'script' must not be null or empty
	at org.springframework.util.Assert.hasText(Assert.java:181) ~[spring-core-4.3.9.RELEASE.jar:4.3.9.RELEASE]
	at org.springframework.jdbc.datasource.init.ScriptUtils.splitSqlScript(ScriptUtils.java:171) ~[spring-jdbc-4.3.9.RELEASE.jar:4.3.9.RELEASE]
	at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:461) ~[spring-jdbc-4.3.9.RELEASE.jar:4.3.9.RELEASE]
	... 107 common frames omitted

The fact that it’s an UncategorizedScriptException does not look like a good start and I initially assumed that ‘script’ was a column that had somehow worked it’s way into my query. As is so often the case with cryptic exception messages like this Google didn’t turn up anything particularly useful.

It turned out the problem was actually just me not paying attention. I’d entered my SQL into a query window in IntelliJ rather than into the data.sql file. This meant the data.sql file was completely empty and this in turn caused an assertion to fail in the ScriptUtils class (specifically it’s trying to split the data.sql file into individual queries). Putting the SQL into the correct file solved the issue.

It would be nice if the ScriptUtils class could provide a better error message, something like “The provided SQL script must not be null or empty”, those few extra words would clear up any ambiguity. I’m actually rather surprised it chokes and dies on an empty file at all, surely if the file is empty just don’t do anything.