简体   繁体   English

Java从其他类调用方法

[英]Java calling a method from a different class

I'm doing some pretty basic coding, trying to call a method from a different class but for some reason I'm getting a null pointer exception whenever I try to call any method from any different class. 我正在做一些非常基本的编码,试图从其他类中调用方法,但是由于某种原因,每当我尝试从任何其他类中调用任何方法时,我都会得到一个空指针异常。 I think I've created the instances of the class correctly but I'm not sure. 我想我已经正确创建了该类的实例,但不确定。 If anybody can explain what's going wrong to me I'd appreciate it. 如果有人能解释我出了什么问题,我将不胜感激。

here is the class that makes the call: 这是进行呼叫的类:

 public class Menu extends JPanel implements ActionListener{

Skeleton skeleton;
Board board;

public Menu(){

    setBackground(Color.BLACK);

    JButton button = new JButton("hello");  
    button.addActionListener(this);
    this.add(button);
}

public JPanel getPanel(){
    return this;
}

@Override
public void actionPerformed(ActionEvent e) {
    board.boardTest();
}
}

and here is the class containing the method 这是包含方法的类

public class Board extends JPanel{

public Board(){
setBackground(Color.WHITE);
}

public JPanel getPanel(){
    return this;
}

public void boardTest(){
    System.out.print("hello");
}
}

As you can see, whenever the user clicks the button it should print out 'hello'. 如您所见,每当用户单击按钮时,都应打印出“ hello”。

Your code looks as though it should throw a NullPointerException (NPE) when you try to call board.boardTest() because you're making the call before assigning a Board object to the board variable and are thus making a method call on a null variable. 尝试调用board.boardTest() )时,您的代码似乎应该抛出NullPointerException(NPE),因为在将Board对象分配给board变量之前进行了调用,从而对null变量进行了方法调用。

You have to create a Board instance before you can try to use the Board variable, board. 您必须先创建一个Board实例,然后才能尝试使用Board变量board。 ie,

Board board = new Board();

Note 1: that for similar questions in the future, you will want to show us the exception text and indicate by comment in your code which lines throws the exception. 注意1:对于以后类似的问题,您将需要向我们展示异常文本,并在代码中通过注释指示哪些行引发了异常。 ie,

@Override
public void actionPerformed(ActionEvent e) {
    board.boardTest();  // **** A NullPOinterException is thrown here ****
}

Note 2: this question is not Swing specific but rather is a basic Java issue -- you cannot use a reference variable until you first assign it a valid object. 注意2:这个问题不是特定于Swing的,而是一个基本的Java问题-您必须先给它分配一个有效的对象,然后才能使用引用变量。

This is not how you create an instance of class. 这不是创建类实例的方式。 When you declare on some variable of some class you must use the new method. 在某个类的某个变量上声明时,必须使用new方法。 If you don't the compiler won't know how to relate to this and therefore the program isn't valid and that's why you are getting null exception. 如果您不这样做,编译器将不知道如何与此相关,因此该程序无效,这就是为什么您会得到null异常的原因。 You see, when you write a program and try to start it, it first go through the compiler, the compiler checks if the program is valid and if not is alerting about it. 您会看到,当您编写程序并尝试启动它时,首先要通过编译器,编译器会检查该程序是否有效,如果无效,则不会发出警告。 The compiler see the code as a long string and divide it into tokens. 编译器将代码视为长字符串,并将其划分为令牌。 To be more simple, let's say that each declaration about some variable is a token,each keyword is also token. 简单来说,每个关于某个变量的声明都是一个标记,每个关键字也是一个标记。 The variable name is identifier. 变量名称是标识符。 So the compiler search for tokens and save there data in some symbols table and say what is type and what is value. 因此,编译器搜索令牌并将数据保存在某些符号表中,并说出什么是类型和什么是值。 For example int num =3; 例如int num = 3; int is token, num is identifier and 3 is the value, now the compiler will know how much memory to allocate for this. int是令牌,num是标识符,3是值,现在编译器将知道为此分配多少内存。 Now in your case you didn't write Board board = new Board(); 现在,在您的情况下,您无需编写Board board = new Board(); Because of this, the compiler doesn't know how much space to allocate and there is no reference to some instance. 因此,编译器不知道要分配多少空间,也没有对某些实例的引用。 So the value in the symbols table isn't declare. 因此,符号表中的值未声明。 This cause the compiler to allet about null exception. 这导致编译器分配有关空异常的信息。 Note that another rolenof the constructor of some class is to initialize some class fields. 请注意,某些类的构造函数的另一个作用是初始化某些类字段。 Let's say you have the class Point and you want that each time you create a new point the initial x,y will be zero. 假设您拥有Point类,并且您希望每次创建一个新点时,初始x,y将为零。

So 所以

Class Point{
int x,y;
 Point(){
    x=y=0;
}

And when you create a new point the initial coordinate will (0,0). 创建新点时,初始坐标将为(0,0)。

Point p = new Point();

Whenever you call a method residing in some other class, you have to create an object of that other class and then call it.Here I see that you have not created the object. 每当调用驻留在某个其他类中的方法时,都必须创建该另一个类的对象,然后再调用它。在这里,我看到您尚未创建该对象。

Board board = new Board(); Board board =新的Board(); is missing from the code 代码中缺少

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM