Avatar
Maros2470
Člen
Avatar
Maros2470:

Mám dotaz. Při spouštění aplikace se mi zobrazí chyba: "Object reference not set to an instance of an object". Kde jsem udělal chybu?

Mám WinXP 32 bit bude to aplikace chodit na Win7, případně Win8 32, nebo 64 bit? Tvořil jsem to ve VS 2010 profi.

 
Odpovědět 18.3.2014 20:02
Avatar
juraj
Člen
Avatar
Odpovídá na Maros2470
juraj:

Ukaz zdrojovy kod :)

Editováno 19.3.2014 18:02
 
Nahoru Odpovědět 19.3.2014 18:02
Avatar
Odpovídá na Maros2470
Michal Žůrek (misaz):

protože odkazuješ na nic, podívej se kde chyba vznikne a oprav si to.

Nahoru Odpovědět  +1 19.3.2014 18:46
Nesnáším {}, proto se jim vyhýbám.
Avatar
Maros2470
Člen
Avatar
Odpovídá na juraj
Maros2470:

Je to moje první aplikace. Začal jse s programováním, protože tuhle aplikaci potřebuji k práci. Tady je kód:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Revize_Sp1
{
    public class Spotrebic
    {
        public string Customer { get; set; }
        public string Place_1 { get; set; }
        public string Place_2 { get; set; }
        public string Place_3 { get; set; }
        public string Place_4 { get; private set; }
        public string Id { get; private set; }
        public string Name { get; private set; }
        public string SortingKey { get; private set; }
        public string InvertoryNumber { get; private set; }
        public string SerialNumber { get; private set; }
        public int RevisionInterval { get; private set; }
        public DateTime TakenIntoEvidence { get; private set; }
        public string Note { get; private set; }
        public string Group { get; private set; }
        public string ProtectionClass { get; private set; }
        public DateTime MeasDate { get; private set; }
        public string MeasGlobalNote { get; private set; }
        public string MeasGlobalStatus { get; private set; }
        public string InspectorName { get; private set; }
        public string InspectorSecondName { get; private set; }
        public string InspectorLicense { get; private set; }
        public string DeviceSerialNum { get; private set; }
        public string DeviceType { get; private set; }
        public string DeviceCalibrationNum { get; private set; }
        public DateTime DeviceCalibrationExpire { get; private set; }
        public string SupplFirm { get; private set; }
        public string SupplIC { get; private set; }







        public Spotrebic()
        {
        }
        public Spotrebic(string customer, string place_1, string place_2, string place_3, string place_4, string id, string name, string sortingKey, string invertoryNumber,
                         string serialNumber, int revisionInterval, DateTime takenIntoEvidence, string note, string group, string protectionClass, DateTime measDate,
                         string measGlobalNote, string measGlobalStatus, string inspectorName, string inspectorSecondName, string inspectorLicense, string deviceSerialNum,
                         string deviceType, string deviceCalibratinNum, DateTime deviceCalibrationExpire, string supplFirm, string supplIC)
        {
            Customer = customer;
            Place_1 = place_1;
            Place_2 = place_2;
            Place_3 = place_3;
            Place_4 = place_4;
            Id = id;
            Name = name;
            SortingKey = sortingKey;
            InvertoryNumber = invertoryNumber;
            SerialNumber = serialNumber;
            RevisionInterval = revisionInterval;
            TakenIntoEvidence = takenIntoEvidence;
            Note = note;
            Group = group;
            ProtectionClass = protectionClass;
            MeasDate = measDate;
            MeasGlobalNote = measGlobalNote;
            MeasGlobalStatus = measGlobalStatus;
            InspectorName = inspectorName;
            InspectorSecondName = inspectorSecondName;
            InspectorLicense = inspectorLicense;
            DeviceSerialNum = deviceSerialNum;
            DeviceType = deviceType;
            DeviceCalibrationNum = deviceCalibratinNum;
            DeviceCalibrationExpire = deviceCalibrationExpire;
            SupplFirm = supplFirm;
            SupplIC = supplIC;


        }

        public override string ToString()
        {

            return Name;

        }

        public DateTime SpoctiRevizi()
        {
            DateTime naslednaRevize = MeasDate.AddMonths(+RevisionInterval);
            return naslednaRevize;
        }

        public DateTime SpoctiRevizi1()
        {
            DateTime naslednaRevize = SpoctiRevizi().AddMonths(- 1);
            return naslednaRevize;
        }

        public DateTime SpoctiRevizi2()
        {
            DateTime naslednaRevize = SpoctiRevizi().AddMonths( - 2);
            return naslednaRevize;
        }
    }

}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;


