异常详情
Java在Ubuntu下访问字体方法报NPE。具体堆栈信息如下:
Caused by: java.lang.NullPointerException at sun.awt.FontConfiguration.getVersion(FontConfiguration.java:1262) ~[?:?] at sun.awt.FontConfiguration.readFontConfigFile(FontConfiguration.java:225) ~[?:?] at sun.awt.FontConfiguration.init(FontConfiguration.java:107) ~[?:?] at sun.awt.X11FontManager.createFontConfiguration(X11FontManager.java:719) ~[?:?] at sun.font.SunFontManager$2.run(SunFontManager.java:367) ~[?:?] at java.security.AccessController.doPrivileged(AccessController.java:310) ~[?:?] at sun.font.SunFontManager.(SunFontManager.java:312) ~[?:?] at sun.awt.FcFontManager.(FcFontManager.java:35) ~[?:?] at sun.awt.X11FontManager.(X11FontManager.java:56) ~[?:?] at jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:?] at jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[?:?] at jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:?] at java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500) ~[?:?] at java.lang.reflect.Constructor.newInstance(Constructor.java:481) ~[?:?] at sun.font.FontManagerFactory$1.run(FontManagerFactory.java:84) ~[?:?] at java.security.AccessController.doPrivileged(AccessController.java:310) ~[?:?] at sun.font.FontManagerFactory.getInstance(FontManagerFactory.java:74) ~[?:?] at sun.font.SunFontManager.getInstance(SunFontManager.java:247) ~[?:?] at sun.font.FontDesignMetrics.getMetrics(FontDesignMetrics.java:265) ~[?:?] at sun.java2d.SunGraphics2D.getFontMetrics(SunGraphics2D.java:856) ~[?:?]
解决方案
ubuntu
sudo apt install fontconfig
centos
sudo yum install fontconfig
Reading package lists… Done Building dependency tree Reading state information… Done The following additional packages will be installed: fontconfig-config fonts-dejavu-core libfontconfig1 The following NEW packages will be installed: fontconfig fontconfig-config fonts-dejavu-core libfontconfig1 0 upgraded, 4 newly installed, 0 to remove and 0 not upgraded. Need to get 1,355 kB of archives. After this operation, 3,902 kB of additional disk space will be used.
具体原因
jdk缺少字体, 在生成图片时获取不到font导致NPE。