今天我在遗留代码中发现了一个错误,这让我有了一个令人震惊的发现:
String s = null + ""; //results in "null" string
s = String.valueOf(null); // NullPointerException is thrown
Integer i = null;
s = String.valueOf(i); //results in "null" string
Run Code Online (Sandbox Code Playgroud)
第一个问题是:为什么会发生这种意想不到的行为?
这意味着像这样的方便代码:
String age = person.getAge() + "";
Run Code Online (Sandbox Code Playgroud)
是完全出乎意料的。
第二个问题:获得 null 而不是“null”的最佳(最优雅)方法是什么
我正在使用 C# 上的新 MIP SDK 构建 POC 应用程序。要求之一是使用存储在服务器上的用户名/密码。所有示例应用程序都使用 OAuth2 登录,并带有一个用于用户凭据输入的弹出窗口。我相信正确实施 IAuthDelegate 可能会有所帮助,但内嵌文档并没有多大帮助。在我的引擎初始化方法中,我正在关注 SDK 示例
var authDelegate = new AuthDelegateImplementation(appInfo);
//Initialize and instantiate the File Profile
//Create the FileProfileSettings object
var profileSettings = new FileProfileSettings(
path: "mip_data",
useInMemoryStorage: true,
authDelegate: authDelegate,
consentDelegate: new ConsentDelegateImplementation(),
applicationInfo: appInfo,
minimumLogLevel: LogLevel.Trace);
Console.WriteLine("Load the Profile async and wait for the result");
var fileProfile = Task.Run(async () => await MIP.LoadFileProfileAsync(profileSettings)).Result;
Run Code Online (Sandbox Code Playgroud)
和 AuthDelegateImplementation 具有以下代码
public string AcquireToken(Identity identity, string authority, string resource)
{
AuthenticationContext authContext = new …Run Code Online (Sandbox Code Playgroud)