Solved

Checking to see if a array is empty

Posted on 2011-09-30
28
257 Views
Last Modified: 2012-05-12
I have one program that takes the user selection and creates a array with the data.
I have a second program take creates a dialog and puts the array data in to a table.  

This all works well.
But of course there is always a exception.  The first program runs from a Right Mouse Button click.
The second program runs from a command in the menu bar.

If the user clicks on the command in the menu bar before the array is populated the program throws NULL pointers.

I need to figure out how to check the array to make sure there is something in there and if there is not something I need to popup a message window - Saying you have to select objects first.

I am doing this in my table class to get the array

this.aList = EdiSelection.rds;

Here is how the second program is called.

public class EdiDialogHandler extends AbstractHandler {
	
	/* The constructor */
	
	public EdiDialogHandler() {
		
   }
       
   @Override
   public Object execute(final ExecutionEvent event) throws ExecutionException {
	     TCSession theSession = (TCSession)AIFDesktop.getActiveDesktop().getCurrentApplication().getSession();   
	   showediDialog(theSession);
	   return null;
   }
   private void showediDialog(TCSession theSession) {
		 EdiBaseDialog ar = new EdiBaseDialog(null, theSession);
		 ar.setModal(false);
	  	 ar.setVisible(true);  
	    }    

}

Open in new window


When user clicks the command on the menu - this program is executed - it is calling  EdiBaseDialog.  The  EdiBaseDialog class is where I create the dialog.

I am not sure where I would check the array and terminate the program is empty, should it be in the Handler Class or the EdiBaseDialog?

0
Comment
Question by:jkteater
  • 19
  • 9
28 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36894002


you can check in this way

if(aList != null  &&  aList.get(0) != null)

ot opposite

if(aList == null  ||  aList.get(0) == null)





0
 
LVL 47

Expert Comment

by:for_yan
ID: 36894013
In general you should not have the situation when you list was not created,
so you should check if the first element is populated

if(aList.get(0) == null)//print error message
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36894030

If your constructor of the arraylist was run before you pass the list , so if your
EdiSelection.rds list was created before you assigned it to aList
aList will not be equal to null
So if your list was created but no obects wheer placed there then
aList.get(0) will be equiual to null

So I think that's waht you should be checking for


0
 
LVL 47

Expert Comment

by:for_yan
ID: 36894039
Or you cannalso check for size()

if(aList.size() == 0)//print error message
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36894043

You would not run any risk of course if you check both:

if(aList == null || aList.size() ==0)//print error message

but in general you should not have situation where aList is equal to null
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36894060
By the way, as I remember your aList and rds aare not arrays - they are ArrayList's
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36894089
Where do you check it - befgore you pass it to create the table or insaide - that depends on your organization - perhpas before would nmake more sense
but it depnds on many factors
0
 

Author Comment

by:jkteater
ID: 36894219
I can not get it to work.  

public void checkArray() {
         if(aList.size() == 0) {
               System.out.println("Hey");
               System.exit(0);
         }
   }

It is never getting in to the if statement
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36894226

add printout:
public void checkArray() {
  Systerm.out.println("size: " + aList.size());

         if(aList.size() == 0) {
               System.out.println("Hey");
               System.exit(0);
         }
   }
0
 

Author Comment

by:jkteater
ID: 36894228
Actually the list would not be created - If the user never selects the objects - That means the list is never created.  So I guess I would need to check and see if the list exist?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36894231
yes, then check
if(aList == null)...
0
 

Author Comment

by:jkteater
ID: 36894234
Systerm.out.println("size: " + aList.size());

I have done that and when the user selects objects is shows correct.  It has to be that the list does not exist
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36894242
if aList is null, then

aList.size() shoul throw nullpointer exception

You should not leave any exceptions unprinted - in all catches
you should put ex.printStackTrace() - that facilitates debugging a lot
 

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36894248

>I have done that and when the user selects objects is shows correct.  It has to be that the list does not exist

what means it "shows coerrect" ?

It should print the number
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 47

Expert Comment

by:for_yan
ID: 36894252
If user sselected and you populated the luist then it should show a number > 0
what do you expect?
0
 

Author Comment

by:jkteater
ID: 36894268
OK that was the problem.  

System.exit(0); kills the entire program.  I only wanted to display a message and then terminate the program that was running it - not the entire program and application.

Any Ideas?

Being this is a carry on question I will add points
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36894275
Sure, if you want to show the message - popup JOptionPane

never use System.exit(0)
unless you ewant to exit of java virtual amachine altgeteher
0
 

Author Comment

by:jkteater
ID: 36894285
public void checkArray() {
         if(aList == null) {
               String st="You Must First Select Your Datasets!";
               JOptionPane.showMessageDialog(null,st);
               //System.exit(0);
         }
   }

So how do I exit the program from here?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36894292
If you want to exit after JOptionPane - then System.exit(0) after show option pane is  fine -
it will show mssage to user and block till user prsees the button and then  it will exit
 
0
 

Author Comment

by:jkteater
ID: 36894301
But it kills the whole base application and not just the custom program I am running
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36894309
Of course it will kill the whole virtual machine.

If you don't want that to happen that you need to return from your method instead of using System.exit(0).

Where youy are going to retur - depends on wherefrom you were calling it
0
 

Author Comment

by:jkteater
ID: 36894313
OK - I think I understand
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36894317
So you may want to make this method boolean not void:


public boolean checkArray() {
      boolean b = true;
         if(aList.size() == 0) {
               System.out.println("Hey");
               b = false;
         }
    return b;
   }
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36894326
And then at the place friom where you are calling it - you should chack the returned boolean and take appropriate action - if your array is good
or if it is bad - you may popup window there - in the place where you are claling it from - that alldepends on the ovwerall flow of your application

You may returtn user tio the beginning to give him an option to populate array again, orr do something eles
0
 

Author Comment

by:jkteater
ID: 36894353
The population of the array is from a different program all together.  That is the reason I want to close this program out and let them start the other one
0
 
LVL 47

Accepted Solution

by:
for_yan earned 200 total points
ID: 36894370
System.exit(0) will close everything which was started
by the java command - you cannot close any part of it - you can only transfer execution to one
or anither part of it - you can dispose somne windows, etc, but you cannot "close" one part of your java app
and not close another one.

You you just call this  method form another part of the code - this is not different program altogether - this is the same program,
otherwise you cannot call the method - it may belong to the different class - but this does not matter.

You need to return boolean and make decision based on its value in the calling code - otherwise
what is the point of checking ?
0
 

Author Closing Comment

by:jkteater
ID: 36894394
I am simply return a null return and that  works.  Thanks
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36894395
You should go to the place from where you call this method. If you can call the metghod from that place - it means you can modify the code there
- so go there and check whta the method returns annd there should be something like that in that piece of code:

if(checkArray()){
// go forward wwith waht you need to do if array is fine
} else
{
//perhpas popup the JOption pane here (id you didn't do it in checkArary() - better actually to do it here
//and after it  return to even upper level of your program - and again consider maybe you want to return boolean value there
}

In the program everything is interconnected - there oculd not be one part another part - there can be different part
but general logic should connect them all together
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

746 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

13 Experts available now in Live!

Get 1:1 Help Now