Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Run time exception when loading an Image.

Posted on 1998-04-10
2
Medium Priority
?
187 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 40 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

636 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