2016-12-30 146 views
-1

我知道我的所有代碼都是正確的,除了路徑。我一直在使用C:/Users/Julian Jacobs/Pictures/Saved Pictures/spaceship/spaceship.png圖像的文件路徑

我曾嘗試:

  1. 使用\\而不是使用/
  2. _代替空格

有人能解釋正確的格式,我需要改變什麼在我的路上?

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Image; 
import javax.swing.ImageIcon; 
import javax.swing.JPanel; 

public class Content extends JPanel { 
int radius = 50; 
private Image spaceship; 
    public Content() { 
     super.setDoubleBuffered(true); 
    } 

    public void paintComponent(Graphics g){ 
     ImageIcon Ship = new ImageIcon(this.getClass().getResource("C:/Users/Julian Jacobs/Pictures/Saved Pictures/spaceship/spaceship.png")); 
     spaceship = Ship.getImage(); 
     Graphics2D g2d= (Graphics2D)g; 
     g2d.drawImage(spaceship, 100, 100, this); 
     g2d.setColor(Color.DARK_GRAY); 
     g2d.fillOval(100, 100, radius, radius); 

    } 
} 

拋出異常:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
    at javax.swing.ImageIcon.<init>(Unknown Source) 
    at Content.paintComponent(Content.java:17) 
    at javax.swing.JComponent.paint(Unknown Source) 
    at javax.swing.JComponent.paintChildren(Unknown Source) 
    at javax.swing.JComponent.paint(Unknown Source) 
    at javax.swing.JComponent.paintChildren(Unknown Source) 
    at javax.swing.JComponent.paint(Unknown Source) 
    at javax.swing.JLayeredPane.paint(Unknown Source) 
    at javax.swing.JComponent.paintChildren(Unknown Source) 
    at javax.swing.JComponent.paintToOffscreen(Unknown Source) 
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source) 
    at javax.swing.RepaintManager$PaintManager.paint(Unknown Source) 
    at javax.swing.RepaintManager.paint(Unknown Source) 
    at javax.swing.JComponent.paint(Unknown Source) 
    at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source) 
    at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source) 
    at sun.awt.SunGraphicsCallback.runComponents(Unknown Source) 
    at java.awt.Container.paint(Unknown Source) 
    at java.awt.Window.paint(Unknown Source) 
    at javax.swing.RepaintManager$4.run(Unknown Source) 
    at javax.swing.RepaintManager$4.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) 
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) 
    at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source) 
    at javax.swing.RepaintManager.access$1200(Unknown Source) 
    at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source) 
    at java.awt.event.InvocationEvent.dispatch(Unknown Source) 
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
    at java.awt.EventQueue.access$500(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue.dispatchEvent(Unknown Source) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.run(Unknown Source) 
+0

\\代替/ –

+0

進行驗證問題是什麼,您是否無法訪問該文件? – amishra

+0

是的,它必須是地址 –

回答

2

您需要使用\\。這是因爲\用於指定轉義序列。例如\n是換行符。 \\實際上給你一個\。所以,Java程序中的字符串"C:\\Users\\Julian Jacobs\\Pictures\\Saved Pictures\\spaceship\\spaceship.png"會給你:C:\Users\Julian Jacobs\Pictures\Saved Pictures\spaceship\spaceship.png。您可以使用System.out.println("C:\\Users\\Julian Jacobs\\Pictures\\Saved Pictures\\spaceship\\spaceship.png")

+0

我已經嘗試過,但由於 –

+0

@JulianJacobs你試過了,它沒有用,但它仍然是正確的,因爲問題不在路徑之中。這正確地回答了有關如何格式化路徑的問題。 –