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

Posted on 1998-04-23
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
Question by:skel

Accepted Solution

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.


Author Comment

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,


Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSumClump challenge 9 115
JDeveloper 12c for 32 bit 4 84
Basic Java Case or If-Else statement... 3 50
by zero exception 10 40
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
This video teaches viewers about errors in exception handling.

770 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