G.TheHamleter
17-10-2014, 16:52
Salve a tutti,
Vorrei postare parte di codice di un'applicazione in java perchè, facendo funzionare JUnit mi dà errore java.lang.NullPointerException :
public class AppointmentCollection
{
private Appointment[] lista; //array di Appointment
//Costruttore per dimensione array
public AppointmentCollection(int dim)
{
this.lista = new Appointment[dim]; //ho definito l'array ma ancora vuoto
}
public Appointment[] getLista()
{
return this.lista;
}
//metodo di aggiunta elemento in array
public int add(Appointment app)
{
int position = -1;
int i = 0;
do
{
if(this.lista[i].equals(null)):help:java.lang.nullpointerexception
{
this.lista[i] = app;
position = i;
}
else
{
i++;
}
}
while(i < this.lista.length && position == -1);
return position;
}
....//
Vi posto anche la classe Appointment di cui è composizione
import java.text.DateFormat;
public class Appointment
{
/**
Campi esemplare : stato appuntamento rappresentato da descrizione a parole e da orario
*/
private String Description;
private Date From;
private Date To;
// da uml noto che unico campo esemplare è description??
public Appointment()
{
}
/**
Inizializzo il mio oggetto Appointment
@param1 stringa descrittiva appuntamento
@param2 millisec per inizializzare Date
@param3 millisec per inizializzare Date
*/
public Appointment(String kind,long start, long finish)
{
this.Description = kind;
this.From = new Date(start);
this.To = new Date(finish);
}
/**
metodi accessor e modificatori
metodi accessor
* @return descrizione o Date; */
public String getDescription() {
return this.Description;
}
public void setDescription(String description) {
this.Description = description;
}
public Date getFrom() {
return this.From;
}
public void setFrom(Date from) {
this.From = from;
}
public Date getTo() {
return this.To;
}
public void setTo(Date to) {
this.To = to;
}
public int getDuration()
{
long duration = this.getTo().getTime() - this.getFrom().getTime();
return (int) (duration*10^-3/60);
}
public void setDuration(int time)
{
long tiiime = time;
long duration = this.getFrom().getTime() + tiiime ;
this.getTo().setTime(duration);
}
public String toString()
{
DateFormat dateFormatter = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
//ho ottenuto formattatore data ed ora
return this.getDescription() + dateFormatter.format(this.getFrom()) + dateFormatter.format(this.getTo());
}
public boolean equals(Appointment a)
{
DateFormat dateFormatter = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
return(this.getDescription().equals(a.getDescription()) && dateFormatter.format(this.getFrom()).equals(dateFormatter.format(a.getFrom()))
&& dateFormatter.format(this.getTo()).equals(dateFormatter.format(a.getTo())));
}
Vi ringrazio
}
Vorrei postare parte di codice di un'applicazione in java perchè, facendo funzionare JUnit mi dà errore java.lang.NullPointerException :
public class AppointmentCollection
{
private Appointment[] lista; //array di Appointment
//Costruttore per dimensione array
public AppointmentCollection(int dim)
{
this.lista = new Appointment[dim]; //ho definito l'array ma ancora vuoto
}
public Appointment[] getLista()
{
return this.lista;
}
//metodo di aggiunta elemento in array
public int add(Appointment app)
{
int position = -1;
int i = 0;
do
{
if(this.lista[i].equals(null)):help:java.lang.nullpointerexception
{
this.lista[i] = app;
position = i;
}
else
{
i++;
}
}
while(i < this.lista.length && position == -1);
return position;
}
....//
Vi posto anche la classe Appointment di cui è composizione
import java.text.DateFormat;
public class Appointment
{
/**
Campi esemplare : stato appuntamento rappresentato da descrizione a parole e da orario
*/
private String Description;
private Date From;
private Date To;
// da uml noto che unico campo esemplare è description??
public Appointment()
{
}
/**
Inizializzo il mio oggetto Appointment
@param1 stringa descrittiva appuntamento
@param2 millisec per inizializzare Date
@param3 millisec per inizializzare Date
*/
public Appointment(String kind,long start, long finish)
{
this.Description = kind;
this.From = new Date(start);
this.To = new Date(finish);
}
/**
metodi accessor e modificatori
metodi accessor
* @return descrizione o Date; */
public String getDescription() {
return this.Description;
}
public void setDescription(String description) {
this.Description = description;
}
public Date getFrom() {
return this.From;
}
public void setFrom(Date from) {
this.From = from;
}
public Date getTo() {
return this.To;
}
public void setTo(Date to) {
this.To = to;
}
public int getDuration()
{
long duration = this.getTo().getTime() - this.getFrom().getTime();
return (int) (duration*10^-3/60);
}
public void setDuration(int time)
{
long tiiime = time;
long duration = this.getFrom().getTime() + tiiime ;
this.getTo().setTime(duration);
}
public String toString()
{
DateFormat dateFormatter = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
//ho ottenuto formattatore data ed ora
return this.getDescription() + dateFormatter.format(this.getFrom()) + dateFormatter.format(this.getTo());
}
public boolean equals(Appointment a)
{
DateFormat dateFormatter = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
return(this.getDescription().equals(a.getDescription()) && dateFormatter.format(this.getFrom()).equals(dateFormatter.format(a.getFrom()))
&& dateFormatter.format(this.getTo()).equals(dateFormatter.format(a.getTo())));
}
Vi ringrazio
}