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
170 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 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 161
Java: How do I open the default windows program for a given file type 3 62
junit as external jar or library 7 58
Java regex 5 22
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

738 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