java.lang.NullPointerException in Applet -


i'm writing applet makes mickey mouse face appear when click on screen , @ moment writing code make sure when click on face can drag entire image. mickey made of 3 filledoval objects: face, leftear, , rightear. whenever run program gives me errors, applet works should. these errors:

exception in thread "awt-eventqueue-1" java.lang.nullpointerexception         @ mickey.onmousepress(mickey.java:73)         @ objectdraw.windowcontrollerlistener.mousepressed(windowcontroller.java:200)         @ java.awt.component.processmouseevent(component.java:6502)         @ javax.swing.jcomponent.processmouseevent(jcomponent.java:3321)         @ java.awt.component.processevent(component.java:6270)         @ java.awt.container.processevent(container.java:2229)         @ java.awt.component.dispatcheventimpl(component.java:4861)         @ java.awt.container.dispatcheventimpl(container.java:2287)         @ java.awt.component.dispatchevent(component.java:4687)         @ java.awt.lightweightdispatcher.retargetmouseevent(container.java:4832)         @ java.awt.lightweightdispatcher.processmouseevent(container.java:4489)         @ java.awt.lightweightdispatcher.dispatchevent(container.java:4422)         @ java.awt.container.dispatcheventimpl(container.java:2273)         @ java.awt.component.dispatchevent(component.java:4687)         @ java.awt.eventqueue.dispatcheventimpl(eventqueue.java:707)         @ java.awt.eventqueue.access$000(eventqueue.java:101)         @ java.awt.eventqueue$3.run(eventqueue.java:666)         @ java.awt.eventqueue$3.run(eventqueue.java:664)         @ java.security.accesscontroller.doprivileged(native method)         @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:76)         @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:87)         @ java.awt.eventqueue$4.run(eventqueue.java:680)         @ java.awt.eventqueue$4.run(eventqueue.java:678)         @ java.security.accesscontroller.doprivileged(native method)         @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:76)         @ java.awt.eventqueue.dispatchevent(eventqueue.java:677)         @ java.awt.eventdispatchthread.pumponeeventforfilters(eventdispatchthread.java:211)         @ java.awt.eventdispatchthread.pumpeventsforfilter(eventdispatchthread.java:128)         @ java.awt.eventdispatchthread.pumpeventsforhierarchy(eventdispatchthread.java:117)         @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:113)         @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:105)         @ java.awt.eventdispatchthread.run(eventdispatchthread.java:90) 

the error starts on line 73, have onmousepress method. here code:

import objectdraw.*; import java.awt.*;  public class mickey extends windowcontroller {   private static final int instr1_x = 50;   private static final int instr1_y = 50;   private static final int instr2_x = instr1_x;   private static final int instr2_y = instr1_y + 20;   private static final int face_radius = 50;   private static final int ear_radius = 30;   private static final int ear_offset = 50; // center of each ear offset                                             // , on (x , y) center                                             // of face.   private static double x_pos = 0;   private static double y_pos = 0;   private text instrone, instrtwo;   private filledoval face, leftear, rightear;   private location mousepoint, difference;    private static boolean isclicked = false;   private static boolean isover = false;    public void begin()   {     instrone =  new text( "click display mickey silhouette " +                           "centered @ mouse click",                           instr1_x, instr1_y, canvas );     instrtwo = new text( "mouse press in part of image , " +                          "drag move image around.",                          instr2_x, instr2_y, canvas );   }    public void onmouseclick( location point )   {     if ( !isclicked )     {       x_pos = ( point.getx() - (face_radius / 2) );       y_pos = ( point.gety() - (face_radius / 2) );       mousepoint = new location ( x_pos, y_pos );       instrone.hide();       instrtwo.hide();       face = new filledoval( mousepoint, face_radius, face_radius, canvas);       leftear = new filledoval( (x_pos - (ear_radius / 2)),                                 (y_pos - (ear_radius / 2)),                                 ear_radius, ear_radius, canvas );        rightear = new filledoval((x_pos + (ear_offset - (ear_radius / 2))) ,                                 (y_pos - (ear_radius / 2)),                                 ear_radius, ear_radius, canvas );       isclicked = true;     }   }    public void onmousepress ( location point )   {     if (face.contains ( point ) ||         leftear.contains ( point ) ||         rightear.contains ( point ) )     {       isover = true;     }   }    public void onmouserelease ( location point )   {     isover = false;   }    public void onmousedrag ( location point )   {     if ( isover )     {       x_pos = ( point.getx() - (face_radius / 2) );       y_pos = ( point.gety() - (face_radius / 2) );       face.moveto( x_pos, y_pos );       leftear.moveto( (x_pos - (ear_radius / 2)),                       (y_pos - (ear_radius / 2)) );       rightear.moveto( (x_pos + (ear_offset - (ear_radius / 2))),                        (y_pos - (ear_radius / 2)) );     }   }    public void main ( string[] args )   {     mickey current = new mickey();     current.begin();   } } 

onmousepress called when mouse button pressed down. tries use face, has not been initialized yet. face initialized in onmouseclick, called when mouse clicked. means "the mouse button pressed , released". can't use face (or field) before initialization, crashes nullpointerexception. try moving code

face = new filledoval( mousepoint, face_radius, face_radius, canvas); leftear = new filledoval( (x_pos - (ear_radius / 2)),                           (y_pos - (ear_radius / 2)),                           ear_radius, ear_radius, canvas ); rightear = new filledoval((x_pos + (ear_offset - (ear_radius / 2))) ,                           (y_pos - (ear_radius / 2)),                           ear_radius, ear_radius, canvas ); 

to method called when ellipses needed (onmousepress before code uses them).


Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -