Is it a bug or am I wrong? Syntax error on int processing - syntax

I'm kinda new to processing and I've been trying to set two variables, but while I'm setting the rest of the code, I get an error
Syntax error: error on "int"
I've tried to change the name of the variables and such but it keeps happening.
Here's the code:
int circulox = 100
int circuloy = 100
void setup() {
background(255);
size(500,500);
}

You missed the semicolons (;). The Processing syntax is based on Java. In Java (unlike in JavaScript/TypeScript) a statement must be terminated with a semicolon:
int circulox=100;
int circuloy=100;
void setup(){
background(255);
size(500,500);
}

Related

I came across a reflect invocation target exeption in processing and can't find what is causing it

I was remaking a platformer after I closed it without saving my code and came across the error. I don't know how the error occurred and am completely lost.
Here's the code below(sorry for any incomplete code):
PImage saw = loadImage("saw.png");
game run = new game();
void setup(){
size(1000, 1000);
}
void draw(){
run.saw(100, 100);
}
class game{
boolean dead;
int ballX;
int ballY;
int sawRotate;
game(){
dead = false;
ballX = 100;
ballY = 50;
sawRotate = 1;
}
void ball(){
}
void saw(int x, int y){
pushMatrix();
rotate(sawRotate);
translate(x, y);
image(saw, 100, -100, 500, 500);
popMatrix();
}
void platform(){
}
}
You can't use Processing functions until after the setup() function has been called. This line happens before that:
PImage saw = loadImage("saw.png");
You need to move the call to loadImage() to be inside the setup() function:
PImage saw;
void setup(){
size(1000, 1000);
saw = loadImage("saw.png");
}

NullPointerException in processing with 2D array of objects

I am creating a small game in processing and I am trying to print a 2D array of square objects. I have this NullPointerException and I cannot seem to find anything like it on the web.
int edge = 10;
public int sizeOfRect = 50;
public int numberOfRects = 10;
Rectangle[][] player = new Rectangle[numberOfRects][numberOfRects];
public int k;
public int l;
public int kcount=0;
public int lcount=0;
void setup(){
background(200);
size(565, 565);
}
void draw(){
for(k=edge; k<width-edge; k+=55){
for(l=edge; l<height-edge; l+=55){
player[kcount][lcount].display();
lcount++;
}
lcount=0;
kcount++;
}
kcount=0;
}
and the Rectangle Class
class Rectangle{
int i;
int j;
Rectangle(){
i=k;//xcoor
j=l;//ycoor
}
void display(){
fill(0);
rect(i,j,sizeOfRect,sizeOfRect);
}
}
And finally the exception
Plain.pde:17:0:17:0: NullPointerException Finished. Could not run the
sketch (Target VM failed to initialize). For more information, read
revisions.txt and Help? Troubleshooting. Could not run the sketch.
Thank you in advance
You're creating a 2D array, but you're never filling that array with any objects. In other words, your 2D array is full of null values. That's why you're getting a NullPointerException.
You need to fill your array with values. Here's an example:
player[1][2] = new Rectangle();
You probably want to use a nested for loop to fill your array.

Arduino 1.6.9/1.610 build fails with "'constexprint' does not name a type"

Supposedly Arduino's IDE > 1.6.2 has C++11 support.
I have just freshly downloaded and run version 1.6.9 on OSX (and as others have reported, this repros on Windows as well, with 1.6.9/1.6.10).
I cannot get this simple program to compile:
constexpr int get_five() { return 5; }
void setup() {
Serial.begin(9600);
Serial.println(get_five());
}
void loop() {
}
I receive this error when I try to build or upload:
sketch_jul25a:1: error: 'constexprint' does not name a type
constexpr int get_five() { return 5; }
^
exit status 1
'constexprint' does not name a type
I've looked at this question and answer, but it is supposedly no longer applicable in 1.6.9 version of the IDE that I am using - error: 'constexpr' does not name a type m- arduino ide
I have dug into the temporary files that are output by the IDE when building, and it seems it is trying to automatically generate headers for functions (I assume for multi-file sketch support), and does the wrong thing when it encounters constexpr:
#include <Arduino.h>
#line 1 "/Users/<my_username>/Documents/Arduino/sketch_jul25a/sketch_jul25a.ino"
#line 1 "/Users/<my_username>/Documents/Arduino/sketch_jul25a/sketch_jul25a.ino"
#line 1 "/Users/<my_username>/Documents/Arduino/sketch_jul25a/sketch_jul25a.ino"
constexprint get_five(); // **** <- This looks to be the culprit
#line 3 "/Users/<my_username>/Documents/Arduino/sketch_jul25a/sketch_jul25a.ino"
void setup();
#line 9 "/Users/<my_username>/Documents/Arduino/sketch_jul25a/sketch_jul25a.ino"
void loop();
#line 1 "/Users/<my_username>/Documents/Arduino/sketch_jul25a/sketch_jul25a.ino"
constexpr int get_five() { return 5; }
void setup() {
Serial.begin(9600);
Serial.println(get_five());
}
void loop() {
}
Is this a bug in the Arduino IDE? Is it unique to OSX? Is there a workaround that allows constexpr to work?
In googling I have found that some Arduino libraries are using constexpr, so I am assuming it could be made to work in some cases.
This is a known limitation of the arduino-builder.
Until it is fixed, you can add a prototype yourself above the function. This will prevent the IDE from incorrectly generating its own.
constexpr int get_five();
constexpr int get_five() { return 5; }
void setup() {
Serial.begin(9600);
Serial.println(get_five());
}
void loop() {
}

