Solved

Run time exception when loading an Image.

Posted on 1998-04-10
2
184 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
hibernate example issues from command prompt 10 66
Java string replace 11 64
ejb stateless example 2 44
Java array 10 65
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

730 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