Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unhandled exception: nlohmann::detail::parse_error #3078

Closed
2 of 5 tasks
ghost opened this issue Oct 13, 2021 · 14 comments
Closed
2 of 5 tasks

Unhandled exception: nlohmann::detail::parse_error #3078

ghost opened this issue Oct 13, 2021 · 14 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@ghost
Copy link

ghost commented Oct 13, 2021

What is the issue you have?

I have an Unhandled exception error:
Exception thrown at 0x76BBB5B2 in guessTheNumber.exe: Microsoft C++ exception: nlohmann::detail::parse_error at memory location 0x012FE18C.
Unhandled exception at 0x76BBB5B2 in guessTheNumber.exe: Microsoft C++ exception: nlohmann::detail::parse_error at memory location 0x012FE18C.
And Visual Studio 2022 says the error is on line 6176
image

Please describe the steps to reproduce the issue.

  1. Download, fork or clone my code
  2. Compile and run the project using Visual Studio Community 2019 or 2022 (They both give error)
  3. See the error for yourself

Can you provide a small but working code example?

My code is hosted on github here

What is the expected behavior?

Uhh, for the code to run and not give an error? I don't really understand the json.hpp code yet

And what is the actual behavior instead?

Two errors:
Exception thrown at 0x76BBB5B2 in guessTheNumber.exe: Microsoft C++ exception: nlohmann::detail::parse_error at memory location 0x012FE18C.
Unhandled exception at 0x76BBB5B2 in guessTheNumber.exe: Microsoft C++ exception: nlohmann::detail::parse_error at memory location 0x012FE18C.

