Solved

Run time exception when loading an Image.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What is JNDI datasource in spring 1 50
Java - Why doesn't this JFrame work 3 40
HSSFWorkbook cannot be resolved error 10 49
javap not working 8 36
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.

914 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

18 Experts available now in Live!

Get 1:1 Help Now