namespace Revize_Sp1
{
    public class Databaze
    {
        public List<Spotrebic> spotrebice;
        public string soubor;
        public Databaze(string soubor)
        {
            spotrebice = new List<Spotrebic>();
            this.soubor = soubor;
        }

        public void PridejSpotrebic(string customer, string place_1, string place_2, string place_3, string place_4, string id, string name, string sortingKey, string invertoryNumber,
                                    string serialNumber, int revisionInterval, DateTime takenIntoEvidence, string note, string group, string protectionClass, DateTime measDate,
                                    string measGlobalNote, string measGlobalStatus, string inspectorName, string inspectorSecondName, string inspectorLicense,
                                    string deviceSerialNum, string deviceType, string deviceCalibratinNum, DateTime deviceCalibrationExpire, string supplFirm, string supplIC)
        {
            Spotrebic u = new Spotrebic(customer, place_1, place_2, place_3, place_4, id, name, sortingKey, invertoryNumber, serialNumber, revisionInterval, takenIntoEvidence,
                                        note, group, protectionClass, measDate, measGlobalNote, measGlobalStatus, inspectorName, inspectorSecondName, inspectorLicense,
                                        deviceSerialNum, deviceType, deviceCalibratinNum, deviceCalibrationExpire, supplFirm, supplIC);
            spotrebice.Add(u);

        }

        public Spotrebic[] VratVsechny()
        {
            return spotrebice.ToArray();
        }


        public void Uloz()
        {
            using (StreamWriter sw = new StreamWriter(soubor))
            {
                // projetí uživatelů
                foreach (Spotrebic sp in spotrebice)
                {
                    // vytvoření pole hodnot
                    string[] hodnoty = { sp.Customer, sp.Place_1, sp.Place_2, sp.Place_3, sp.Place_4, sp.Id, sp.Name, sp.SortingKey, sp.InvertoryNumber, sp.SerialNumber,
                                         sp.RevisionInterval.ToString(), sp.TakenIntoEvidence.ToShortDateString(), sp.Note, sp.Group, sp.ProtectionClass, sp.MeasDate.ToShortDateString(),
                                         sp.MeasGlobalNote, sp.MeasGlobalStatus, sp.InspectorName, sp.InspectorSecondName, sp.InspectorLicense, sp.DeviceSerialNum, sp.DeviceType,
                                         sp.DeviceCalibrationNum, sp.DeviceCalibrationExpire.ToShortDateString(), sp.SupplFirm, sp.SupplIC};
                    // vytvoření řádku
                    string radek = String.Join(";", hodnoty);
                    // zápis řádku
                    sw.WriteLine(radek);
                }
                // vyprázdnění bufferu
                sw.Flush();
            }
        }



