java - Painting balloons on Graphics2D -
i want make bubble shooter game , have problem generate bubbles @ start. when trying compile program, have error: exception in thread "awt-eventqueue-0" java.lang.nullpointerexception.
public class mypanel extends jpanel { init init; public mypanel(){ super(); init = new init(); } public void paint(graphics g){ dimension size = getsize(); graphics2d g2d = (graphics2d) g; g2d.setcolor(color.gray); g2d.fillrect(0, 0, size.width, size.height - 70); for(int j = 0; j < 10; j++) for(int = 0; < 20; i++){ init.fields[i][j].b.paint(g); //here compiler shows error } } } public class field { private int x; private int y; private int r = 30; public baloon b; public field(int x, int y){ this.x = x*r; this.y = y*r; } public void addbaloon(int n){ b = new baloon(this.x, this.y, r, n); } } public class init { parser pr = new parser(); private int r = pr.getradius(); private int x = pr.getxdimension(); private int y = pr.getydimension(); private int ni = pr.getcolorrange(); field[][] fields = new field[x][y]; private int startx = 20; private int starty = 10; public init(){ for(int yi = 1; yi<y; yi++){ (int xi = 1; xi<x; xi++){ fields[xi][yi] = new field(xi*r, yi*r); } } for(int yi = 1; yi < starty; yi ++){ for(int xi = 1 ; xi < startx; xi++){ random rand = new random(); int n = rand.nextint(ni); fields[xi][yi].addbaloon(n); } } } }
you initializing array index 1:
for(int yi = 1; yi<y; yi++){ (int xi = 1; xi<x; xi++){ fields[xi][yi] = new field(xi*r, yi*r); } }
while accessing 0 like:
for(int j = 0; j < 10; j++) for(int = 0; < 20; i++){ init.fields[i][j].b.paint(g); //here compiler shows error }
array index starts 0 , goes upto n-1. need initialize like:
for(int yi = 0; yi<y; yi++){ (int xi = 0; xi<x; xi++){ fields[xi][yi] = new field(xi*r, yi*r); } }
Comments
Post a Comment