[英]JavaFX - Why does this throw a java.lang.NullPointerException?
[英]Why does this code throw a java.lang.NullPointerException?
我找到了源代碼,並將其添加到框架中只是為了測試它是否使用Java2D。 但是它引發了一個例外。 我不明白為什么。
我的課:
package ClientGUI;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.RenderingHints;
import java.awt.geom.CubicCurve2D;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
/**
*
* @author ICC
*/
public class SignInFrame extends javax.swing.JFrame implements Runnable {
private static int iw, ih, iw2, ih2;
private static Image img;
private static final int FORWARD = 0;
private static final int BACK = 1;
// the points of the curve
private Point2D pts[];
// initializes direction of movement forward, or left-to-right
private int direction = FORWARD;
private int pNum;
private int x, y;
private Thread thread;
private BufferedImage bimg;
/** Creates new form SignInFrame */
public SignInFrame() {
initComponents();
img = getToolkit().getImage(Image.class.getResource("Yahoo-Messanger.jpg"));
try {
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(img, 0);
tracker.waitForID(0);
} catch (Exception e) {
}
iw = img.getWidth(this);
ih = img.getHeight(this);
iw2 = iw / 2;
ih2 = ih / 2;
}
public void reset(int w, int h) {
pNum = 0;
direction = FORWARD;
// initializes the cubic curve
CubicCurve2D cc = new CubicCurve2D.Float(
w * .2f, h * .5f, w * .4f, 0, w * .6f, h, w * .8f, h * .5f);
// creates an iterator to define the boundary of the flattened curve
PathIterator pi = cc.getPathIterator(null, 0.1);
Point2D tmp[] = new Point2D[200];
int i = 0;
// while pi is iterating the curve, adds points to tmp array
while (!pi.isDone()) {
float[] coords = new float[6];
switch (pi.currentSegment(coords)) {
case PathIterator.SEG_MOVETO:
case PathIterator.SEG_LINETO:
tmp[i] = new Point2D.Float(coords[0], coords[1]);
}
i++;
pi.next();
}
pts = new Point2D[i];
// copies points from tmp to pts
System.arraycopy(tmp, 0, pts, 0, i);
}
public void step(int w, int h) {
if (pts == null) {
return;
}
x = (int) pts[pNum].getX();
y = (int) pts[pNum].getY();
if (direction == FORWARD) {
if (++pNum == pts.length) {
direction = BACK;
}
}
if (direction == BACK) {
if (--pNum == 0) {
direction = FORWARD;
}
}
}
public void drawDemo(int w, int h, Graphics2D g2) {
g2.drawImage(img,
0, 0, x, y,
0, 0, iw2, ih2,
this);
g2.drawImage(img,
x, 0, w, y,
iw2, 0, iw, ih2,
this);
g2.drawImage(img,
0, y, x, h,
0, ih2, iw2, ih,
this);
g2.drawImage(img,
x, y, w, h,
iw2, ih2, iw, ih,
this);
}
public Graphics2D createGraphics2D(int w, int h) {
Graphics2D g2 = null;
if (bimg == null || bimg.getWidth() != w || bimg.getHeight() != h) {
bimg = (BufferedImage) createImage(w, h);
reset(w, h);
}
g2 = bimg.createGraphics();
g2.setBackground(getBackground());
g2.clearRect(0, 0, w, h);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
return g2;
}
@Override
public void paint(Graphics g) {
Dimension d = getSize();
step(d.width, d.height);
Graphics2D g2 = createGraphics2D(d.width, d.height);
drawDemo(d.width, d.height, g2);
g2.dispose();
g.drawImage(bimg, 0, 0, this);
}
public void start() {
thread = new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
public synchronized void stop() {
thread = null;
}
public static void main(String argv[]) {
SignInFrame f = new SignInFrame();
f.start();
}
public void run() {
Thread me = Thread.currentThread();
while (thread == me) {
repaint();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
break;
}
}
thread = null;
}}
例外:
init: deps-jar: Compiling 1 source file to C:\Users\ICC\Documents\NetBeansProjects\YahooServer\build\classes compile-single: run-single: Uncaught error fetching image: java.lang.NullPointerException at sun.awt.image.URLImageSource.getConnection(URLImageSource.java:97) at sun.awt.image.URLImageSource.getDecoder(URLImageSource.java:107) at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:240) at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:172) at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136)
令人討厭的行是img = getToolkit().getImage(Image.class.getResource("Yahoo-Messanger.jpg"));
確保該文件存在,請參考此文檔以查看如何加載資源的順序Java Doc for getResource
java.lang.NullPointerException
at sun.awt.image.URLImageSource.getConnection(URLImageSource.java:97)
我做出了一個瘋狂的猜測:URL為空。 您需要調試在堆棧跟蹤中首次出現自己的代碼時使用的變量。 我已經在您以前的主題之一中解釋了如何調試。
使用調試器並定義一個斷點,以便在引發NPE時停止應用程序。 然后,您將在代碼行中找到一個空引用,這會引起麻煩。 (或在打印在堆棧跟蹤上的代碼行上設置斷點)
沒有看到引發異常的代碼部分,幾乎不可能提供更詳細的幫助。
編輯
這次簡單的錯字? 該圖像文件名為Yahoo-Messanger.jpg
還是不應該為Yahoo-Messenger.jpg
? 可能是您找不到圖片。 不幸的是,您的stacktrace片段沒有在類中開始出現問題的代碼行。
我發現解決這些問題的最佳方法是每次僅一步。 該異常指出獲取圖像時出錯。 在SignInFrame()
您嘗試檢索圖像img = getToolkit().getImage(Image.class.getResource("Yahoo-Messanger.jpg"));
確保您正確指向圖像。 查看javadoc的getResource可能也有幫助。
另外,我認為(而且不是專家)通常最好將可能引發異常的方法放在try-catch中。 這樣,您可以確切地知道引發異常時錯誤發生的位置。
我認為,當您調用f.start()時,它將實際調用run()方法,而不是您自己的start()方法,因為該類實現了Runnable。
該問題已得到解答,但我建議在此行中進行兩個其他更改:
img = getToolkit().getImage(Image.class.getResource("Yahoo-Messanger.jpg"));
1-檢查返回的URL,如果未找到圖片,則該字段將為null
。
2-我認為Image.class
在這里具有誤導性。 使用getClass()
因為沒有(明顯的)理由使用Image類的Classloader。
URL url = getClass().getResource("Yahoo-Messanger.jpg");
if (url == null) {
// some error handling here: throw an Exception, logging, ...
}
img = getToolkit().getImage(url);
拋出異常將是最好的解決方案恕我直言。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.