Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java错误:由于缺少构造函数而导致NullPointerException_Java_Methods_Nullpointerexception - Fatal编程技术网

Java错误:由于缺少构造函数而导致NullPointerException

Java错误:由于缺少构造函数而导致NullPointerException,java,methods,nullpointerexception,Java,Methods,Nullpointerexception,我试图访问另一个类中的一个方法,但我在所述类中已经有一些构造函数,当我试图在另一个类中创建实例时,这些构造函数抛出了一个错误 编辑:我知道我需要填写一个构造函数,但我不知道该填什么。我是在ArrayControl类中创建另一个JFrame还是什么 这是我运行时遇到的错误: Exception in thread "main" java.lang.NullPointerException at Position.ArrayControl.updatePosition(ArrayControl.ja

我试图访问另一个类中的一个方法,但我在所述类中已经有一些构造函数,当我试图在另一个类中创建实例时,这些构造函数抛出了一个错误

编辑:我知道我需要填写一个构造函数,但我不知道该填什么。我是在ArrayControl类中创建另一个JFrame还是什么

这是我运行时遇到的错误:

Exception in thread "main" java.lang.NullPointerException
at Position.ArrayControl.updatePosition(ArrayControl.java:10)
at Display.Display.<init>(Display.java:39)
at Main.Game.<init>(Game.java:19)
at Main.Launch.main(Launch.java:6)
我试图访问的“Display”类:

public class Display {
public JPanel panel = new JPanel();
public ArrayControl arrayC = new ArrayControl();
public JFrame frame;
JLabel[][] grid= new JLabel[5][5];

public JFrame getFrame() {
    return frame;
}

public Display(JFrame frame){
    this.frame = frame;
}

private String title;
private int width, height;
Color sky = new Color(137, 182, 255);
Color enemyPortal = new Color(154, 91, 193);


//Creating a display function that takes in a title, the width (pixels) and height (pixels)
public Display(String title, int width, int height){
    this.title = title;
    this.width = width;
    this.height = height;

    createDisplay();
    arrayC.updatePosition("Test", 2, 3);
}
我意识到这可能是一个愚蠢的问题,答案直视着我的脸,但所有的帮助都是感激的。
谢谢。

您创建了一个构造函数

公共显示(JFrame){
this.frame=frame;
}


但是在创建显示对象时没有填写构造函数(JFrame)。

您正在从
display
的构造函数调用
arrayC.updatePosition
,此时
arrayC.display
仍然是
null
,因为应该分配给它的实例是,仍在构造中。

在Java中,只有在未指定其他构造函数时,才会显示默认构造函数(没有任何参数的构造函数)。由于您编写了自己的构造函数:

public Display(JFrame frame){
    this.frame = frame;
}
现在不能调用new Display()。 您可以:

  • 在ArrayControl类中创建JFrame并调用new Display(JFrame)
  • 在显示类中创建不带参数的新构造函数:

    public Display() { //do nothing or maybe create JFrame here? }
    

  • 如果编写java类时未声明任何显式构造函数,则它有一个默认的无参数构造函数。但是,如果至少显式声明了一个构造函数,则默认情况下不存在无参数构造函数,如果需要,需要添加该构造函数。在您的例子中,您有一个
    显示(字符串标题、整数宽度、整数高度)
    构造函数,因此如果需要,必须显式声明无参数构造函数


    这就是说,您有一个用于
    Display
    调用
    arrayC.updatePosition()
    的构造函数,它反过来创建另一个不同的显示对象。这闻起来不对。我怀疑您想要一个
    显示
    实例。我将创建一个单独的方法实例化display,然后实例化需要引用它的所有其他对象,并将其作为参数传递给它们的构造函数。

    正如您所识别的,错误在您的
    ArrayControl
    类中:

    Display display = new Display();
    
    Display
    没有无参数构造函数

    然而,从评论中可以确定,即使修复了它也无法修复您的程序

    当前,您的
    ArrayControl
    类正在构造
    Display
    的新实例(错误),并尝试更新存储在
    ArrayControl
    类中
    Display
    字段中的
    Display
    实例。这不是您想要做的,而是您想要更新实例化
    ArrayControl
    对象的
    Display
    实例

    要实现这一点,您需要将
    Display
    的当前实例传递到
    ArrayControl
    以供使用。例如(简化):


    我不确定您的更大目标是什么,以及为什么会有
    ArrayControl
    类。我想问题中还有更多内容。

    我知道,很抱歉我解释得不够,我会编辑问题。我知道我需要填写构造函数,但我不知道该填写什么,我是否要创建另一个JFrame?这就是我被难倒的部分。感谢您的快速响应。您希望将Display实例传递给ArrayControl,因为您不想更新其他显示器,或者您想?我怀疑这种情况,因此在您的显示类中,使用
    this
    将当前显示实例传递给ArrayControl的构造函数,该构造函数接受显示对象作为参数,并将传递的显示对象分配给ArrayControl中的字段
    Display
    。您的假设是正确的!我不想更新其他显示。我不确定该如何做到这一点,摆弄这个想法只会给我带来堆栈溢出错误。事实上,我不是最有经验的程序员。我不知道如何按照你的建议去做。您提到的display方法正在我的程序的主类中调用。EDIT我试图尽可能按照您的说明进行操作,我创建了一个单独的方法来实例化display,更新了对它的所有相关调用,现在我只剩下默认的no args构造函数。但是,我得到了一个StackOverflow错误,可以在这里找到:非常感谢您迄今为止的帮助。您仍然在从
    Display
    的构造函数实例化
    位置。ArrayControl
    ,在那里您正在实例化一个新的
    Display
    。如果Display和ArrayControl都需要相互引用,我会这样做:`myNewMethod(){Display d=new Display();非常感谢!这完全解决了我的问题。感谢您让我关于StackOverflow的第一个问题变得有价值!
    Display display = new Display();
    
    public class Display {
        private ArrayControl arrayControl;
    
        public Display() {
            // Pass this instance of Display to ArrayControl to use
            arrayControl = new ArrayControl(this);
            arrayControl.updatePosition("Test", 2, 3);
        }
    
        public void updateGrid(String name, int x, int y) {
            // Do update grid stuff
        }
    
    }
    
    public class ArrayControl {
    
        private Display display;
    
        public ArrayControl(Display display) {
            this.display = display; // assign instance of Display to the field display
        }
    
        public void updatePosition(String name, int x, int y) {
            display.updateGrid(name, x, y);
            // I'm not sure why you want to pass the display back to the
            // Display class as in your example so I removed it
        }
    
    }