Null PdsDirectory.MemberInfo.Statistics object

General discussion on the JZOS batch launcher and toolkit
Post Reply
Cogito-Ergo-Sum
Posts: 30
Joined: Thu Jan 25, 2007 8:31 am
Location: Bengaluru, India

Null PdsDirectory.MemberInfo.Statistics object

Post by Cogito-Ergo-Sum »

Hi,
If I have deleted the ISPF Statistics of PDS member names using option =3.5, then a null PdsDirectory.MemberInfo.Statistics object is created for the PDS member(s). This caused a Null Pointer Exception in a certain class and it took me sometime to guess the reason as missing statistics.

It would be nice if the class could throw an exception (such as NoStatsException) if statistics are not available for a PDS member.

Regards,
Nags.
Debugging tip:
When you have eliminated all which is impossible, then whatever remains, however improbable, must be the truth.
--Sherlock Holmes
dovetail
Site Admin
Posts: 2022
Joined: Thu Jul 29, 2004 12:12 pm

Post by dovetail »

Where are you getting a NullPointerException? Can you provide a stack dump?
Cogito-Ergo-Sum
Posts: 30
Joined: Thu Jan 25, 2007 8:31 am
Location: Bengaluru, India

Post by Cogito-Ergo-Sum »

Hi,
For this class :

Code: Select all

package pdsStat;

import java.util.Date;
import java.util.Iterator;
import com.ibm.jzos.*;
import java.io.IOException;

public class PDSStatistics {
	public static void main(String[] args) {
		String pdsName = args[0];
		
		try {
			PdsDirectory pdir = new PdsDirectory(pdsName);
			for (Iterator iter = pdir.iterator(); iter.hasNext();) {
				PdsDirectory.MemberInfo pm = (PdsDirectory.MemberInfo) iter.next();
				PdsDirectory.MemberInfo.Statistics pms = pm.getStatistics();
				Date modifDate = pms.modificationDate;
				System.out.println("Modification date : " + modifDate);				
			}			
		} catch (IOException i) {
			i.printStackTrace();
		}
	}
}
I get an Exception as :

Code: Select all

Exception in thread "main" java.lang.NullPointerException
.at pdsStat.PDSStatistics.main(PDSStatistics.java:15)    
where line number 15 is :

Code: Select all

Date modifDate = pms.modificationDate;
I could confirm that pms is created as a null object by inserting an if statement after the creation of pms to see if it was null. The if statement evaluates to true.
Debugging tip:
When you have eliminated all which is impossible, then whatever remains, however improbable, must be the truth.
--Sherlock Holmes
dovetail
Site Admin
Posts: 2022
Joined: Thu Jul 29, 2004 12:12 pm

Post by dovetail »

I think that the code is behaving as designed - the getStatistics() method returns null if there is no statistics. If it were changed to throw an exception, then it would break other applications that were expecting "null" to mean "not found".

My only suggestion would be to change your code to have something like this.

Code: Select all

if (pms != null) {
   Date modifDate = pms.modificationDate;
   System.out.println("Modification date : " + modifDate);  
}
Cogito-Ergo-Sum
Posts: 30
Joined: Thu Jan 25, 2007 8:31 am
Location: Bengaluru, India

Post by Cogito-Ergo-Sum »

ok.

Thank you.
Debugging tip:
When you have eliminated all which is impossible, then whatever remains, however improbable, must be the truth.
--Sherlock Holmes
Post Reply