Exception while reading file

Hi,

While reading a file using the code shown below, I’m getting an exception (shown in the screenshot) that I am not able to catch. Can you please suggest what should I do?

File fileToRead = File::getCurrentWorkingDirectory().getChildFile("file.txt");
    if (!fileToRead.existsAsFile())
    {
        DBG("File doesn't exist ...");
    }
    
    std::unique_ptr<juce::FileInputStream> input{ fileToRead.createInputStream() };
    if (!input->openedOk())
    {
        DBG("Failed to open file");
        // ... Error handling here
    }

    bool readWholeFile = true;
    if (readWholeFile)
    {   
        try {
            String content = input->readString();
            // ... Do your stuff. New lines are normally seperated by "\n"
            DBG(content);
        }
        catch (const std::exception& e) {
        }
    }

Your program is crashing because it is dereferencing a null pointer. Perhaps fileToRead.createInputStream() is failing and returning a null stream. You should check input != nullptr before trying to call input->openedOk().

Thank you @reuk , it worked :tada: