Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

drawing lines with mouse  RSS feed

 
Mohamad Samy
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still have the error of NullPointerException for the following code (to draw on the the panel just with mouse press and release). i have four classes, the error is at DrawPanel class when i create an object of class MyLine, i don't know why
the following is the main abstract class



the next is the class for drawing lines




the following is the panel class to draw on



finally is the test for the code

 
Rob Camick
Ranch Hand
Posts: 2800
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you create the DrawPanel class your Points array is empty.

 
Mohamad Samy
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but i first declared any array of points before the constructor as Point [] points = new Point[10000]; which will be initiated in the constructor when the mouse is pressed and released. please, indicate more am i right!!
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're allocating the array, but that array is empty as Rob said - there are no Point objects in it. You need to check for that before you try to use any array elements.
 
Mohamad Samy
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, i removed all this array and replaced it by integers for the coordintaes like integers x1, y1 and x2, y2 and the error is removed by still displaying the panel without the capablility for drawing lines, !!!
so, i replaced the line drawing using MyLline class object by the simple and it works but i want to use MyLine class object in my paint component method
 
Mohamad Samy
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks alot with your responses. i solved the problem which was in my MyLine class as i tried to use it directly where i should use it polymorphically as all the variables become a part of Shape class when i declared the the MyLine object in the DrawPanel class. i will convert the code then to use points instead of the exact integers. thanks again
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!