java 为什么我不能从其他类调用目标函数?

c90pui9n  于 2023-06-20  发布在  Java
关注(0)|答案(1)|浏览(131)

正如标题所提到的。为什么主类无法调用event.java函数并打印出我想要它打印的消息?我还漏了什么吗非常感谢。
App.java:(主程序)

package com.mainsystem;

public class App {

    public static void main(String[] args) throws Exception {
    
        System.out.println("Hi");
        Event event = new Event();
        event.StartGame();
    }

}

Event.java (由主程序调用)

package com.mainsystem;

import java.util.Scanner;

public class Event {
Scanner sc = new Scanner(System.in);

    public void StartGame() {
        System.out.println("Welcome to new World.");
        System.out.println("You are now invited to this new world...");
    }

}

我的预期结果:

Hi.
Welcome to new World.
You are now invited to this new world...

但现在显示的结果是:

Hi
6rqinv9w

6rqinv9w1#

也许在初始化Event-Class时抛出了异常?你也在创建PlayerReactionPlayer的示例,也许里面有一个NullPointer?
为了调试的目的,你可以捕获并打印可能出现在某处的所有异常:

public static void main(String[] args) throws Exception {

    System.out.println("Hi");
    try{
       Event event = new Event();
       event.StartGame();
    } catch (Exception e){
       e.printStackTrace();
    }
}

相关问题