java - 对象引用困惑

标签 java object reference

我在以下代码中的 MainClass.ob1.noOfVerts 变量上收到 NullPointerException:

<小时/>
import java.awt.*;
import javax.swing.*;

public class drawr extends JPanel{

    public void paintComponent(Graphics g){

        super.paintComponent(g);
        g.setColor(Color.decode("#ffc000"));
        g.drawLine(0, 0, getWidth(), getHeight());
        for(int cnt=0; cnt/displayObject.dim*2<=**MainClass.ob1.noOfVerts**; cnt+=displayObject.dim*2){
            g.drawLine(MainClass.ob1.coords[cnt], MainClass.ob1.coords[cnt+1], MainClass.ob1.coords[cnt+2], MainClass.ob1.coords[cnt+3]);
        }
    }

该对象在这里实例化:

import java.awt.Color;
import javax.swing.*;

public class MainClass{

    public static final int windowWidth = 1280;
    public static final int windowHeight = 640;
    public static boolean crash = false;

    public static displayObject **ob1**, ob2, ob3;

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

        String colorString="#ff0000";
        int ob1verts[]={10,10,50,50,30,80};
        **displayObject ob1=new displayObject("#ff0000", ob1verts);**
        int ob2verts[]={30,50,70,90,130,180,75,30};
        displayObject ob2=new displayObject("#00ff00", ob2verts);
        int ob3verts[]={10,10,70,50,70,80,110,130,30,30};
        displayObject ob3=new displayObject("#0000ff", ob3verts);

使用这个构造函数:

  public class displayObject {

        //  Each object is defined by a color, number of vertices, and dim (2,3,4) coordinate vertex locations
        //  Use dim to verify that the right number of vertices were sent

            public static int dim=2;

            public String color;
            **public int noOfVerts**;
            public int coords [];

            public displayObject (String col, int verts[]){

                if (verts.length%dim != 0){
                    System.out.printf ("Crap in!");
                    return;
                }
                this.coords=new int[verts.length+2];
                color=col;
                **noOfVerts=verts.length/dim;**
                for (int cnt=0; cnt<verts.length; cnt++){
                    coords[cnt]=verts[cnt];
                }
                coords[verts.length]=verts[0];  //make last vertex equal first to close the shape
                coords[verts.length+1]=verts[1];
            }

    }

我创建了一个静态变量,我认为它会通过适当的指针引用对象的特定内存空间。为什么对象引用是空指针?

最佳答案

The object is instantiated here:

public static displayObject **ob1**, ob2, ob3;

不,这不是实例化任何东西。它声明一个变量,但它的默认值是null。在某些时候,您需要为其分配一个非空值,例如

ob1 = new displayObject(...);

现在你有这一行:

displayObject ob1=new displayObject("#ff0000", ob1verts);

...但这不是同一件事。这就是声明一个完全独立的局部变量,并为其赋予一个值。它恰好具有相同的名称 (ob1),但它是一个不同的变量。您可能只需将其更改为不是局部变量声明即可:

ob1 = new displayObject("#ff0000", ob1verts);

...其他变量也是如此。

(顺便说一句,我强烈建议避免非私有(private)字段,并开始遵循 Java 命名约定。)

关于java - 对象引用困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31035503/

相关文章:

Java 与 PHP 基准测试

java - 正则表达式在java中查找以 'JD'固定字长开头的单词

ios - 如何检查(id)对象在ios中是否为空?

php - 如何访问本身是另一个对象的属性的对象的静态属性?

c# - Visual Studio 2010 "System.Web"引用未解决 [C#]

java - 如何将linux命令从一个设备发送到通过usb otg连接的另一台设备?

java - Apple Silicon(M1) 11.4 出现 java 错误 (jdk16)

javascript - 在对象数组中查找下一个可用的 id

c++ - *&vs&in将变量传递给函数

c++ - 无法在结构中创建 1 个球体实例