Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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";
    }
}
like image 498
Sam Avatar asked Nov 29 '22 07:11

Sam


2 Answers

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).

like image 25
uthomas Avatar answered Dec 04 '22 11:12

uthomas


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.

like image 139
Emerson Farrugia Avatar answered Dec 04 '22 11:12

Emerson Farrugia