"IllegalArgumentException: Argument is not an array" error in processing when using append(); - class

My code is as so...
ArrayList<Ray> rays = new ArrayList<Ray>();
Particle() {
for(int a=0; a < 360; a+=10) {
append(rays, new Ray(position, radians(a)));
}
}
I'm initializing an ArrayList of the class Ray. Then I run through a for loop and am attempting to
append a new Ray() to the list. I get no errors in the editor but whenever I run the code I get the error message: IllegalArgumentException: Argument is not an array
I've looked around and nothing seems to answer my question. Why is this happening?

The append function is for use with arrays (e.g.: rays[]). However rays is an ArrayList. Hence, you need to use the add method:
append(rays, new Ray(position, radians(a)));
rays.add(new Ray(position, radians(a));

Related

Call a PostgreSQL function and get result back with no loop

I have a simple rust program that interacts with a PostgreSQL database.
The actual code is:
for row in &db_client.query("select magic_value from priv.magic_value();", &[]).unwrap()
{
magic_value = row.get("magic_value");
println!("Magic value is = {}", magic_value);
}
And.. it works. But I don't like it: I know this function will return one and only one value.
From the example I found, for example here: https://docs.rs/postgres/latest/postgres/index.html
and here: https://tms-dev-blog.com/postgresql-database-with-rust-how-to/
You always have a recordset to loop on.
Which is the clean way to call a function without looping?
query returns a Result<Vec<Row>, _>. You are already unwrapping the Vec, so you can just use it directly instead of looping. By turning the Vec into an owning iterator yourself, you can even easily obtain a Row instead of a &Row.
magic_value = db_client.query("select magic_value from priv.magic_value();", &[])
.unwrap() // -> Vec<Row>
.into_iter() // -> impl Iterator<Item=Row>
.next() // -> Option<Row>
.unwrap() // -> Row
.get("magic_value");

Protractor- ElementFinder returning unexpected values

I am writing a protractor test case to compare the name(s) of the displayed data is same as the searched name.
Even though my test case works fine, I am not able to understand what is happening. Because when i expect the name to compare, it compares as expected, but when i print the elementFinder's(rowData)(i have attached the output screen shot here) value in console.log, it show a huge list of some values which i am not able to understand?
PS: I am a newbie to protractor`
This is the testCase:
it('should show proper data for given last name', function () {
var searchValue='manning';
searchBox.sendKeys(searchValue);
search.click();
element.all(by.binding('row.loanNumber')).count().then(function(value) {
var loanCount = value;
for (var i = 0; i < loanCount; i++) {
var rowData = element.all(by.binding('row.borrowerName')).get(i).getText();
expect(rowData).toMatch(searchValue.toUpperCase());
console.log(rowData,'*****',searchValue.toUpperCase());
}
});
});`
And give me valuable suggestions about my style of code
rowData is a promise (not a value), so when you console.log it, you get the promise object
Protractor patches Jasmine to automatically resolve promises within the expect(), so that's how it knows to resolve the value and compare to the expected result.
If you want to console.log the value, you need to resolve the promise with .then() to get the value.
rowData.then(function(rowDataText) {
console.log(rowDataText);
)}
This is pretty much everyone's first question when they start using protractor. You will want to learn how promises work if you want a good understanding of how to manipulate them.

Useless use of private variable in void context in simple perl loop, no idea where the error is

if ( $num_of_things > 1) {
my $max_element = $num_of_things -1;
for($max_element; $max_element >= 0; $max_element--) {
$value_array[$max_element] = $starting_hash{$key}[$max_element];
}
All of my variables not initialized in this code snippet have been initialized as part of the larger subroutine (which I don't want to put up due to length). I'm not sure where I'm getting the useless use of private variable in void context error in this code, my compiler is telling me it's the last line (with nothing but the closing brace "}"). All help is hugely appreciated as I've been staring at this loop for almost an hour with no idea what is wrong.
Move initialization (and declaration) of $max_element into for statement.
[see ooga comment ]
for( my $max_element=$num_of_things-1; $max_element>= 0; $max_element--) {
$value_array[$max_element] = $starting_hash{$key}[$max_element];
}

Using FindObjectofType and FindObjectOfTypeAll Unity methods

GameObject[] rm = FindObjectsOfTypeAll(typeof(RoadMovement)) as GameObject[];
if (Input.GetKeyDown (KeyCode.LeftArrow))
{
foreach(GameObject r in rm) //objectRefrence not set to instance of an object error here
{
var bounds1 = r.renderer.bounds;
var bounds2 = player.renderer.bounds;
Transform roadtransform = r.transform;
if(bounds1.Intersects(bounds2))
{
if (this.transform.position.x > r.renderer.bounds.min.x)
this.transform.position = new Vector3 (this.transform.position.x - 0.6f, this.transform.position.y, this.transform.position.z);
}
}
}
What is difference between FindObjectOfType and FindObjectOfTypeAll? I want to get all the objects that have RoadMovement script attached to them.I understand the error: Object reference not set to an instance but not sure how to fix this?
First problem: FindObjectsOfTypeAll has been deprecated for quite some time, and is no longer documented at all in current versions of Unity. Because you're comparing objects in the scene, it looks like you want Object.FindObjectsOfType.
Second problem: the lookup call you're making doesn't return an array of GameObjects, so the as operator will return null when the cast fails. You mentioned that RoadMovement is a behavior script you wrote. That implies that every RoadMovement has a GameObject, but not that every RoadMovement is a GameObject.
This is also why you're getting an InvalidCastException that you're asking about. Understand your return types and you will understand the error.
Recent Unity versions even added a generic version of FindObjectsOfType:
RoadMovement[] roadMovements = Object.FindObjectsOfType<RoadMovement>();
foreach (RoadMovement roadMovement in roadMovements) {
GameObject myGameObject = roadMovement.gameObject;
//do something
}

TActorIterator<T> to T

Im having problems with something,
Im not sure if its my understanding of pointers, or Unreal itself. Here is my code
TArray<Anode> nodes;
TActorIterator<Anode> ActorItr = TActorIterator< Anode >(GetWorld());
while (ActorItr) //Go through EVERY Node and check distance
{
if (this->GetUniqueID() != ActorItr->GetUniqueID())
{
//Check Distance
if (FVector::DistSquared(this->GetActorLocation(), ActorItr->GetActorLocation()) < 262144) //Check the distance between the two nodes.
{
Anode node = ActorItr;
//Anode* node = Cast<Anode>(*ActorItr);
nodes.Add(node); //Calls a error because
//cannot convert from 'TActorIterator<Anode>' to 'Anode'
}
}
}
}
So my issue is i cannot convert the Actor Iterator to my TArray, and i have played around with de-referencing etc, i got it to compile but crash at run-time. Thanks!
Edit: For clarity
i have tried
Anode node = *ActorItr;
but got a error C2440: 'initializing' : cannot convert from 'Anode *' to 'Anode'.
I then tried
Anode *node = *ActorItr;
nodes.Add(*node);
It compiled but created a run time error with unhanded memory or something, i think this is because its just handing the pointer to the TArray without handling the issue (is that right?). I understand the concept i feel, but not the syntax.
The actor iterator is not an actor, so direct assignment doesn't make any sense. Try dereferencing the iterator (Anode* node = *ActorItr;), that operator should be overloaded to return the current element of the iteration, i.e. the node you want to use.