无法调用“java.net.URL.toExternalForm()”,因为“位置”为空Java Netbeans Graphics2D

ioekq8ef  于 2022-11-24  发布在  Java
关注(0)|答案(1)|浏览(546)

我有问题检测我的图像它总是说它的位置它的空idk做什么。我需要的图像被调用。我已经尝试了大多数的事情,仍然不能弄清楚。我已经尝试了在资源包,在另一个包,与完整的路径,仍然没有如果。

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package game.objeto;

import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.net.URL;
import javax.swing.ImageIcon;

/**
 *
 * @author sousa
 */
public class Player {
    
    public Player(){
       
        this.image=new ImageIcon(getClass().getResource("resources/plane.png")).getImage();
        this.image_speed=new ImageIcon(getClass().getResource("resources/plane_speed.png")).getImage();
    }
    
    
    public static final double PLAYER_SIZE=64;
    private double x;
    private double y;
    private float angle=0f;
    private final Image image;
    private final Image image_speed;
    
    public void changeAngle(float angle){
        if(angle<0){
            angle=359;
            
        }else if(angle>359){
            angle=0;
        }
        this.angle=angle;
    }
    
    public void draw(Graphics2D g2){
        AffineTransform oldTransform = g2.getTransform();
        g2.translate(x, y);
        g2.drawImage(image, 0,0,null);
        
        
        g2.setTransform(oldTransform);
    }
    
    public double getX(){
        return x;
    }
    
    public double getY(){
        return y;
    }
    
    public float getAngle(){
        return angle;
    }
}

且错误

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "java.net.URL.toExternalForm()" because "location" is null
    at java.desktop/javax.swing.ImageIcon.<init>(ImageIcon.java:232)
    at game.objeto.Player.<init>(Player.java:22)
    at game.componentes.PanelGame.initObjectGame(PanelGame.java:74)
    at game.componentes.PanelGame.start(PanelGame.java:69)
    at game.main.Main$1.windowOpened(Main.java:35)
    at java.desktop/java.awt.Window.processWindowEvent(Window.java:2082)
    at java.desktop/javax.swing.JFrame.processWindowEvent(JFrame.java:298)
    at java.desktop/java.awt.Window.processEvent(Window.java:2044)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4991)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324)
    at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4823)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:775)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:720)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:714)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:747)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:744)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
------------------------------------------------------------------------

Thx预付款

owfi6suc

owfi6suc1#

我猜你是在一个Web应用程序中。你有没有试着在“资源”前面加一个反斜杠?

this.image=new ImageIcon(getClass().getResource("/resources/plane.png")).getImage();
 this.image_speed=new ImageIcon(getClass().getResource("/resources/plane_speed.png")).getImage()

相关问题