AnthonyTex
08-09-2010, 13:46
Buongiorno a tutti sono alle prime armi nella programmazione in java e non riesco ad uscire da questa situzione: quando vado ad eseguire questo programma mi viene restituito l'errore NullPointerException qualcuno potrebbe gentilmente aiutarmi?:D grazie mille
public class MioDiz
{
final int MAX = 100;
public Coppia[] v = new Coppia[MAX];
private int taglia = 0;
private int cerca(String s)
{
for(int i=0; i<v.length; i++)
{
if (v[i].chiave.compareTo(s) == 0)
{ return i;}
}
return -1;
}
public void nuovo(String z) throws ErrorException
{ System.out.println(z);
if (cerca(z) == -1)
{v[taglia].chiave= z;
v[taglia].attributo = 0.0;
taglia++;
}
else
{ throw new ErrorException();}
}
public void versa(String s, double d) throws ErrorException
{
int i = cerca(s);
if (i != -1)
{ v[i].attributo += d; }
if (i == -1)
{ throw new ErrorException();}
}
public void preleva(String s, double d) throws ErrorException
{
int i = cerca(s);
if (i == -1)
{throw new ErrorException();}
if (i != -1)
{
if ((v[i].attributo - d) < 0 )
{throw new ErrorException();}
else
{ v[i].attributo = v[i].attributo - d;}
}
}
public void cancella(String s) throws ErrorException
{
int i = cerca(s);
if (i == -1)
{ throw new ErrorException();}
if (v[i].attributo != 0)
{throw new ErrorException();}
for (int j = i; j< taglia -1; j++)
{ v[j] = v[j +1]; }
}
public void saldo(String s) throws ErrorException
{ int i = cerca(s);
if(i == -1)
{throw new ErrorException();}
System.out.println(v[i].toString());
}
public String [] stampa()
{ String [] et = new String[MAX];
for (int k = 0; k< taglia; k++)
{
et[k] = v[k].toString(); }
return et;
}
}
public class MioDiz
{
final int MAX = 100;
public Coppia[] v = new Coppia[MAX];
private int taglia = 0;
private int cerca(String s)
{
for(int i=0; i<v.length; i++)
{
if (v[i].chiave.compareTo(s) == 0)
{ return i;}
}
return -1;
}
public void nuovo(String z) throws ErrorException
{ System.out.println(z);
if (cerca(z) == -1)
{v[taglia].chiave= z;
v[taglia].attributo = 0.0;
taglia++;
}
else
{ throw new ErrorException();}
}
public void versa(String s, double d) throws ErrorException
{
int i = cerca(s);
if (i != -1)
{ v[i].attributo += d; }
if (i == -1)
{ throw new ErrorException();}
}
public void preleva(String s, double d) throws ErrorException
{
int i = cerca(s);
if (i == -1)
{throw new ErrorException();}
if (i != -1)
{
if ((v[i].attributo - d) < 0 )
{throw new ErrorException();}
else
{ v[i].attributo = v[i].attributo - d;}
}
}
public void cancella(String s) throws ErrorException
{
int i = cerca(s);
if (i == -1)
{ throw new ErrorException();}
if (v[i].attributo != 0)
{throw new ErrorException();}
for (int j = i; j< taglia -1; j++)
{ v[j] = v[j +1]; }
}
public void saldo(String s) throws ErrorException
{ int i = cerca(s);
if(i == -1)
{throw new ErrorException();}
System.out.println(v[i].toString());
}
public String [] stampa()
{ String [] et = new String[MAX];
for (int k = 0; k< taglia; k++)
{
et[k] = v[k].toString(); }
return et;
}
}