[英]Grails: using java class from controller
*UPDATED *更新
I am new to grails and stack. 我是新来的grails和stack。 I am trying to instantiate a java class in a controller. 我正在尝试在控制器中实例化Java类。 My .java file is in the /src folder. 我的.java文件在/ src文件夹中。 I have tested the class outside of grails and it works fine. 我已经在grails之外测试了该类,并且效果很好。 However, when I try to instantiate it from a controller I get a null pointer exception. 但是,当我尝试从控制器实例化它时,会出现空指针异常。 My relevant code: 我的相关代码:
package matrices
import org.springframework.dao.DataIntegrityViolationException
class MFController {
static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
def index() {
MatrixFactorization m = new MatrixFactorization()
def answer = m.getAnswer().toString()
return answer
}
My trace: 我的踪迹:
Message: null
Line | Method
->> 45 | run in matrices.MatrixFactorization
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 36 | <init> in ''
| 10 | index . . in matrices.MFController$$ENtqy0Aa
| 195 | doFilter in grails.plugin.cache.web.filter.PageFragmentCachingFilter
| 63 | doFilter in grails.plugin.cache.web.filter.AbstractFilter
| 1110 | runWorker in java.util.concurrent.ThreadPoolExecutor
| 603 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^ 722 | run in java.lang.Thread
MatrixFactorization class 35-45 MatrixFactorization类别35-45
public MatrixFactorization() {
double[] l1 = { 5, 3, 0, 1 };
double[] l2 = { 4, 0, 0, 1 };
double[] l3 = { 1, 1, 0, 5 };
double[] l4 = { 1, 0, 0, 4 };
double[] l5 = { 0, 1, 5, 4 }; //45
Thanks in advance 提前致谢
The problem is at line 45 in the run
method of the MatrixFactorization
class as the 1st line of the stacktrace shows. 问题出在MatrixFactorization
类的run
方法的第45行,如stacktrace的第一行所示。 The class is instantiating fine, but it's failing internally. 该类实例化良好,但是在内部失败。 Is there some missing configuration or setup before calling getAnswer()
? 调用getAnswer()
之前是否缺少某些配置或设置?
Glad this worked for you. 很高兴为您工作。 Here's the comment posted as an answer too. 这也是发布作为答案的评论。
Is the Java class in a package? Java类在包中吗? I am assuming matrices because your code snippet doesn't show an import. 我假设使用矩阵,因为您的代码段未显示导入。 I have experienced issues with accessing classes in the default package. 我在访问默认程序包中的类时遇到问题。 Try putting it under a test package, if not already, and then import it in your controller. 尝试将其放在测试包下(如果尚未安装),然后将其导入控制器中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.