java Java从不同的类调用方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17776763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 14:57:28  来源:igfitidea点击:

Java calling a method from a different class

javaclassmethods

提问by Corey

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”。

回答by Hovercraft Full Of Eels

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.

当您尝试调用时,您的代码看起来好像应该抛出 NullPointerException (NPE),board.boardTest()因为您在将 Board 对象分配给 board 变量之前进行调用,从而对空变量进行方法调用。

You have to create a Board instance before you can try to use the Board variable, board. i.e.,

您必须先创建一个 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. i.e.,

注意 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 问题——在您首先为它分配一个有效对象之前,您不能使用引用变量。

回答by Elior

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. 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. 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 is token, num is identifier and 3 is the value, now the compiler will know how much memory to allocate for this. Now in your case you didn't write 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.

这不是您创建类实例的方式。当您声明某个类的某个变量时,您必须使用新方法。如果您不这样做,编译器将不知道如何与此相关,因此程序无效,这就是您收到空异常的原因。你看,当你编写一个程序并尝试启动它时,它首先通过编译器,编译器检查程序是否有效,如果无效则发出警报。编译器将代码视为一个长字符串并将其划分为标记。更简单地说,假设关于某个变量的每个声明都是一个标记,每个关键字也是一个标记。变量名是标识符。所以编译器搜索标记并将数据保存在一些符号表中,并说明什么是类型,什么是值。例如 int num =3; int 是令牌,num 是标识符,3 是值,现在编译器将知道为此分配多少内存。现在在你的情况下你没有写 Board board = new Board(); 因此,编译器不知道要分配多少空间,也没有对某些实例的引用。所以符号表中的值没有声明。这会导致编译器对空异常发出警告。注意一些类的构造函数的另一个作用是初始化一些类的字段。假设您有 Point 类,并且希望每次创建新点时初始 x,y 为零。所以符号表中的值没有声明。这会导致编译器对空异常发出警告。注意一些类的构造函数的另一个作用是初始化一些类的字段。假设您有 Point 类,并且希望每次创建新点时初始 x,y 为零。所以符号表中的值没有声明。这会导致编译器对空异常发出警告。注意一些类的构造函数的另一个作用是初始化一些类的字段。假设您有 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();

回答by sandeepsharat

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(); is missing from the code

Board board = new Board(); 代码中缺少