1

I need to split string to an array. And I get object reference not set to an instance of an object when I try to add new object to the array:

array<d3^> ^pr_d3; //d3 - class
parts = sr->ReadLine()->Split(
    (array<String^>^)nullptr, 
    StringSplitOptions::RemoveEmptyEntries); //array<String ^> ^parts;

pr_d3[0] = gcnew d3(
     parts[0], parts[1], 
     parts[2],
     Convert::ToInt16(parts[3]), Convert::ToInt16(parts[4])); //error

Code on Ideon

2
  • 1
    @tobi303 The tag C++ was wrong as this is actually C++/CLI (en.wikipedia.org/wiki/C%2B%2B/CLI) Commented Feb 17, 2016 at 10:18
  • 1
    What is criteria of Split? Can u provide normal source code on ideone.com? Commented Feb 17, 2016 at 10:19

1 Answer 1

1

You're not initializing the array pr_d3.

Removing the correct code, you currently have this:

array<d3^> ^pr_d3;
pr_d3[0] = gcnew d3(...);

The error is trying to access [0] of pr_d3, but pr_d3 is still null.

You need to initialize pr_d3 with gcnew array<d3^>(<some array size>), or if you're not sure of the size you'll need, use a List<d3^> instead.

Sign up to request clarification or add additional context in comments.

Comments

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.