如何从mouseClicked传递参数?

时间:2015-10-23 11:08:32

标签: java

我正在使用java3D并希望将点击的项目的ID传递给我的班级。但它没有成功。它可以打印ID,也可以将其传递给字符串文本。但它在行game.btClicked(text)中表示nullPointerException;我如何将ID传递给我的班级?

public class GUI extends JFrame implements ActionListener, MouseListener {

  private Game game;
  private String text;

  public GUI(Game game /*...*/) {

    this.game = game;
    //.......
  }

  //.......
  public void mouseClicked(MouseEvent e) {
    pickCanvas.setShapeLocation(e);
    Primitive pickedShape = null;
    PickResult result = pickCanvas.pickClosest();

    if (result != null) {
      pickedShape = (Primitive) result.getNode(PickResult.PRIMITIVE);
    }

    //actions to be carried out when object is clicked
    if (pickedShape != null) {
      System.out.println("clicked: " + pickedShape.getName());
      String text = pickedShape.getName();
      System.out.println(text);
      game.btClicked(text);
    } else {
    }
  }
}

公共课游戏{

public Game(){

// .......

}

public void btClicked(String text) {
    spielfeld.disableAll();
    int buttonNr = Integer.parseInt(text);
    System.out.println("step: "+step+ " nr: "+buttonNr);
   // .......

} }

以下是堆栈跟踪:

点击:38

38

线程中的异常“AWT-EventQueue-0”java.lang.NullPointerException

at spiel.Game.btClicked(Game.java:403)

at spiel.SpielGUI.mouseClicked(GUI.java:552)

at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

这是相当多的,但最多的是由于其他问题,我正在为游戏编写新的GUI,所以仍然有一些代码需要更改。所以只看第一行 “GUI.java:552”指的是“game.btClicked(text);” “Game.java:403”指的是“spielfeld.disableAll();”但我确定这条线路没问题。

1 个答案:

答案 0 :(得分:2)

您提供的代码中没有构造函数。你有一个看起来非常像构造函数的方法。我假设游戏没有初始化。

构造函数未定义返回类型。在你的情况下你有

public void GUI(Game game ....)

当你真正需要时

public GUI(Game game ....)

以下是一个例子:

public class GUI {
    boolean b = false;
    public static void main(String[] args) {
        GUI g = new GUI();
        System.out.println(g.b);
    }
    public void GUI() {
        b = true;
    }
}

提供输出:

run:
false
BUILD SUCCESSFUL (total time: 2 seconds)

正确的构造函数是:

public class GUI {
    boolean b = false;
    public static void main(String[] args) {
        GUI g = new GUI();
        System.out.println(g.b);
    }
    public GUI() {
        b = true;
    }
}

给出了:

run:
true
BUILD SUCCESSFUL (total time: 1 second)