Ravi
Ravi

Reputation: 31

IllegalArgumentException occured : id to load is required for loading?

When I try to access the findById it is showing the following:

IllegalArgumentException "id to load is required for loading"

Here's my code:

package controllers;
import play.*;
import play.mvc.*;
import java.util.*;
import models.*;

public class Application extends Controller {

    public static void index() {

       render();
    }
    public static void saveUser(String name)
    {
        User1 user =new User1(name);
        String res = "";
        if( user.save()!=null){
            res="Stored Successfully";
        }
        else{
            res="Failed to store";
        }
        render(res);
    }
    public static void showUser(Long id)
    {
       User1 user=  User1.findById(id);
       render(user);
    }

}

and below is my routes file i don't understand why the error is coming and illegal argument exception. # Routes # This file defines all application routes (Higher priority routes first) # ~~~~

# Home page
GET     /                                       Application.index

# Ignore favicon requests
GET     /favicon.ico                            404

# Map static resources from the /app/public folder to the /public path
GET     /public/                                staticDir:public

# Catch all
*       /{controller}/{action}                  {controller}.{action}

Upvotes: 2

Views: 21101

Answers (2)

Mon Calamari
Mon Calamari

Reputation: 4463

IllegalArgumentException is thrown because the id is null. Make sure you pass correct value with the request. Mapping the controller method in routes file as follows would prevent from passing null:

GET /user/{id} Aplication.showUser

Upvotes: 5

Nick Volynkin
Nick Volynkin

Reputation: 15119

The value of id variable that you pass to User1.findById is something unacceptable.

Maybe it's a negative number. Read documentation to User1.findById to find requirements to the id variable.

Try this code to check the value of id before calling findById(id):

    public static void showUser(Long id)
    {
       System.out.println("Application,showUser(id = " + id + " );"
       User1 user=  User1.findById(id);
       render(user);
    }

Upvotes: 0

Related Questions