MVC 中通过线程从内部类调用外部类

发布于 2024-11-04 20:24:25 字数 518 浏览 10 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酸甜透明夹心 2024-11-11 20:24:25

我意识到了错误。我在控制器外部类和 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.

甜柠檬 2024-11-11 20:24:25

我知道这已经很老了,但请尝试 AMModel.this.withdraw(10, "USD");。这是一个通用示例:

class Outer
{
    class Inner
    {
        public void test()
        {
            Outer.this.variable = 1;
        }
    }
    public int variable = 0;
    private Inner inner;
}

调用 inner.test() 后,variable 将是 1

I know this is pretty old, but try AMModel.this.withdraw(10, "USD");. Here's a generic example:

class Outer
{
    class Inner
    {
        public void test()
        {
            Outer.this.variable = 1;
        }
    }
    public int variable = 0;
    private Inner inner;
}

After calling inner.test(), variable would be 1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文