检查StringBuilder是否为null

时间:2015-10-02 12:55:38

标签: c# .net

我尝试在尝试从缓存中填充StringBuilder之后检查StringBuilder videoSitemap; if (AppSettings.CachingEnabled) { videoSitemap = CacheHelper.Get<StringBuilder>("DynamicVideoSitemap"); } if (videoSitemap == null) { videoSitemap = new StringBuilder(); .... } 是否为空:

StringBuilder

但是在尝试编译时遇到以下错误:

  

CS0165使用未分配的变量'videoSitemap'

如何执行此操作然后检查对象是否为null - 如果我实例化它但不使用缓存,那么private static final Logger logger = LoggerFactory.getLogger(VlcjTest.class); private static final String NATIVE_LIBRARY_SEARCH_PATH = "C:\\Program Files\\VideoLAN\\VLC"; private static final String DUMP_NATIVE_MEMORY = "false"; static { // Initialise Log4J (this is good enough for testing, vlcj depends on log4j only for testing here) BasicConfigurator.configure(); LibXUtil.initialise(); if(null != NATIVE_LIBRARY_SEARCH_PATH) { logger.info("Explicitly adding JNA native library search path: '{}'", NATIVE_LIBRARY_SEARCH_PATH); NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), NATIVE_LIBRARY_SEARCH_PATH); } System.setProperty("jna.dump_memory", DUMP_NATIVE_MEMORY); } public static void main(String[] args) throws Exception { if(args.length != 1) { System.out.println("Specify a single MRL to stream"); System.exit(1); } String media = args[0]; String options = formatHttpStream("10.3.1.31", 5555); System.out.println("Streaming '" + media + "' to '" + options + "'"); MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory(args); HeadlessMediaPlayer mediaPlayer = mediaPlayerFactory.newHeadlessMediaPlayer(); mediaPlayer.playMedia(media, options); // Don't exit Thread.currentThread().join(); } private static String formatHttpStream(String serverAddress, int serverPort) { StringBuilder sb = new StringBuilder(60); sb.append(":sout=#duplicate{dst=std{access=http,mux=ts,"); sb.append("dst="); sb.append(serverAddress); sb.append(':'); sb.append(serverPort); sb.append("}}"); return sb.toString(); } 将永远不会为空

1 个答案:

答案 0 :(得分:8)

您需要指定默认值null以避免此错误。

StringBuilder videoSitemap = null;