java - Null pointer exception on method -
this question has answer here:
- what nullpointerexception, , how fix it? 12 answers
this first every java project.all going , program compiled. got 3d looking object on screen. when use moveto(int x,int y) method null pointer exception. i've looked , it's huge issue lot of people. still can't grasp concept of , of fixes worked other people don't seem fit program. have tried using if statements check null , stuff can't syntax right or something. example base ellipse , ellipse class has moveto(x,y) method.but when call on using base.moveto(x,y) im getting error. can help? cylinder1.moveto(8, 8); exception occurred. java.lang.nullpointerexception @ cylinder3d.moveto(cylinder3d.java:57) ment stack trace? here code moveto method in rectangle class. public void moveto(int x, int y) { xposition = x; yposition = y; makevisible(); draw(); }
public class cylinder3d { private int diameter; private int height; private ellipse top; private ellipse base; private rectangle wall; int xposition; int yposition; public cylinder3d(int diameter, int height) { int x = 0; int y = 0; ellipse top = new ellipse(); int xdiameter=diameter; int ydiameter=diameter/2; int xposition = 0; int yposition = 0; top.setstate(xdiameter,ydiameter,xposition,yposition,"black"); ellipse base = new ellipse(); int ypositionbase = yposition + height; base.setstate(xdiameter,ydiameter,xposition,ypositionbase,"black"); rectangle wall = new rectangle(); int xsidelenght = diameter; int ysidelenght = height; int ypositionwall = yposition + (diameter/4); wall.setstate(xsidelenght,ysidelenght,xposition,ypositionwall,"black"); //.... } public void moveto(int x, int y) { wall.moveto(x,y); //here error. base.moveto(x,y); top.moveto(x,y); } }
your constructor not touch of member variables top
, base
, wall
, because local variables declared in constructor shadow member variables want initialise.
thus, stay null
, , trying call member functions on them in moveto
throws.
Comments
Post a Comment