Ciao a tutti, ho un funzione che mi "deserializza" un file json, mi tira fuori i dati e me li associa ad una mia classe
Ho già controllato e il file json viene salvato, quindi non è questo il problema.
L'errore si presenta solo in esecuzione e non in compilazione, praticamente crasha il programma e mi genera questo errore:
"Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'Impossibile convertire Null in 'int' perché è un tipo di valore non nullable'"
Grazie davvero...
C#:
//struttura classe:
public class Telefoni
{
private static List<string> _eletelefoni = new List<string>();
private string _seriale;
public string seriale
{
get { return _seriale; }
set
{
if (string.IsNullOrEmpty(value))
throw new Exception("Seriale non inserito");
_seriale = value;
}
}
private decimal _prezzoacquisto;
public decimal prezzoacquisto
{
get { return _prezzoacquisto; }
set
{
if (value < 0)
throw new Exception("Errore prezzo");
_prezzoacquisto = value;
}
}
public decimal prezzovendita { get; set; }
public int quantita { get; set; }
public string sim { get; set; }
public bool dualsim { get; set; }
public bool hspa { get; set; }
public bool lte { get; set; }
private int _download;
public int donwload
{
get { return _download; }
set
{
if (value < 0)
throw new Exception("Errore Download speed");
_download = value;
}
}
private int _upload;
public int upload
{
get { return _upload; }
set
{
if (value < 0)
throw new Exception("Errore Upload speed");
_upload = value;
}
}
public string marca { get; set; }
public string modello { get; set; }
public string processore { get; set; }
public int ram { get; set; }
public int memoriaint { get; set; }
public bool memoriaesp { get; set; }
public decimal pollici { get; set; }
public string risoluzione { get; set; }
public string tipodisplay { get; set; }
public int fotoant { get; set; }
public int fotopost { get; set; }
public bool face { get; set; }
public string videorec { get; set; }
public bool wifi { get; set; }
public bool bluetooth { get; set; }
public bool irda { get; set; }
public bool nfc { get; set; }
public bool accelerometro { get; set; }
public bool prossimita { get; set; }
public bool giroscopio { get; set; }
public bool bussola { get; set; }
public bool impronta { get; set; }
public string tipobatteria { get; set; }
public int capacitabatteria { get; set; }
private bool disposed = false;
public virtual void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
}
_eletelefoni.Remove(seriale);
disposed = true;
}
}
~Telefoni()
{
Dispose(false);
}
public Telefoni(string serial, decimal prezzoacq, decimal prezzovend, int quant,
string si, bool dual, bool h, bool g, int maxdown, int maxup,
string mar, string mod, string proces, int r, int memint, bool memesp, decimal pol,
string ris, string tipodis, int fant, int fpost, bool fac, string video, bool wi, bool blue,
bool ir, bool nf, bool acc, bool prox, bool gir, bool bus, bool impr, string tipobat, int capbat)
{
if (_eletelefoni.Contains(serial))
throw new Exception("Codice seriale già inserito");
this._seriale = serial;
this.prezzoacquisto = prezzoacq;
this.prezzovendita = prezzovend;
this.quantita = quant;
this.sim = si;
this.dualsim = dual;
this.hspa = h;
this.lte = g;
this.donwload = maxdown;
this.upload = maxup;
this.marca = mar;
this.modello = mod;
this.processore = proces;
this.ram = r;
this.memoriaint = memint;
this.memoriaesp = memesp;
this.pollici = pol;
this.risoluzione = ris;
this.tipodisplay = tipodis;
this.fotoant = fant;
this.fotopost = fpost;
this.face = fac;
this.videorec = video;
this.wifi = wi;
this.bluetooth = blue;
this.irda = ir;
this.nfc = nf;
this.accelerometro = acc;
this.prossimita = prox;
this.giroscopio = gir;
this.bussola = bus;
this.impronta = impr;
this.tipobatteria = tipobat;
this.capacitabatteria = capbat;
_eletelefoni.Add(serial);
}
}
//funzione che mi apre il file json
private void aPRIToolStripMenuItem_Click(object sender, EventArgs e)
{
var telefonifile = JsonConvert.DeserializeObject<dynamic>(File.ReadAllText("telefoni.json"));
eletelefoni.Clear();
foreach (var tel in telefonifile)
{
var nuovotelefono = new Telefoni((string)tel.seriale, (decimal)tel.prezzoacquisto, (decimal)tel.prezzovendita,
(int)tel.quantita, (string)tel.sim, (bool)tel.dualsim, (bool)tel.hspa, (bool)tel.lte, (int)tel.download,
(int)tel.upload, (string)tel.marca, (string)tel.modello, (string)tel.processore, (int)tel.ram, (int)tel.memoriaint,
(bool)tel.memoriaesp, (decimal)tel.pollici, (string)tel.risoluzione, (string)tel.tipodisplay, (int)tel.fotoant,
(int)tel.fotopost, (bool)tel.face, (string)tel.videorec, (bool)tel.wifi, (bool)tel.bluetooth, (bool)tel.irda,
(bool)tel.nfc, (bool)tel.accelerometro, (bool)tel.prossimita, (bool)tel.giroscopio, (bool)tel.bussola, (bool)tel.impronta,
(string)tel.tipobatteria, (int)tel.capacitabatteria);
eletelefoni.Add(telefonifile);
};
tabControl1.SelectedIndex = -1;
tabControl1.SelectedIndex = 0;
}
//funzione che salva:
private void sALVAToolStripMenuItem_Click(object sender, EventArgs e)
{
File.WriteAllText(@"telefoni.json", JsonConvert.SerializeObject(eletelefoni, Formatting.Indented));
}
L'errore si presenta solo in esecuzione e non in compilazione, praticamente crasha il programma e mi genera questo errore:
"Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'Impossibile convertire Null in 'int' perché è un tipo di valore non nullable'"
Grazie davvero...