반응형
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace study05_HW
{
public class Inventory
{
public int capacity;
public Item[] items;
public int index;
public Inventory(int capacity)
{
this.capacity = capacity;
this.items = new Item[this.capacity];
}
public void Additem(Item item)
{
this.items[this.index] = item;
Console.WriteLine("{0}인덱스에 {1}아이템을 넣었습니다.", this.index, item.GetName());
this.index++;
}
public void Print()
{
foreach (Item itemprint in this.items) {
Console.WriteLine("{0}, {1}", itemprint.GetName(), itemprint.GetItemType());
}
}
}
}
public void Print()
{
foreach (Item itemprint in this.items) {
Console.WriteLine("{0}, {1}", itemprint.GetName(), itemprint.GetItemType());
}
개체 참조가 개체의 인스턴스로 설정되지 않았습니다. 이 오류가 이해되지 않았습니다. //
이 말은 배열중에 널 값이 있을 경우 생기는 오류 였습니다.
해결
----> if (itemprint != null) 의 조건문을 걸어주면 됩니다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace study05_HW
{
public class Inventory
{
public int capacity;
public Item[] items;
public int index;
public Inventory(int capacity)
{
this.capacity = capacity;
this.items = new Item[this.capacity];
}
public void Additem(Item item)
{
this.items[this.index] = item;
Console.WriteLine("{0}인덱스에 {1}아이템을 넣었습니다.", this.index, item.GetName());
this.index++;
}
public void Print()
{
foreach (Item itemprint in this.items)
{
if (itemprint != null)
{
Console.WriteLine("{0}, {1}", itemprint.GetName(), itemprint.GetItemType());
}
}
}
}
}
반응형
'C# > 문제해결' 카테고리의 다른 글
0325_json 불러올 때 파일을 찾을 수가 없습니다. (0) | 2021.03.25 |
---|---|
0322_delegate// 행동하기전에 대리자 정의 안하면, 나중에 호출하면 생기는 오류 (0) | 2021.03.23 |
0317_인벤토리만들기, 인덱스가 배열 범위를 벗어났습니다. (0) | 2021.03.18 |
0317 _ 인벤토리, 맴버 변수 배열 만들기 (0) | 2021.03.17 |
0317 _ 콜렉션에서 //요소 출력 부분 막힌 것 (0) | 2021.03.17 |