Strange exception: "Name must not be null or empty!"

37,323

Solution 1

Your KeyService is calling some Spring Data JPA repository with a method findKeyByKeyString() method. That method is causing Spring Data JPA to explode because some query parameter is missing.

Put a conditional breakpoint on org.springframework.data.jpa.repository.query.StringQuery.getBindingFor(StringQuery.java:104) that matches if !StringUtils.hasText(name) to see what's going on, or review the findKeyByKeyString() method.

Solution 2

EDIT: As @Gilbertoca pointed out I indeed had to use @Param. Source of my confusion was that I used @QueryParam annotation by accident.

So if you use @Param it'll work.

Original response:

I got exactly the same and took some time while I figured out what was the problem: I used JpaRepository interface with a method annotated like

@Query(value = "SELECT * FROM table WHERE property=:something", nativeQuery = true).

As it turned out in this context named parameters are not supported. It started to work for me after I changed to:

@Query(value = "SELECT * FROM table WHERE property=(?1)", nativeQuery = true).

(brackets are important, otherwise it didn't work).

Solution 3

I had the same exception and I was missing @Param("appname") in method args:

@Query("SELECT p.preferenceId.userId FROM Preference p WHERE p.preferenceId.appName = :appname") List getAppUsers(@Param("appname") String appname);

Share:
37,323
Sam
Author by

Sam

Updated on September 07, 2020

Comments

  • Sam
    Sam over 3 years

    I'm experiencing a strange error in a Spring project, which is the following:

    SEVERE: Servlet.service() for servlet [calzoneServlet] in context with path [/calzone] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Name must not be null or empty!; nested exception is java.lang.IllegalArgumentException: Name must not be null or empty!] with root cause java.lang.IllegalArgumentException: Name must not be null or empty!

    In this project, all models are mapped to the database using hibernate and jpa. The front-end uses Twitter Bootstrap (with spring form validation, etc)

    The error occurs at different parts of the program, one of which is a controller to activate a user account (code below). To me this looks like some sort of validation error, but it isn't an error I have ever defined. Since I can't pinpoint the exact location of the error, I'll only provide the controller below. It is noteworthy that any debug messages (whether it be through the logger or just a plain old sysout) in the activateUser method do not get displayed.

    All dependencies (pom.xml) can be found here: http://pastebin.com/fs7SG0W2 Web.xml here: http://pastebin.com/vAJh29Aw The entire stacktrace can be found here: http://codepad.org/p0Yt5hi2 (on codepad, because it has horizontal scrolling)

    Does anyone have any idea why this error could be happening, or have any clue as to how I can find out why it's happening?

    @Controller
    public class ActivateAccountController {
        final Logger logger = LoggerFactory.getLogger(getClass());
        @RequestMapping(value = "/activate/{keyString}", method = RequestMethod.GET)
        public String activateUser(@PathVariable String keyString) {
            ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            KeyService keyService = (KeyService) context.getBean("keyService");
            UserService userService = (UserService) context.getBean("userService");
            User user;
            try {
                logger.error("DID I GET HERE?");
                user = keyService.findUserByKey(keyString);
            } catch (KeyNotFoundException ex) {
                return "ActivatedNotAccount";
            } finally {
                // Close the application context in every case
                context.close();
            }
            // Activate the in-memory user
            userService.activateUser(user);
            // Delete the key from the database
            keyService.deleteKey(keyString);
            // Finally, update the user in the database
            userService.updateUser(user);
            logger.info("Acticated user with ID \"{}\", First name: \"{}\", Last name: \"{}\" and username: \"{}\"",
                    user.getId(), user.getPerson().getFirstName(), user.getPerson().getLastName(), user.getUsername());
            return "ActivatedAccount";
        }
    }
    
  • Sam
    Sam about 10 years
    My @Query annotation in my DAO which extends JpaRepository wasn't correct (for the method findKeyByKeyString), and thus threw the error. Thank you!
  • Gilberto
    Gilberto almost 10 years
    Shouldn't "query parameter is missing" be a better exception description?
  • Gilberto
    Gilberto almost 10 years
    You need use @Param("paramname") in the method parameters
  • abeauchamp
    abeauchamp almost 9 years
    I accidentally left out one of @Param tags on my queries. Thanks for this answer.