Argument Structure
I have a object that I'm creating which takes a number of String parameters. The arguments themselves are calling back to another class to get the information that I want to present. Does this sound doable? I'm wondering what I'm doing wrong.
Below you'll see that elog[numLogs] is where I'm have problems. I get a NullPointerError when I compile. Thanks everyone!!
public void authResident(String passKey){
Resident[] o = getResidents();
int x = 0;
while (o[x] != null){
if (passKey.equals(o[x].getApartmentNumber() +
o[x].getLastDigitsPhone() +
o[x].getPassword())){
System.out.println("open Door!");
elog[numLogs] = new EntryHistory(
residents[x].getDateCreated(),
"New User Added",
residents[x].getApartmentNumber(),
residents[x].getFullName(),
"");
numLogs++;
}
x++;
}
}
[909 byte] By [
hallia] at [2007-11-26 17:13:59]

> I have a object that I'm creating which takes a
> number of String parameters. The arguments
> themselves are calling back to another class to get
> the information that I want to present. Does this
> sound doable? I'm wondering what I'm doing wrong.
Poor design and implementation.
> Below you'll see that elog[numLogs] is where I'm have
> problems. I get a NullPointerError when I compile.
> Thanks everyone!!
Because either the array is null or the entries in it are null.
EntryHistory seems badly done to me. It duplicates a lot of information that the Resident class already has. I think it would be better to have EntryHistory with Date, Resident, and an Event class.
An array of EntryHistory is limited. Better to have a List<EntryHistory>, which can accept as many entries as you like. An array is fixed in length.
%
I think this looks a lot cleaner:
package cruft;
import java.util.Date;
import java.util.List;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
/**
* Date: Feb 3, 2007
* Time: 10:13:49 PM
*/
public class ApartmentManager
{
private List<EntryEvent> eventLog;
private List<Resident> residents;
public Resident authorizeResident(String passKey) throws NotAuthorizedException
{
if (passKey == null)
throw new NotAuthorizedException("passkey cannot be null");
Resident authorized = null;
for (Resident resident : residents)
{
if (passKey.equals(resident.getPasskey()))
{
authorized = resident;
eventLog.add(new EntryEvent(new Date(), resident, EntryEventType.AUTHORIZE));
break;
}
}
return authorized;
}
}
class NotAuthorizedException extends RuntimeException
{
public NotAuthorizedException()
{
super();
}
public NotAuthorizedException(String string)
{
super(string);
}
public NotAuthorizedException(String string, Throwable throwable)
{
super(string, throwable);
}
public NotAuthorizedException(Throwable throwable)
{
super(throwable);
}
}
enum EntryEventType { ENTER, AUTHORIZE, DENY, EXIT, }
class EntryEvent implements Serializable
{
private static final DateFormat DEFAULT_DATE_FORMAT = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss");
private Date eventTime;
private Resident resident;
private EntryEventType entryEventType;
public EntryEvent(Date eventTime, Resident resident, EntryEventType entryEventType)
{
this.eventTime = eventTime;
this.resident = resident;
this.entryEventType = entryEventType;
}
public String toString()
{
return new StringBuilder().append("EntryEvent{").append("eventTime=").append(DEFAULT_DATE_FORMAT.format(eventTime)).append(", resident=").append(resident).append(", entryEventType=").append(entryEventType).append('}').toString();
}
}
class Resident implements Serializable
{
private String fullName;
private String apartmentNumber;
private String lastDigitsPhone;
private String password;
public Resident(String fullName, String apartmentNumber, String lastDigitsPhone, String password)
{
this.fullName = fullName;
this.apartmentNumber = apartmentNumber;
this.lastDigitsPhone = lastDigitsPhone;
this.password = password;
}
public String getFullName()
{
return fullName;
}
public String getApartmentNumber()
{
return apartmentNumber;
}
public String getLastDigitsPhone()
{
return lastDigitsPhone;
}
public String getPassword()
{
return password;
}
public String getPasskey()
{
StringBuilder passkey = new StringBuilder(512);
passkey.append(getApartmentNumber()).append(getLastDigitsPhone()).append(getPassword());
return passkey.toString();
}
}
%