Which compiler and operating system are you using?

  • Operating System: Windows 10 (Yeah, I wasn't one of the first to get allowed 11)
  • Code Editor: Visual Studio 2022 (Bug is also in Visual Studio 2019)

Which version of the library did you use?

  • latest release version 3.10.3
  • other release - please state the version: ___
  • the develop branch

If you experience a compilation error: can you compile and run the unit tests?

  • yes
  • no
    I don't know how to turn that on in Visual Studio 2022, I don't use cmake to compile my code.
@ghost ghost added the kind: bug label Oct 13, 2021
@nlohmann
Copy link
Owner

nlohmann commented Oct 13, 2021

I can't find an instance of json::parse in your code. See https://json.nlohmann.me/api/basic_json/parse_error/ and https://json.nlohmann.me/home/exceptions/#parse-errors for information on the exception. Can you please catch the exception and output the result of what() - it should contain detailed information on the source location that triggered the parse error.

(Removing the bug label as this is very likely just an error in the application to properly open a file).

@nlohmann nlohmann added state: needs more info the author of the issue needs to provide more details and removed kind: bug labels Oct 13, 2021
@gregmarr
Copy link
Contributor

@ghost
Copy link
Author

ghost commented Oct 13, 2021

I can't find an instance of json::parse in your code

I am parsing/reading the Highscore.json right? In file.cpp?

Can you please catch the exception and output the result of what() - it should contain detailed information on the source location that triggered the parse error.

image
The first time I ran the code with my try, catch blocks my computer crashed... But this is the results of the second run.

Does this help? I'm new at C++ so I may have just broken stuff with my try catch blocks.

@gregmarr
Copy link
Contributor

This means that your program is failing to open the file.

@ghost
Copy link
Author

ghost commented Oct 13, 2021

This means that your program is failing to open the file.

Why would that be happening? I see no bugs in my code where the file wouldn't open...

Do any of you use Visual Studio Comminuty on Windows? If so it may be easier to find the bug by compiling, debuging and editing the code with it like I am.

@nlohmann
Copy link
Owner

There are plenty of exceptions in your screenshot, but the first one indeed points out that either the file you want to parse is empty or could not be opened.

Best would be to run your program in the debugger to see stack traces in case of an uncaught exception.

@nlohmann nlohmann removed the state: needs more info the author of the issue needs to provide more details label Oct 13, 2021
@gregmarr
Copy link
Contributor

Why would that be happening? I see no bugs in my code where the file wouldn't open...

Most likely the file is not where you think it is relative to the working directory. Try using a full path instead of a relative path.

Another possibility is that you are using Unix path separators on Windows. Try using \\ instead of / in your path.

You can verify that you are opening the proper file by reading from it and then printing to the console. Note that you can't then use the same stream in the >> call, so just do that long enough to resolve your file issue, then return to using >> to read the json .

@ghost
Copy link
Author

ghost commented Oct 14, 2021

Most likely the file is not where you think it is relative to the working directory. Try using a full path instead of a relative path.

Yeah, the file path was different from what I put but that still didn't fix the problem, and I WAS using Unix path separators! I had no idea that Windows used \\, but I am compiling as Win32 so there shoudn't be any problems using Unix path separators. But thanks for that info! Very useful!

Here is the output of the call stack: (Not very useful in my opinion)
image

Now when I compile my code, it gives this exception: (How did it trigger a breakpoint? I didn't add any breakpoints)
image

And this is my console: (Still the same sad errors)
image

You can verify that you are opening the proper file by reading from it and then printing to the console.

After I ran file.open() in main I put std::cout << file.highscore; but that code never ran, so the problem is in the file.open() function.

Note that you can't then use the same stream in the >> call, so just do that long enough to resolve your file issue, then return to using >> to read the json .

I'm sorry but I don't understand what you mean by this. Also, I just pushed my code to my repository now, so you can see the changes I made.

@ghost
Copy link
Author

ghost commented Oct 14, 2021

I found more info about the error, I made some simple code to test the error location. Here is part of my test code:

std::cout << "\nDebug Point 0\n";
std::ifstream FileRead("test\\test.json"); // This is the correct location of json file
std::cout << "\nDebug Point 1\n";
nlohmann::json TheFile;
std::cout << "\nDebug Point 2\n";
FileRead >> TheFile;
std::cout << "\nDebug Point 3\n";
std::cout << TheFile;
std::cout << "\nDebug Point 4\n";
FileRead.close();

std::cout << TheFile; // Read the json file

When I run this the terminal displays:
image

So I decided to put some things in their own try catch, here is the code:

std::cout << "\nDebug Point 0\n";
    std::ifstream FileRead("test\\test.json"); // This is the correct location of json file
    std::cout << "\nDebug Point 1\n";
    nlohmann::json TheFile;
    std::cout << "\nDebug Point 2\n";
    try
    {
        FileRead >> TheFile;
    }
    catch (const std::exception& exc)
    {
        std::cout << "Error from \'FileRead >> TheFile;\': ";
        std::cout << exc.what() << '\n';
    }
    std::cout << "\nDebug Point 3\n";
    try
    {
        std::cout << TheFile;
    }
    catch (const std::exception& exc2)
    {
        std::cout << "Error from \'std::cout << TheFile;\': ";
        std::cout << exc2.what() << '\n';
    }
    std::cout << "\nDebug Point 4\n";
    try
    {
        FileRead.close();
    }
    catch (const std::exception& exc3)
    {
        std::cout << "Error from p\'FileRead.close();\': ";
        std::cout << exc3.what() << '\n';
    }
    try
    {
        std::cout << TheFile; // Read the json file
    }
    catch (const std::exception& exc4)
    {
        std::cout << "Error from \'std::cout << TheFile;\': ";
        std::cout << exc4.what() << '\n';
    }
    return 0;

And here is the result:
image

But I KNOW I have the right file location:
image image

@nlohmann
Copy link
Owner

std::ifstream FileRead("test\\test.json");

This is a relative path to file test.json. You must make sure that this path is valid from the location where you start your binary. It seems your IDE starts the binary guessTheNumber.exe from the Debug directory. Hence, you need to make sure file Debug\test\test.json exists.

@ghost
Copy link
Author

ghost commented Oct 14, 2021

Hence, you need to make sure file Debug\test\test.json exists.

It wasn't there, but even when I added it I still got a problem, I am also asking on Microsoft's Q&A center about the problem with Visual Studio Community.

I'll let you know if I find anything or if the problem is solved.

@gregmarr
Copy link
Contributor

In the code below, replace nlohmann::json TheFile; with std::string TheFile; and then adjust the passed file path and actual file location until you get the content of the file displayed properly in between point 3 and point 4. Then once you know you are reading the file properly, you can put back nlohmann::json TheFile;.

nlohmann::json TheFile;
std::cout << "\nDebug Point 2\n";
try
{
    FileRead >> TheFile;
}
catch (const std::exception& exc)
{
    std::cout << "Error from \'FileRead >> TheFile;\': ";
    std::cout << exc.what() << '\n';
}
std::cout << "\nDebug Point 3\n";
try
{
    std::cout << TheFile;
}
catch (const std::exception& exc2)
{
    std::cout << "Error from \'std::cout << TheFile;\': ";
    std::cout << exc2.what() << '\n';
}

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Oct 14, 2021
@ghost
Copy link
Author

ghost commented Oct 15, 2021

Now I have the actual path of the file, and now I know the problem is with the file path!? I still can't find the relative path, but at least I have the actual path and it works. Any tips about were the relative path is?

Edit: Is there some way to output the current directory in C++? Maybe then I can find where I should put the json file as a relative path.

@ghost
Copy link
Author

ghost commented Oct 16, 2021

Alright, I found out my problem, at first I thought there was a problem with the json.hpp because the debugger said there was a problem somewhere there. But with the try catch blocks put in, json.hpp said that there was a parse error even though I THOUGHT I had the right relative file path.
At the root of my project I have the Debug/ folder with the .exe, and at the root I also have a folder called data/ that has the file I want fstream and json.hpp to read. I thought the relative path to the json was to go ../data/ (get out of Debug/ and into data/ to my json file) but that was not the correct solution, what is the correct solution was to just go into data/ because the exe's relative path starts at the root of the project where the data/ and src/ and Debug/ etc.

I hope I make sense, and thank you for your help!

@ghost ghost closed this as completed Oct 16, 2021
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

2 participants