Link to home
Start Free TrialLog in
Avatar of DaThiN
DaThiN

asked on

Run time exception when loading an Image.

I'm here again. I'm trying the following. First of all, I want to program a menu with buttons. I created two classes: Menu and Opcion. Menu is the main class and Opcion is a class for a button. In Menu Class, I've created 9 objects of Opcion class (that is, 9 buttons). I've used: boton[i] = new Opcion( "list of paramaters to the constructor" ). In the Opcion class, I have a method called create( ) which creates the button. Here is the code:

                    protected void create( ) {
            MediaTracker t;

            if( off_im == null ) {
                  try {
                        boton = getImage( getDocumentBase( ), img_src );
                        t = new MediaTracker( this );
                        t.addImage( boton, 0 );
                        t.waitForID( 0 );
                  } catch( Exception e) {
                        showStatus( "Ha ocurrido el error en el Applet: " + e );
                  }
            }
            width = boton.getWidth( this );
            height = boton.getHeight( this );
            Font fuente = new Font( fontname, Font.BOLD, ( width/length ) );
            Color color = new Color( fontcolor );
            off_im = createImage( width, height );
            off_graph = off_im.getGraphics( );

            off_graph.setColor( color );
            off_graph.setFont( fuente );

            FontMetrics fm = off_graph.getFontMetrics( );
            while( fm.stringWidth( ltext ) < width - 30 ) {
                  fuente = new Font( fontname, Font.BOLD, fuente.getSize( ) + 1 );
                  off_graph.setFont( fuente );
                  fm = off_graph.getFontMetrics( );
            }
            
            off_graph.drawImage( boton, 0, 0, this );
            drawCenteredString( label, width, height, off_graph );
      }

The problem is that after boton = getImage( getDocumentBase( ), img_src );, boton contains "null" and an run-time exception occures. The img_src string is "Imagenes/boton.gif" which does exist. I had this code before inside the paint( ) method of Menu Class and it worked well, now I moved it to the Opcion class, and it didn't.

What is the problem?
thanks in advance
Jaime
Avatar of wuest
wuest

You mentioned at the end that this code existed inside the paint method of the Menu
class.  You might want to duplicate this implementation inside the Opcion class. Basically put the code that initializes the graphics of the button in a paint method instead
of the create method you have shown above.
ASKER CERTIFIED SOLUTION
Avatar of ammorris
ammorris

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial