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
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.
Related
I have the following classes and objects defined in Java
class HouseStructures
{
String roof;
String windows;
String doors;
}
HouseStructures structure1=new HouseStructures();
HouseStructures structure2 = new HouseStructures();
structure1.doors = "wood";structure1.windows="aluminium"; structure1.roof="tiles";
structure2.doors = "steel"; structure2.windows = "iron"; structure2.roof="zinc";
class Estates
{
String description;
ArrayList<HouseStructures> housestructures;
}
Estates estates = new Estates();
estates.description="fancycomplex";
estates.housestructures.add(structure1);
estates.housestructures.add(structure2);
When I get to the last couple of statements, the programme crashes and I get the error message
java.lang.NullPointerException: Attempt to invoke virtual method 'void java.util.ArrayList.add(int, java.lang.Object)' on a null object reference
How can i populate the estates.housestructure with elements. I also would like to understand why I get a null object reference when there is a fully populated object being passed as an argument.
I have many class in my library almost 300, and I want to generate instance name by loop. In other words I wanna instead of this approach(witch has many code line):
X:This is my problem:
var cm1: Cm1 = new Cm1();
var cm2: Cm2 = new Cm2();
var cm3: Cm3 = new Cm3();
...
use like this approach (less than 10 code lines):
Y:I think this is solution:
for (var i: uint = 1; i < 4; i++)
{
var getChildByName("cm" + i): getChildByName("Cm" + i) = new getChildByName("Cm" + i);
}
but I know above code does not work, is there any way to make them !
-What am I actually trying to solve?
Make many variable by a few line code and save time and size app!
-Why do I have ~300 classes and why are you trying to create them in a loop at once?
This is about to data of request application!
-What do these classes do that you unconditionally need one of each all at the same time?
Because those data would show at first time!
First, it is better to store the classes in an Array or Object rather than an external variable for each item. if you want to access them by name, better to use object:
var classList:Object=new Object();
Then in your loop:
for(var i:uint=1;i<NumberOfClasses;i++){
classList["cm"+i.toString()]=new (getDefinitionByName("Cm"+i.toString()) as Class)();
}
getDefinitionByName is used to make Constructors using String;
Note: if your classes contain a package directory, you should include it. for example:
getDefinitionByName("myclasses.cm.Cm123")();
Then you can access them using Bracket syntax:
classList["cm123"].prop=val;
And don't forget to:
import flash.utils.getDefinitionByName;
I Hope it will help.
EDIT
to use Array instead of object, the loop should be:
for(var i:uint=1;i<NumberOfClasses;i++){
classList[i]=new (getDefinitionByName("Cm"+i.toString()) as Class)();
}
then to access them:
addChild(classList[0]);//0 or any other index;
I am learning how to build apps and working with Swift for this project.
I had a buddy help me pull data in from a website and it looks like he created classes with variables and mapped them to certain extensions (IE "Username") so when I call the variable data such as profile I would call it. The below uses luck_30 able to store "Stats.luck_30"
luck_30.text = profile.luck_30
So inside one of my variables that is in this "Profile" class is setup into an array. I can pull the array out of the class, but I can't seem to do for while statement replacing the [#] with a variable from the for command.
func aliveWorkers(profile: Profile) -> NSNumber{
var myworkers : Array = profile.workers!
//this test works and returns the proper value
var testworker: NSNumber = myworkers[0].alive!
println("The satus of the test worker is " + testworker.description)
/* This code is giving error "Could not find member alive" it does not ifor var
for ifor in myworkers{
var thisworker: NSNumber = myworkers[ifor].alive! as NSNumber
}
*/
return 42
}
Your variable ifor is not a counter, it is an actual object. You could do something like this:
for worker in myWorkers {
let workerIsAlive = worker.alive!
}
Alternatively, if you need the index,
for i in 0 ..< myWorkers.count {
let worker = myWorkers[i]
let workerIsAlive = worker.alive!
}
If you need both:
for (i, worker) in enumerate(myWorkers) {
let workerIsAlive = worker.alive!
}
And as a matter of style, I would stay away from NSNumber and use Int or Bool or whatever the data actually is. Also, it looks like the alive variable should not be optional, as you're unwrapping it everywhere. To avoid "mysterious" crashes later, you may want to think about making it a non-optional type.
when using a for in loop, your loop variable isn't an index, its the objects you're looping through. so..
func aliveWorkers() {
var myworkers = [1, 2, 3]
//this test works and returns the proper value
let testworker = myworkers[0]
print("The satus of the test worker is \(testworker)")
for ifor in myworkers {
print(ifor)
}
}
Notice a few things... you don't need to use + to concatenate those strings. you can just use string interpolation. \(variable) inserts the value of variable in the string.
Try to use let instead of var when you don't change the variable. You don't need to explicitly define type on variables either.
Alright, so I got a private ?Vector $lines which is empty when constructing the object and now I want to add strings to that Vector. The following Hack code works well:
<?hh
class LineList {
private ?Vector<string> $lines;
public function addLine(string $line): void {
$this->file[] = trim($line);
}
}
But when checking the code with hh_client, it gives me the following warning:
$this->file[]]: a nullable type does not allow array append (Typing[4006])
[private ?Vector<string> $lines]: You might want to check this out
Question: How do I add elements to the Vector without that the checker pushs this warning?
The easiest way would be to not use a nullable Vector. private Vector<string> $lines = Vector {}; gets around the need for a constructor too.
Otherwise, you'll need to check if the value isn't null, then append to it:
public function addLine(string $line): void {
$vec = $this->lines;
if ($vec !== null) $vec[] = trim($line);
}
You can't just check if $this->lines !== null as it is possible for it to change value between checking and appending (with something like a tick function), hence why it is assigned to a local variable instead.
I am learning to build a surface using JMonkey api. The class Surface has a method
createNurbsSurface(controlPoints, nurbKnots, uSegments, vSegments, basisUFunctionDegree, basisVFunctionDegree).
I am trying to make a simple example to understand the meaning of the arguments. However, I can't initialize the second argument:
List<Float>[] nurbKnots
I tried:
List<Float>[] nurbKnots = {new ArrayList<Float>()};
but it complains that you cannot create a generic array of List<Float>.
Could someone show me how to initialize this nurbKnots.
It works for non-generic List:
List[] listNonGeneric = new ArrayList[10];
But this won't work:
List<Float>[] listGeneric = new ArrayList<Float>[10];
You have to use:
List<List<Float>> nurbKnots = new ArrayList<>();
and pass the argument as
(List<Float>[])nurbKnots.toArray();
One friend helped. He told me:
In Java, it's not really possible to have arrays of generic types (safely). You have to allow unchecked assignment. Something like:
#SuppressWarnings("unchecked")
List<Float>[] f = new List[2];
f[0] = new ArrayList<Float>();
f[0].add(0.1);
f[1] = new ArrayList<Float>();
f[1].add(0.2);
But, it worked!