Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
178 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
[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
 

Accepted Solution

by:
jreagan earned 280 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

688 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