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]
# 1

You don't get NPE when you compile. You get it when you run.

When you post code, please use[code] and [/code] tags as described in [url=http://forum.java.sun.com/help.jspa?sec=formatting]Formatting tips[/url] on the message entry page. It makes it much easier to read.

Indicate which line the NPE is occurring on.

jverda at 2007-7-8 23:41:57 > top of Java-index,Java Essentials,New To Java...
# 2

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

%

duffymoa at 2007-7-8 23:41:57 > top of Java-index,Java Essentials,New To Java...
# 3

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();

}

}

%

duffymoa at 2007-7-8 23:41:57 > top of Java-index,Java Essentials,New To Java...
# 4

You are correct that my design process is bad, actually terrible. I have a tendency to just start coding as I read the objectives. If you have any suggestions on what I can do in the future I would be very appreciative.

Your code basically looks like what the end product might look like at the end of the semester. Did you just bust that out? We are going to cover lists next week. Thank you for your quick response! Have a good one.

hallia at 2007-7-8 23:41:57 > top of Java-index,Java Essentials,New To Java...