0

I am getting a NullPointerException on the MainClass.ob1.noOfVerts variable in the following code:


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]); } } 

The object is instantiated here:

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); 

with this constructor:

 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]; } } 

I've created a static variable that I would think would refer to a specific memory space for the object with an appropriate pointer. Why is the object reference a Null Pointer?

1 Answer 1

1

The object is instantiated here:

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

No, that's not instantiating anything. It's declaring a variable, but it's just got its default value of null. At some point you need to assign a non-null value to it, e.g.

ob1 = new displayObject(...); 

Now you have this line:

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

... but that's not the same thing. That's declaring an entirely separate local variable, and giving that a value. It happens to have the same name (ob1) but it's a different variable. You possibly just need to change it to not be a local variable declaration:

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

... and ditto the other variables.

(As an aside, I'd strongly recommend avoiding non-private fields, and also starting to follow Java naming conventions.)

Sign up to request clarification or add additional context in comments.

4 Comments

Whoa! So the object type (in this case displayObject) on the left side of the declaration makes the difference in creating a new object or not. And the new declaration creates a private (local) field? Does declaring the variable 'static' have a function in this case outside of an object class?
@user2671186: No, not in creating a new object - in declaring a local variable or not. And private and local aren't the same thing - it's really important to be precise with terminology. Not sure what you mean about declaring a static field "outside of an object class", but a static field and a regular (instance) field are very different, yes.
Very helpful, thanks. Any suggestions for places to learn or read about these things? I don't want to do 'Hello World' type lessons, but it would be nice to have a better understanding of subjects like this.
@user2671186: Why don't you want to do "Hello World" type lessons? They're precisely designed to help you learn the basics like this.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.