Solved

Run time exception when loading an Image.

Posted on 1998-04-10
2
180 Views
Last Modified: 2010-04-16
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
0
Comment
Question by:DaThiN
2 Comments
 

Expert Comment

by:wuest
ID: 1234022
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.
0
 

Accepted Solution

by:
ammorris earned 20 total points
ID: 1234023
It sounds like you had a problem loading the image.  The only exception thrown by the MediaTracker.waitForID() method is InterruptedException, which means that the Thread was interrupted.  There is no exception thrown by the getImage method.  It just creates a thread, starts it and exits.  Check for errors in loading the images with the MediaTracker.isErrorAny() method.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
triangle challenge 4 79
countAbc challenge 9 49
unix in java example 9 43
Java Loop 4 22
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now