0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
namespace shop_management
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public class user
        {
            public string İsim { set; get; }
            public string Borç { set; get; }

        }
        public class item
        {
            public string Tehlike { set; get; }
            public string İsim { set; get; }
            public decimal Birim { set; get; }
            public decimal Miktar { set; get; }

        }
        public MainWindow()
        {
            this.InitializeComponent();
            if (!(File.Exists("C:\\data\\users"))) //here the software checks if the files exist or not
            {
                MessageBox.Show("Error!!!");
                this.Hide();
            }
            if (!(File.Exists("C:\\data\\items")))
            {
                MessageBox.Show("Error!!!");
                this.Hide();
            }
            string dan_in="";
            StreamReader sr_main;
            int i = 0,j=0;
            List<item> list_items = new List<item>();
            string first_read;
            sr_main = File.OpenText("C:\\data\\items"); //this is the file that we take our items data
            first_read = sr_main.ReadToEnd();
            string[] manip_read = first_read.Split('\t'); //in the file, there is only "/t"s between datas. for example "name1/tprice1/tcount1/tdanger1t/name2/tprice2/tcount2/tdanger2" etc.
            item[] items = new item[300]; //here i declared a 300 items long array, which means the software will crash if there is 301, solve this!
            foreach (string line in manip_read)
            {


                if (i == 0) items[j].İsim = line; //this line keeps record of the items name
                if (i == 1) items[j].Birim = Convert.ToDecimal(line); // this line keeps the price
                if (i == 2) items[j].Miktar = Convert.ToDecimal(line); // this line keeps how many left
                if (i == 3) items[j].Tehlike = line; //and this line keeps the danger level
                i++;
                if (i == 4) //here the loop adds the data to list
                {
                    if (items[j].Miktar < Convert.ToDecimal(items[j].Tehlike) || items[j].Miktar == Convert.ToDecimal(items[j].Tehlike)) dan_in = "!!!";
                    list_items.Add(new item() { İsim = items[j].İsim, Miktar =items[j].Miktar , Birim=items[j].Birim, Tehlike=dan_in });
                    dan_in = "";
                    i = 0;
                    j++;
                }

            }
            grid_items.ItemsSource = list_items;
        }
    }
}

Here the problem is, i ran this part of the software before, but without the items[300] array. At that time, there is only one instance of item, but now i also need to keep them in a array. It seems there is an error with the first if statement that tries to assign the name İsim value of the first item (item[0]).

thanks for any help

2 Answers 2

4

You're not assigning a value to the element itself - somewhere you need:

items[j] = new item();

Currently you're just trying to set properties on an object that doesn't exist - i.e. via a null reference.

(I would agree with the idea of using a List<item> as well, by the way. I'd also rename item to Item to fit in with .NET naming conventions.)

Given your current code, the simplest solution is probably this:

if (i == 0)
{
    items[j] = new item();
    items[j].İsim = line;
}
Sign up to request clarification or add additional context in comments.

2 Comments

ok than i believe my code is not the "debugabble" type :D thanks anyway :D
@gkaykck: Yes, I would seriously think about trying to refactor your code to be more readable. Things like only declaring variables when you need them - and breaking up methods into smaller ones.
0
item[] items = new item[300]; //here i declared a 300 items long array, which means the software will crash if there is 301, solve this!

Use a dynamic list instead of a fixed length array:

List<item> items = new List<item>();

1 Comment

but also i think i can only user the list_items for manipulating the item counts. i'll try then, thanks again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.