-
NullPointerException
I get NullPointerException when I compile the following code. Can you tell me the problem in this one.
Code:
public class DrawRec {
public static void main(String[] args) {
Rectangle myRect;
myRect.width = 40;
myRect.height = 50;
System.out.println("myRect's area is " + myRect.area());
}
}
-
You are not creating a rectangle object. When you try to define width, there is no object there.
Code:
public class DrawRec {
public static void main(String[] args) {
Rectangle myRect = new Rectanle();
myRect.width = 40;
myRect.height = 50;
System.out.println("myRect's area is " + myRect.area());
}
}
-
Code:
Rectangle myRect = new Rectangle();
Thanks. I missed this.