Monday 15 March 2010

nullpointerexception - Processing button class -



nullpointerexception - Processing button class -

the exercise follows:

rewrite programming exercise 3 lecture 6 creating class called button replace arrays. a) create class , define class variables hold info position, dimensions , color. in add-on class variable should made, contains title of particular button. utilize constructor set initial values of class variables.

so basically, have convert previous exercise have done class. how made previous exercise in case need it: http://pastebin.com/rqm6hj6k

so tried convert class, apparently gives me error , cannot see how prepare it. teached said don't have maintain array, , instead create many variables instead of info in array.

the language processing , gives error code nullpointerexception

class button { int[] nums; button(int n1, int n2, int n3, int n4) { nums[0] = n1; nums[1] = n2; nums[2] = n3; nums[3] = n4; } void display() { fill(255, 0, 0); rect(nums[0], nums[1], nums[2], nums[3]); } }; void setup() { size(800, 800); button butt = new button(75, 250, 200, 200); butt.display(); }

you're declared nums, not initialized it. results in nullpointerexception: in constructor you're accessing nums[0], nums doesn't have length yet. seek this:

class button { //remember initialize/allocate array int[] nums = new int[4]; button(int n1, int n2, int n3, int n4) { nums[0] = n1; nums[1] = n2; nums[2] = n3; nums[3] = n4; } void display() { fill(255, 0, 0); rect(nums[0], nums[1], nums[2], nums[3]); } }; void setup() { size(800, 800); button butt = new button(75, 250, 200, 200); butt.display(); }

in future, create sure variables seek access properties of(arrays/objects) initialized/allocated first(otherwise you'll nullpointerexception 1 time again , it's no fun)

as @v.k. nicely points out, it's improve have readable code , remove of redundancy. before x,y,width , height of button stored in array. array do: store info , that's it! class can not store same info individual easy read properties, can do more: functions! (e.g. display())

so, more readable version:

class button { //remember initialize/allocate array int x,y,width,height; button(int x,int y,int width,int height) { this.x = x; this.y = y; this.width = width; this.height = height; } void display() { fill(255, 0, 0); rect(x,y,width,height);//why don't utilize this.here or everywhere ? } }; void setup() { size(800, 800); button butt = new button(75, 250, 200, 200); butt.display(); }

yeah, it's sorta easier read, what's deal this may inquire ? well, it's keyword allows gain access object's instance (which ever may in future when take instantiate) , hence it's properties (classes version of variables) , methods (classes version of functions). there's quite lot of neat things larn in terms of oop in java, can take 1 step @ time nice , visual approach in processing. if haven't already, check out daniel shiffman's objects tutorial

best of luck learning oop in processing!

class nullpointerexception processing

No comments:

Post a Comment