SimpleJSON returns null - simplejson

i have an error in c# of NullReferenceException: Object reference not set to an instance of an object
SimpleJSON.JSONNode.Parse (System.String aJSON) (at Assets/Scenes/scripts/SimpleJSON.cs:553)
I found this post in the link below but i didn't understand the solution
SimpleJSON returns null when handling a call from Postmen
Can any one explain it for me, please?
the error is :
NullReferenceException: Object reference not set to an instance of an object
SimpleJSON.JSONNode.Parse (System.String aJSON) (at Assets/Scenes/scripts/SimpleJSON.cs:553)
SimpleJSON.JSON.Parse (System.String aJSON) (at Assets/Scenes/scripts/SimpleJSON.cs:1363)
Control.Update () (at Assets/Control.cs:123)
knowing that i'm sending json data using websocket and i'm receiving it in the console unity. i use simpleJSON script to deserialize the data.
here where the error is provided in line JSONNode root = JSON.Parse(last_message); :
private void Update()
{
if (ws == null)
{
return;
}
List<GameObject> object_to_keep = new List<GameObject>();
JSONNode root = JSON.Parse(last_message);
JSONNode Face = root["streams"][0]["objects"][0];
{
int id = Face["mono"]["id"].AsInt;
JSONNode position = Face["mono"]["position"];
Vector3 pos = new Vector3(position["x"].AsFloat, position["y"].AsFloat, position["z"].AsFloat);
GameObject mono = GetMonoObject();
WintualFaceController win_controller = mono.GetComponent<WintualFaceController>();
win_controller.face_id = id;
if(mono)
{
mono.transform.localPosition = pos;
object_to_keep.Add(mono);
}
else
print("Mono not found");
}
}
The error in SImpleJSON script wan in line: while (i < aJSON.Length)
public static JSONNode Parse(string aJSON)
{
Stack<JSONNode> stack = new Stack<JSONNode>();
JSONNode ctx = null;
int i = 0;
StringBuilder Token = new StringBuilder();
string TokenName = "";
bool QuoteMode = false;
bool TokenIsQuoted = false;
while (i < aJSON.Length)
{
switch (aJSON[i])
{
case '{':
if (QuoteMode)
{
Token.Append(aJSON[i]);
break;
}
stack.Push(new JSONObject());
if (ctx != null)
{
ctx.Add(TokenName, stack.Peek());
}
TokenName = "";
Token.Length = 0;
ctx = stack.Peek();
break;
case '[':
if (QuoteMode)
{
Token.Append(aJSON[i]);
break;
}
stack.Push(new JSONArray());
if (ctx != null)
{
ctx.Add(TokenName, stack.Peek());
}
TokenName = "";
Token.Length = 0;
ctx = stack.Peek();
break;
case '}':
case ']':
if (QuoteMode)
{
Token.Append(aJSON[i]);
break;
}
if (stack.Count == 0)
throw new Exception("JSON Parse: Too many closing brackets");
stack.Pop();
if (Token.Length > 0 || TokenIsQuoted)
{
ParseElement(ctx, Token.ToString(), TokenName, TokenIsQuoted);
TokenIsQuoted = false;
}
TokenName = "";
Token.Length = 0;
if (stack.Count > 0)
ctx = stack.Peek();
break;
case ':':
if (QuoteMode)
{
Token.Append(aJSON[i]);
break;
}
TokenName = Token.ToString();
Token.Length = 0;
TokenIsQuoted = false;
break;
case '"':
QuoteMode ^= true;
TokenIsQuoted |= QuoteMode;
break;
case ',':
if (QuoteMode)
{
Token.Append(aJSON[i]);
break;
}
if (Token.Length > 0 || TokenIsQuoted)
{
ParseElement(ctx, Token.ToString(), TokenName, TokenIsQuoted);
TokenIsQuoted = false;
}
TokenName = "";
Token.Length = 0;
TokenIsQuoted = false;
break;
case '\r':
case '\n':
break;
case ' ':
case '\t':
if (QuoteMode)
Token.Append(aJSON[i]);
break;
case '\\':
++i;
if (QuoteMode)
{
char C = aJSON[i];
switch (C)
{
case 't':
Token.Append('\t');
break;
case 'r':
Token.Append('\r');
break;
case 'n':
Token.Append('\n');
break;
case 'b':
Token.Append('\b');
break;
case 'f':
Token.Append('\f');
break;
case 'u':
{
string s = aJSON.Substring(i + 1, 4);
Token.Append((char)int.Parse(
s,
System.Globalization.NumberStyles.AllowHexSpecifier));
i += 4;
break;
}
default:
Token.Append(C);
break;
}
}
break;
default:
Token.Append(aJSON[i]);
break;
}
++i;
}
this is my json:
{
"cmd": "data",
"frame_time": "2021-06-30T09:04:16.464836+00:00",
"start_time": "2021-06-30T07:51:33.452079+00:00",
"streams": [
{
"name": "usb-0001:012.0-3",
"objects": [
{
"name": "face",
"mono": {
"id": "190",
"position": {
"x": "-0.012259",
"y": "0.059731",
"z": "0.707695"
}
},
"multi": [
{
"id": "190",
"position": {
"x": "-0.012263",
"y": "0.059753",
"z": "0.707151"
}
}
]
}
]
}
]
}

Related

Declaring a variable gives an error, makes me think there's something else I'm missing

I have a function in C that compiles fine until I add another case to my switch statement and declare a variable.
Here's the function
void evaluate(int num_inputs) {
struct gate *ptr = gatehead;
while (ptr->next != NULL) {
switch (ptr->kind) {
case 0:
if (get_input_value(ptr->params[0]) && get_input_value(ptr->params[1])) {
write_to_output(true, ptr->output);
} else {
write_to_output(false, ptr->output);
}
break;
case 1:
if (get_input_value(ptr->params[0]) || get_input_value(ptr->params[1])) {
write_to_output(true, ptr->output);
} else {
write_to_output(false, ptr->output);
}
break;
case 2:
if (!(get_input_value(ptr->params[0]) && get_input_value(ptr->params[1]))) {
write_to_output(true, ptr->output);
} else {
write_to_output(false, ptr->output);
}
break;
case 3:
if (!(get_input_value(ptr->params[0])) && !(get_input_value(ptr->params[1]))) {
write_to_output(true, ptr->output);
} else {
write_to_output(false, ptr->output);
}
break;
case 4:
if ((get_input_value(ptr->params[0]) || get_input_value(ptr->params[1])) && !(get_input_value(ptr->params[0]) && get_input_value(ptr->params[1]))) {
write_to_output(true, ptr->output);
} else {
write_to_output(false, ptr->output);
}
break;
case 5:
if (get_input_value(ptr->params[0])) {
write_to_output(false, ptr->output);
} else {
write_to_output(true, ptr->output);
}
break;
case 6:
if (get_input_value(ptr->params[0])) {
write_to_output(true, ptr->output);
} else {
write_to_output(false, ptr->output);
}
break;
case 7:
char* d = "DECODER";
printf("%s", d);
break;
}
ptr = ptr->next;
}
}
If I comment out case 7, everything works fine. Even keeping the print and break compiles, but declaring the char* causes the error "expected expression"
The problem is that the case 7: label is immediately followed by a variable declaration.
case 7:
char* d = "DECODER";
A label can only be applied to a statement, and a declaration is not considered a statement. You can get around this by adding an empty statement before the case label:
case 7:
;
char* d = "DECODER";

Escape delay with a button in a game

I have a problem with a piece of code for a game. I would like to know, how to break the time delay if I press the button.
List of files
At the end, the game writes your end score on the LCD for 10 seconds. And goes back to the wait for start. I want to keep that as it is - the only thing that I want to include is that if I press the button within those 10 seconds I want it to go immediately back to the wait for start.
Function code is here
void DrawGameScore()
{ //&RFONT_8X12 &RFONT_16X26
//char key;
//key = KBD_GetKey();
char txt[10];
UG_FontSelect(&RFONT_16X26);
UG_SetForecolor(C_WHITE);
switch (level){
case 1:
UG_PutString(60,130,"Peasant Score:");
sprintf(txt,"%d",score);
UG_PutString(120,160,txt);
_delay_ms(10000);
break;
case 2:
UG_PutString(60,130,"Knight Score:");
sprintf(txt,"%d",score);
UG_PutString(120,160,txt);
_delay_ms(10000);
break;
case 3:
UG_PutString(60,130,"Queen Score:");
sprintf(txt,"%d",score);
UG_PutString(120,160,txt);
_delay_ms(10000);
break;
case 4:
UG_PutString(60,130,"King Score:");
sprintf(txt,"%d",score);
UG_PutString(120,160,txt);
_delay_ms(10000);
break;
case 5:
UG_PutString(60,130,"God Score:");
sprintf(txt,"%d",score);
UG_PutString(120,160,txt);
_delay_ms(10000);
break;
}
//UG_PutString(60,130,"Final Score:");
//sprintf(txt,"%d",score);
//UG_PutString(120,160,txt);
//_delay_ms(1000);
}
When the game is over this function draws your score based on your level. In this next function i am calling drawgamescore function, which is in dsip_end_score makro.
int EndOfGame()
{
char key;
//static int state;
int result = 1;
key = KBD_GetKey();
display_msg = DSIP_END_SCORE;
if (key == BTN_OK){
//display_msg = DISP_REFRESH_ALL;
//display_msg = DSIP_END_SCORE;
return result;
}
return result;
}
Here is the main game function
void Game()
{
static int state;
int result;
switch (state)
{
case 1: //waiting for the game start
result = WaitForStart();
if (result == 1) state++;
break;
case 2: //Play the game
result = PlayTheGame();
if (result == 1) state++;
break;
case 3: //Display the score
result = EndOfGame();
if (result == 1) state=0;
break;
default: //0 or unknown value: reset the game
state = 1;
break;
}
}
Thanks.
I tried
int EndOfGame()
{
char key;
static int state;
int result = 1;
key = KBD_GetKey();
display_msg = DSIP_END_SCORE;
if (key == BTN_OK){
//display_msg = DISP_REFRESH_ALL;
//display_msg = DSIP_END_SCORE;
return result;
}
return result;
}
/*
display_msg = DSIP_END_SCORE;
if (key != 0){
switch (key)
{
case BTN_OK:
return result;
break;
}
}
*/
/*
switch (state)
{
case 0:
display_msg = DSIP_END_SCORE;
state++;
break;
case 1:
key = KBD_GetKey();
if (key != 0){
switch (key)
{
case BTN_OK:
return result;
break;
}
break;
}
}
return result;
*/
//display_msg = DISP_REFRESH_ALL;
//display_msg = DSIP_END_SCORE;
//return result;
/*
char key;
int result=0;
//static int state;
key = KBD_GetKey();
if (KBD_isKeyStatePressed(BTN_OK))
{
display_msg = DSIP_END_SCORE;
result = 1;
} else {
result = 1;
}
//return result;
*/
/*
switch (state){
case 0:
KBD_flush(); //empty buffer
//display_msg = DISP_REFRESH_ALL;
display_msg = DSIP_END_SCORE;
return result;
state++;
break;
case 1:
key = KBD_GetKey(); //kbd read dela background services
if (key == BTN_OK){
//display_msg = DISP_REFRESH_ALL;
display_msg = DSIP_END_SCORE;
return result;
break;
}
break;
}
return result;
*/
/*
key = KBD_GetKey();
if (key == BTN_OK) {
//display_msg = DISP_REFRESH_ALL;
return result;
}else{
display_msg = DISP_REFRESH_ALL;
display_msg = DSIP_END_SCORE;
//TODO: write the program
return result;
}*/
I found a solution!
Here is the code if anyone is interested.
int EndOfGame()
{
int result = 0;
char key;
static uint32_t timeStamp;
static int state = 0;
display_msg = DISP_REFRESH_ALL;
switch (state)
{
case 0: //Show end score
display_msg = DSIP_END_SCORE;
timeStamp = GetSysTick();
state++;
break;
case 1: //read keys after 2 seconds
if (Has_X_MillisecondsPassed(2000, &timeStamp)
{
state++;
KBD_flush();
break;
}
break;
case 2:
key = KBD_GetKey();
if (key != 0)
{
switch (key)
{
case BTN_OK:
state = 0;
result = 1;
break;
}
}
//finish after 10 seconds
if (Has_X_MillisecondsPassed(10000, &timeStamp))
{
result = 1;
state = 0;
break;
}
break;
}
return result;
}
If you want to know about the
Has_X_millisecondsPassed();
function you are welcome to see it in systime.c in the File list hyperlink in the question above. For the reference i'm using AtMega328pb microcontroller.

The input string is not in the correct format

private void btnComprobar_Click(object sender, RoutedEventArgs e)
{
Inventario inv = new Inventario();
inv.beneficio = txtBeneficio.Text;
inv.idProducto = txtIdProducto.Text;
inv.idProveedor = txtIdProveedor.Text;
inv.precioEntrada = txtprecioEntrada.Text;
//errores en la conversion de precioSalida y cantidad
inv.precioSalida = double.Parse(txtPrecioSalida.Text);
inv.cantidad = int.Parse(txtCantidad.Text);
inv.clase = txtClase.Text;
switch (txtClase.Text)
{
case "1":
inv.clase = "FUTBOL";
break;
case "2":
inv.clase = "RUNING";
break;
case "3":
inv.clase = "BALONMANO";
break;
default:
inv.clase = "1";
break;
}
inv.descripcion = txtDescripcion.Text;
inv.estado = txtEstado.Text;
databaseConector.instance.comprobarProducto(dtGConsultas, inv);
}
error message:
The input string is not in the correct format.
In these two cases
Inv.precioSalida = double.Parse (txtPrecioSalida.Text);
The call to double.Parse will throw an exception if the string argument cannot be parsed to a double value. Same thing with int.Parse.
You could use the double.TryParse/int.TryParse method to try to parse the values:
private void btnComprobar_Click(object sender, RoutedEventArgs e)
{
Inventario inv = new Inventario();
inv.beneficio = txtBeneficio.Text;
inv.idProducto = txtIdProducto.Text;
inv.idProveedor = txtIdProveedor.Text;
inv.precioEntrada = txtprecioEntrada.Text;
//errores en la conversion de precioSalida y cantidad
double precioSalida;
if(double.TryParse(txtPrecioSalida.Text, out precioSalida))
inv.precioSalida = precioSalida
int cantidad;
if(int.TryParse(txtCantidad.Text, out cantidad))
inv.cantidad = cantidad;
inv.clase = txtClase.Text;
switch (txtClase.Text)
{
case "1":
inv.clase = "FUTBOL";
break;
case "2":
inv.clase = "RUNING";
break;
case "3":
inv.clase = "BALONMANO";
break;
default:
inv.clase = "1";
break;
}
inv.descripcion = txtDescripcion.Text;
inv.estado = txtEstado.Text;
databaseConector.instance.comprobarProducto(dtGConsultas, inv);
}

Comments(/*...*/) longer than one line

I am supposed to count the /*...*/ comments in a file and I was wondering if this code is right or am I missing something out?
My Code
void commentsLongerThanOneLine(FILE* inputStream, FILE* outputStream) {
int count = 0, c, state = 1;
while ((c = fgetc(inputStream) != EOF)) {
switch (state) {
case 1: switch (c) {
case '/': state = 2; break;
}
break;
case 2: switch (c) {
case '/': state = 3; break;
case '*': state = 4; break;
default: state = 1;
}
break;
case 3: switch (c) {
case '\n': state = 1; break;
default: state = 4; count++;
}
break;
case 4:switch (c) {
case'*': if (c == '\n') count++; break;
default: state = 4; if (c == '\n') count++;
}
}
}
fprintf(outputStream, "Comments longer than one line: %d\n", count);
}

C SDL Keyboard Events SDL_KEYUP Triggering When Key Is Down

I am using SDL in C, and I'm trying to make a character move on the screen when a key is pressed, and stop when it is released, but it seems as if the KEYUP event is triggering when the key is still being held down. If I remove the KEYUP section in pressed(), the characters will slide across the screen, but obviously won't stop on their own. If I leave the KEYUP section in, I have to press the key repeatedly to move them across the screen.
Any idea what I'm doing wrong?
Here's what I have:
...
int done = 0;
while (done==0)
{
pressed();
if (kenter == 1)
{
state = 1;
subscreen();
}
if (kescape == 1)
{
done = 1;
}
if (kup == 1)
{
playery += -2;
}
if (kdown == 1)
{
playery += 2;
}
if (kright == 1)
{
playerx += 2;
}
if (kleft == 1)
{
playerx += - 2;
}
...
int pressed ()
{
SDL_Event keyevent;
while (SDL_PollEvent(&keyevent))
{
switch(keyevent.type)
{
case SDL_KEYDOWN:
switch(keyevent.key.keysym.sym)
{
case SDLK_RETURN:
{
kenter = 1;
break;
}
case SDLK_ESCAPE:
{
kescape = 1;
break;
}
case SDLK_a:
{
ka = 1;
break;
}
case SDLK_s:
{
ks = 1;
break;
}
case SDLK_UP:
{
kup = 1;
break;
}
case SDLK_DOWN:
{
kdown = 1;
break;
}
case SDLK_RIGHT:
{
kright = 1;
break;
}
case SDLK_LEFT:
{
kleft = 1;
break;
}
default:
break;
}
}
switch(keyevent.type)
{
case SDL_KEYUP:
switch(keyevent.key.keysym.sym)
{
case SDLK_RETURN:
{
kenter = 0;
break;
}
case SDLK_ESCAPE:
{
kescape = 0;
break;
}
case SDLK_a:
{
ka = 0;
break;
}
case SDLK_s:
{
ks = 0;
break;
}
case SDLK_UP:
{
kup = 0;
break;
}
case SDLK_DOWN:
{
kdown = 0;
break;
}
case SDLK_RIGHT:
{
kright = 0;
break;
}
case SDLK_LEFT:
{
kleft = 0;
break;
}
default:
break;
}
}
}
return 0;
}
I think your switch statements are broken.
Use this less confusing way
int pressed ()
{
SDL_Event event;
while(SDL_PollEvent(&event) )
{
if(event.type == SDLK_KEYDOWN)
{
switch(event.key.keysym.sym)
{
case SDLK_RETURN:
doStuff = 1
break;
default:
break;
}
}
if(event.type == SDLK_KEYUP)
{
switch(event.key.keysym.sym)
{
case SDLK_RETURN:
doStuff = 0;
break;
default:
break;
}
}
}
}
Also important:
SDL Tutorials: Practical Keyboard Input
Oh and avoid using global variables!
Your use of two switch statements is odd and confusing, you should probably fix that.
The issues you're seeing are likely due to keyboard repeat, see the SDL_EnableKeyRepeat() call for how to disable it.
don't use switch. I experienced same thing, but I resolved into using 'if' like this. Maybe switch has 'else if'.
if(event.type == SDL_KEYDOWN){
....
}
if(event.type == SDL_KEYUP){
....
}

Resources