首页 新闻 会员 周边 捐助

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

0
[待解决问题]

用Java运行一个实例,总是抛出空指针异常,究竟问题出在那,请指教,实例如下:


import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.SplashScreen;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;

public class SplashScreenTest {

private static void inital() {
SplashScreen splashScreen = SplashScreen.getSplashScreen();
Toolkit toolkit = Toolkit.getDefaultToolkit();
final Image image = toolkit.getImage(splashScreen.getImageURL());
final JFrame splashFrame = new JFrame();
splashFrame.setUndecorated(true);
final JPanel splashPanel = new JPanel() {

/**
*
*/
private static final long serialVersionUID = -1289515968041228683L;

@Override
protected void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, null);
}
};
final JProgressBar progressBar = new JProgressBar();
progressBar.setStringPainted(true);
splashPanel.setLayout(new BorderLayout());
splashPanel.add(progressBar, BorderLayout.SOUTH);

splashFrame.add(splashPanel);
splashFrame.setSize(splashScreen.getSize().width, splashScreen.getSize().height + 20);
Dimension splashSize = new Dimension();
splashSize.width = splashScreen.getSize().width;
splashSize.height = splashScreen.getSize().height + 20;
Dimension screenSize = toolkit.getScreenSize();
splashFrame.setLocation((screenSize.width - splashSize.width) / 2, (screenSize.height - splashSize.height) / 2);
splashFrame.setVisible(true);

new Thread(new Runnable() {

@Override
public void run() {
for (int i = 0; i < 100; i++) {
progressBar.setString("已经加载:" + i + "%");
progressBar.setValue(i);
splashPanel.repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
splashFrame.setVisible(false);
}
}).start();
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
inital();
}
});
}
}

 

 

控制台出现z空指针异常情况如下:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.mingrisoft.splashscreen.SplashScreenTest.inital(SplashScreenTest.java:20)
at com.mingrisoft.splashscreen.SplashScreenTest.access$0(SplashScreenTest.java:17)
at com.mingrisoft.splashscreen.SplashScreenTest$3.run(SplashScreenTest.java:73)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

LIEBER_JIANG的主页 LIEBER_JIANG | 菜鸟二级 | 园豆:202
提问于:2017-02-07 21:46
< >
分享
所有回答(1)
0

SplashScreenTest.inital(SplashScreenTest.java:20) 这个第20行是哪个函数,估计没有实例化出来

2012 | 园豆:21645 (高人七级) | 2017-02-08 08:46

第二十行函数是:SplashScreen splashScreen = SplashScreen.getSplashScreen();就是不知道该如何做,还请指教,谢谢啦

支持(0) 反对(0) LIEBER_JIANG | 园豆:202 (菜鸟二级) | 2017-02-08 19:49

@LIEBER_JIANG: http://stackoverflow.com/questions/15964323/how-do-i-use-splashscreen-without-throwing-a-nullpointerexception

支持(0) 反对(0) 2012 | 园豆:21645 (高人七级) | 2017-02-09 08:20
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册