MVC 中通过线程从内部类调用外部类
我正在使用 MVC 模型,并尝试在控制器中创建一个线程。当我在内部类 run() 中时,我需要获得正确的模型,但它抛出一个空指针。
以下是从外部控制器创建内部类和线程的代码:
Thread thread = new Thread(new runWithThread(OpsSec, AmToChange, AgentID, balance, currency, selected_account_obj));
thread.start();
在 runWithThread 中,我尝试获取正确的模型。 AMModel 是 Model 类,withdraw 是其中的一个方法。 getModel 是在我正在扩展的抽象控制器中定义的(实现继承)。
((AMModel)getModel()).withdraw(10, "USD");
它在外部类中有效,但在内部类中无效,我不确定为什么我使用 ((AMModel)getModel()) 获取空指针。任何帮助将不胜感激。 谢谢
I am using a MVC model and am trying to create a thread in the controller. When I am in the inner class run() I need to get the correct model but it is throwing a null pointer.
Here is the code to create the inner class and thread from the outer controller:
Thread thread = new Thread(new runWithThread(OpsSec, AmToChange, AgentID, balance, currency, selected_account_obj));
thread.start();
Inside the runWithThread I try to get the correct Model. AMModel is the Model class and withdraw is a method inside it. getModel is defined in the abstract controller I am extending(implementation inheritance).
((AMModel)getModel()).withdraw(10, "USD");
It works in the outer class but not in the inner class and I am not sure why I am getting the null pointer with the ((AMModel)getModel()). Any help would be appreciated.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我意识到了错误。我在控制器外部类和 runWithThread 内部类中都有“extends AbstractController”。我正在使用 Rational Arch,它没有标记任何内容,因此我没有注意到该错误。
I realized the error. I had "extends AbstractController" in both the controller outer class and in runWithThread inner class. I am using Rational Arch and it didn't flag anything so I didn't notice the error.
我知道这已经很老了,但请尝试
AMModel.this.withdraw(10, "USD");
。这是一个通用示例:调用
inner.test()
后,variable
将是1
。I know this is pretty old, but try
AMModel.this.withdraw(10, "USD");
. Here's a generic example:After calling
inner.test()
,variable
would be1
.