Solved

How can I test if an object is null? Painting an object? and background color of the applet.

Posted on 1998-04-23
2
161 Views
Last Modified: 2010-04-16
I have the following while loop:

while( j < subopc[i].length ) {
        posy += size;
        subopc[i][j].pos( 20, posy );
        subopc[i][j++].paint( g );
}

The problem is that if subopc[i] = null, I have an exception error. I want to replace  the condition j < subopc[i].length by a way of testing if subopc[i] is null. In fact, some elements are null, so, in such cases, I don't want to enter that loop. How can I do that?

Another question.. I'm calling the paint method of subopc[][] object using subopc[i][j].paint( g ) called from the paint method of the main applet class. This works, but, is it correct? is this the only way to accomplish a painting of the object?

And the last question. I'm using setBackground method to paint the applet window background. Sometimes it works, and sometimes it doesn't. I'm using it in the paint method of the applet class. What's the problem?

Thanks in advance
Jaime
0
Comment
Question by:skel
2 Comments
 

Accepted Solution

by:
jreagan earned 70 total points
ID: 1219515
1.  There is no way to "magically" check to see if an element in an array is a null reference.  You can catch a NullPointerException in a block that contains your while statement:

try {
   while (j < subopc[i].length) { ... {
} catch (NullPointerException ex) { /* let it fall through */ }

but this will only catch the null references when they are detected, not "in the beginning."

2.  You should never call a paint() method.  paint() gets called by the AWT/JFC framework automatically.  What are you trying to paint?  Graphics?  Controls?  Whatever it is, it should dervice from java.awt.Component, and be added to a layout manager.  It will be drawn automatically.  You can e-mail if you have a specific question about this.

3.  Call setBackground() in your init() method of your applet.

0
 

Author Comment

by:skel
ID: 1219516
Thanks a lot jreagan. I don't have your e-mail address.. so I place a follow-up here of the second question. How can I use the layout manager? I'm programming a menu. Walking step by step, I'm first using simply drawString to place the label of the option. I've created each option as an object that extends Panel. I'm using the following code in the init( ) method of the applet to initialize the objects:

numitems = Integer.parseInt( getParameter( "NumItems") );
// Create menu
opc = new Opcion[numitems];
subopc = new Opcion[numitems][ ];
// For each menu option, create the submenu
for( int i = 1; i <= numitems; i++ ) {
                  // Get the number of suboptions
                  n = Integer.parseInt( getParameter( "NumOpc " + i ) );
      // Create the suboptions
      if( n != 0 ) {
            subopc[i-1] = new Opcion[n];
      }
      // Get the URL which is to be called when the option is selected
      trg_url = getParameter( "URL" + i );
      // Get the label of the option
      label = getParameter( "Item " + i );
      // Call the constructor of the option
      opc[i-1] = new Opcion( font_name, font_color, over_color, target, size, trg_url, label );
      // Call constructors of suboptions
      for( int j = 1; j <= n; j++ ) {
            trg_url = getParameter( "URL" + i + " " + j );
            label = getParameter( "Sub" + i + " " + j );
            subopc[i-1][j-1] = new Opcion( font_name, font_color, over_color, target, size-2, trg_url, label );
      }
}

I want to control, therefore, when a user clicks on the option or moves the mouse over it. As I said, I'm actually putting only a text, but later, I want to place buttons or other way to show the options.

Can you help me in doing this? I think this is simple to do. I have the idea.. but I don't know how to implement it respecting JAVA sintax.

Thanks in advance,
Jaime

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
topping2 challenge 13 80
allswap challenge 6 75
Java - sorting a list of objects where the properties of the objects can change during the sort 7 47
jar file executable 12 37
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

929 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

12 Experts available now in Live!

Get 1:1 Help Now