I am getting runtime error upon incrementing the pointer, what is the plausible reason?

I am new to C++, while I was implementing a tag class, I encountered a runtime error.
Upon debugging I found out that the runtime error was caused by incrementing pointer(**) attrib_list, but the other pointer which points to same memory address produces no error on incrementing,
Please explain to me what is the reason for this odd behaviour ?
I used hackerrank online ide to compile this code
class Tag{
public:
Tag():
tagname{""}, sz{0}, t_sz{0}, attrib_list{nullptr}, ptr_nav{nullptr}{
}
Tag(string name, int n_att):
tagname{name}, sz{0}, t_sz{n_att}, attrib_list{new string*[n_att]}{
for(int i=0; i<n_att; i++){
attrib_list[i] = new string[2];
}
ptr_nav = &attrib_list[0]; //take risk here
}
~Tag(){
for(int i=0; i< t_sz; i++){
delete[] attrib_list[i];
}
attrib_list = nullptr;
ptr_nav = nullptr;
t_sz, sz = 0;
}
// this function produces rintime error
void add_attribute(string name, string value){
(*attrib_list)[0] = name;
(*attrib_list)[1] = value;
sz++;
attrib_list++;
}
/*
This does not produce any error, why ????
void add_attribute(string name, string value){
(*ptr_nav)[0] = name;
(*ptr_nav)[1] = value;
sz++;
ptr_nav++;
}
*/
private:
string tagname;
string **attrib_list, **ptr_nav;
int sz, t_sz;
};
int main() {
Tag t("tag1", 2);
t.add_attribute("value1", "1"); //runtime error
t.add_attribute("value2", "2"); //runtime error
return 0;
}
After two calls to add_attribute, attrib_list is incremented twice and now points past the end of the original new[]-allocated array. Which is not in itself a problem.
But then your Tag instance goes out of scope, its destructor runs and tries to access attrib_list[i] on a now-invalid attrib_list pointer, which of course exhibits undefined behavior.
Unrelated to the immediate issue: t_sz, sz = 0; does not assign 0 to two variables (as you seem to believe), but only to sz. Read about comma operator in your favorite C++ reference. In any case, you don't need to do that in the first place (nor to set the two pointers to nullptr) - they are about to be destroyed anyway, their values at that point don't matter.

Load multiple images in Processing

I would like to load and draw multiple/all images from a directory in Processing.
I cant find a way to extend the one image example:
PImage a;
void setup() {
size(800,800);
background(127);
a = loadImage("a/1.jpg");
noLoop();
}
void draw(){
image(a,random(300),random(300),a.width/2, a.height/2);
}
to multiple images.
Is there a simple way to achieve this?
Thank you very much.
I'm sure there are more elegant ways to do it, but wouldn't something as simple as this work?
PImage a;
Pimage b;
void setup() {
size(800,800);
background(127);
a = loadImage("a/1.jpg");
b = loadImage("b/1.jpg");
noLoop();
}
void draw(){
image(a,random(300),random(300),a.width/2, a.height/2);
image(b,random(300),random(300),b.width/2, b.height/2);
}
You can find an example of listing a directories here: http://processing.org/learning/topics/directorylist.html. The reference section for loops is here: http://processing.org/reference/loop_.html.
Imagine u have a known number of images (n) called 0.jpg, 1.jpg, 2.jpg..., then u can do sth like this:
PImage[] fragment;
int n=3;
void setup() {
size(400, 400);
fragment=new PImage[n];
for(int i=0;i<fragment.length;i++){
fragment[i]=loadImage(str(i) + ".jpg");
}
}
void draw(){
for(int i=0;i<fragment.length;i++){
image(fragment[i],20*i,20*i);
}
}

Resources