How to add widget in Unreal Engine?

You always get class of a asset not the class it generates, so oyu actully getting UWidgetBlueprint

You need to get generated class from it:

But in general you should avoid hard coding assets bindings, best practice is to use UPROPERTY with UClass* or TSubclassOf that you can set in blueprint to point to specific widget in blueprint defaults, in this case you want user (of editor) to pick UUserWidget class

How can I add widget to the screen in Unreal Engine? By some reason variable ‘blackLinesWidgetClass’ is always null.

FStringClassReference blackLinesWidgeClasstRef(TEXT("WidgetBlueprint'/Game/Blueprints/UI/blackLines.blackLines'"));
UClass* blackLinesWidgetClass = blackLinesWidgeClasstRef.TryLoadClass<UUserWidget>();
if (blackLinesWidgetClass) 
{ 
    UUserWidget* blackLinesWidget = CreateWidget<UUserWidget>(this->GetGameInstance(), blackLinesWidgetClass);
    if (blackLinesWidget)
        blackLinesWidget->AddToViewport();
}

I don’t understand what class I need to generate

none, blueprint generates it, just use function from 2nd link to get it