java IllegalArgumentException 发生:加载需要加载的 id?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30415738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IllegalArgumentException occured : id to load is required for loading?
提问by Ravi
When I try to access the findById it is showing the following:
当我尝试访问 findById 时,它显示以下内容:
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) # ~~~~
下面是我的路由文件,我不明白为什么会出现错误和非法参数异常。# Routes # 这个文件定义了所有的应用路由(优先级高的路由优先)# ~~~~
# 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}
回答by Mon Calamari
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:
IllegalArgumentException
抛出是因为 id 为空。确保您通过请求传递正确的值。routes
如下映射文件中的控制器方法将防止传递空值:
GET /user/{id} Aplication.showUser
GET /user/{id} Aplication.showUser
回答by Nick Volynkin
The value of id
variable that you pass to User1.findById
is something unacceptable.
id
您传递给变量的值User1.findById
是不可接受的。
Maybe it's a negative number. Read documentation to User1.findById
to find requirements to the id
variable.
也许这是一个负数。阅读文档以User1.findById
查找对id
变量的要求。
Try this code to check the value of id before calling findById(id)
:
尝试使用此代码在调用之前检查 id 的值findById(id)
:
public static void showUser(Long id)
{
System.out.println("Application,showUser(id = " + id + " );"
User1 user= User1.findById(id);
render(user);
}