        public void Nacti()
        {
            spotrebice.Clear();


            // načte soubor
            using (StreamReader sr = new StreamReader(@"revize.csv", Encoding.Default))
            {
                string s;
                // čte po řádcích
                while ((s = sr.ReadLine()) != null)
                {

                    string[] rozdeleno = s.Split(';');
                    string customer = rozdeleno[0];
                    string place_1 = rozdeleno[1];
                    string place_2 = rozdeleno[2];
                    string place_3 = rozdeleno[3];
                    string place_4 = rozdeleno[4];
                    string id = rozdeleno[5];
                    string name = rozdeleno[6];
                    string sortingKey = rozdeleno[7];
                    string invertoryNumber = rozdeleno[8];
                    string serialNumber = rozdeleno[9];
                    int revisionInterval = int.Parse(rozdeleno[10]);
                    DateTime takenIntoEvidence = DateTime.Parse(rozdeleno[11]);
                    string note = rozdeleno[12];
                    string group = rozdeleno[13];
                    string protectionClass = rozdeleno[14];
                    DateTime measDate = DateTime.Parse(rozdeleno[15]);
                    string measGlobalNote = rozdeleno[16];
                    string measGlobalStatus = rozdeleno[17];
                    string inspectorName = rozdeleno[18];
                    string inspectorSecondName = rozdeleno[19];
                    string inspectorLicense = rozdeleno[20];
                    string deviceSerialNum = rozdeleno[21];
                    string deviceType = rozdeleno[22];
                    string deviceCalibratinNum = rozdeleno[23];
                    DateTime deviceCalibrationExpire = DateTime.Parse(rozdeleno[24]);
                    string supplFirm = rozdeleno[25];
                    string supplIC = rozdeleno[26];



                    PridejSpotrebic(customer, place_1, place_2, place_3, place_4, id, name, sortingKey, invertoryNumber, serialNumber, revisionInterval, takenIntoEvidence,
                                        note, group, protectionClass, measDate, measGlobalNote, measGlobalStatus, inspectorName, inspectorSecondName, inspectorLicense,
                                        deviceSerialNum, deviceType, deviceCalibratinNum, deviceCalibrationExpire, supplFirm, supplIC);
                }
            }
        }

    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Revize_Sp1
{
    public partial class RSpForm : Form
    {

        public Databaze databaze;
        public RSpForm()
        {
            InitializeComponent();
            try
            {
                databaze.VratVsechny();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }





            databaze = new Databaze(@"revize.csv");
            dnesniToolStripStatusLabel.Text = DateTime.Now.ToLongDateString();
        }

        public void ulozButton_Click(object sender, EventArgs e)
        {
            try
            {
                databaze.Uloz();
            }
            catch
            {
                MessageBox.Show("Databázi se nepodařilo uložit, zkontrolujte přístupová práva k souboru.",
                        "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void pridejButton_Click(object sender, EventArgs e)
        {
            string customer = customerTextBox.Text;
            string place_1 = place_1TextBox.Text;
            string place_2 = place_2TextBox.Text;
            string place_3 = place_3TextBox.Text;
            string place_4 = place_4TextBox.Text;
            string id = idTextBox.Text;
            string name = nameTextBox.Text;
            string sortingKey = sortingKeyTextBox.Text;
            string invertoryNumber = invertoryNumberTextBox.Text;
            string serialNumber = serialNumberTextBox.Text;
            int revisionInterval = Convert.ToInt32(revisionIntervalTextBox.Text);
            DateTime takenIntoEvidence = Convert.ToDateTime(takenIntoEvidenceTextBox.Text);
            string note = noteListView.Text;
            string group = groupTextBox.Text;
            string protectionClass = protectionClassTextBox.Text;
            DateTime measDate = Convert.ToDateTime(measDateTextBox.Text);
            string measGlobalNote = measGlobalNoteListView.Text;
            string measGlobalStatus = measGlobalStatusLabel.Text;
            string inspectorName = inspectorNameTextBox.Text;
            string inspectorSecondName = inspectorSecondNametextBox.Text;
            string inspectorLicense = inspectorLicenseTextBox.Text;
            string deviceSerialNum = deviceSerialNumTextBox.Text;
            string deviceType = deviceTypeTextBox.Text;
            string deviceCalibratinNum = deviceCalibrationNumTextBox.Text;
            DateTime deviceCalibrationExpire = Convert.ToDateTime(deviceCalibrationExpireTextBox.Text);
            string supplFirm = supplFirmTextBox.Text;
            string supplIC = supplICTextBox.Text;


            databaze.PridejSpotrebic(customer, place_1, place_2, place_3, place_4, id, name, sortingKey, invertoryNumber, serialNumber, revisionInterval, takenIntoEvidence,
                                        note, group, protectionClass, measDate, measGlobalNote, measGlobalStatus, inspectorName, inspectorSecondName, inspectorLicense,
                                        deviceSerialNum, deviceType, deviceCalibratinNum, deviceCalibrationExpire, supplFirm, supplIC);
            kontrolaListBox.Items.Add(new Spotrebic(customer, place_1, place_2, place_3, place_4, id, name, sortingKey, invertoryNumber, serialNumber, revisionInterval, takenIntoEvidence,
                                        note, group, protectionClass, measDate, measGlobalNote, measGlobalStatus, inspectorName, inspectorSecondName, inspectorLicense,
                                        deviceSerialNum, deviceType, deviceCalibratinNum, deviceCalibrationExpire, supplFirm, supplIC));
        }

        public void kontrolaListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (kontrolaListBox.SelectedItem != null)
            {
                Spotrebic sp = (Spotrebic)kontrolaListBox.SelectedItem;

                measGlobalStatusLabel.Text = sp.Name;
                customerTextBox.Text = sp.Customer;
                place_1TextBox.Text = sp.Place_1;
                place_2TextBox.Text = sp.Place_2;
                place_3TextBox.Text = sp.Place_3;
                place_4TextBox.Text = sp.Place_4;
                idTextBox.Text = sp.Id;
                nameLabel.Text = sp.Name;
                measGlobalStatusLabel.Text = sp.Name;
                invertoryNumberTextBox.Text = sp.InvertoryNumber;
                serialNumberTextBox.Text = sp.SerialNumber;
                revisionIntervalTextBox.Text = sp.RevisionInterval.ToString();
                groupTextBox.Text = sp.Group;
                protectionClassTextBox.Text = sp.ProtectionClass;
                measDateTextBox.Text = sp.MeasDate.ToShortDateString();
                dalsiRevizeTextBox.Text = sp.SpoctiRevizi().ToShortDateString();
                inspectorNameTextBox.Text = sp.InspectorName;
                inspectorSecondNametextBox.Text = sp.InspectorSecondName;
                inspectorLicenseTextBox.Text = sp.InspectorLicense;
                deviceTypeTextBox.Text = sp.DeviceType;
                deviceSerialNumTextBox.Text = sp.DeviceSerialNum;
                deviceCalibrationNumTextBox.Text = sp.DeviceCalibrationNum;
                deviceCalibrationExpireTextBox.Text = sp.DeviceCalibrationExpire.ToShortDateString();
                supplFirmTextBox.Text = sp.SupplFirm;
                supplICTextBox.Text = sp.SupplIC;
                noteListView.Text = sp.Note;
                measGlobalNoteListView.Text = sp.MeasGlobalNote;
                measGlobalStatusLabel.Text = sp.MeasGlobalStatus;
                takenIntoEvidenceTextBox.Text = sp.TakenIntoEvidence.ToShortDateString();
                sortingKeyTextBox.Text = sp.SortingKey;

                if (sp.SpoctiRevizi() < DateTime.Now)
                    measGlobalStatusLabel.ForeColor = Color.Red;

                if (sp.SpoctiRevizi() > DateTime.Now)
                    measGlobalStatusLabel.ForeColor = Color.Green;

                if (noteListView.Text == "Nerevidovat")
                    measGlobalStatusLabel.ForeColor = Color.White;



                if (sp.SpoctiRevizi() < DateTime.Now)
                    dalsiRevizeTextBox.BackColor = Color.Red;
                else
                    dalsiRevizeTextBox.BackColor = Color.Green;

                if (measGlobalStatusLabel.Text == "Vyhovuje")
                    measGlobalStatusLabel.ForeColor = Color.Green;
                else
                    measGlobalStatusLabel.ForeColor = Color.Red;

                if (noteListView.Text == "Nerevidovat")
                    measGlobalStatusLabel.Text = "Vyřazeno";

                else

                    measGlobalStatusLabel.Text = sp.MeasGlobalStatus;

                if (noteListView.Text == "Nerevidovat")
                    measGlobalStatusLabel.ForeColor = Color.White;

                if (sp.SpoctiRevizi1() >= DateTime.Now)
                   nameLabel.BackColor = Color.Indigo;
                else
                   nameLabel.BackColor = Color.Yellow;




            }


        }

        public void nactiButton_Click(object sender, EventArgs e)
        {
            try
            {
                databaze.Nacti();
                kontrolaListBox.Items.Clear();
                kontrolaListBox.Items.AddRange(databaze.VratVsechny());
            }
            catch
            {
                MessageBox.Show("Databázi se nepodařilo načíst, soubor zřejmě neexisituje.",
                    "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }


    }
}
 
Nahoru Odpovědět 20.3.2014 8:31
Avatar
Maros2470
Člen
Avatar
Maros2470:

Už jsem to našel

 
Nahoru Odpovědět 20.3.2014 8:46
Děláme co je v našich silách, aby byly zdejší diskuze co nejkvalitnější. Proto do nich také mohou přispívat pouze registrovaní členové. Pro zapojení do diskuze se přihlas. Pokud ještě nemáš účet, zaregistruj se, je to zdarma.

Zobrazeno 5 zpráv z 5.