2013-08-20 46 views
-3

我使用eclipse將我的項目導出爲可運行jar文件,當我嘗試運行它時什麼都沒有發生,當我使用cmd運行它時出現此錯誤。程序不起作用 - NullPointerException

C:\Users\Enes\Desktop>cmd.exe 
Microsoft Windows [Version 6.1.7601] 
Copyright (c) 2009 Microsoft Corporation. All rights reserved. 

C:\Users\Enes\Desktop>java -jar Game.Jar 
Exception in thread "main" java.lang.reflect.InvocationTargetException 
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
     at java.lang.reflect.Method.invoke(Unknown Source) 
     at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoa 
der.java:58) 
Caused by: java.lang.NullPointerException 
     at scrolls.Resources.createArray(Resources.java:111) 
     at scrolls.Player.<init>(Player.java:31) 
     at scrolls.Draw.<init>(Draw.java:27) 
     at scrolls.Frame.main(Frame.java:18) 
     ... 5 more 

C:\Users\Enes\Desktop> 

當我使用eclipse運行它時,它運行正常,沒有錯誤或警告。

這是我的資源文件,這似乎在行導致問題111

package scrolls; 

import java.awt.Font; 
import java.awt.image.BufferedImage; 
import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileReader; 
import java.io.IOException; 

import javax.imageio.ImageIO; 

import org.imgscalr.Scalr; 

public class Resources 
{ 
    Map map; 
    static BufferedImage[] textures = new BufferedImage[8]; 
    static BufferedImage[] mapTextures = new BufferedImage[9]; 
    static BufferedImage texture; 
    static BufferedImage[] waterAnimated = new BufferedImage[64]; 
    static BufferedImage water; 
    static BufferedImage icon; 
    public static Font f, fs; 
    static int imageCounter = 0; 

    public Resources() 
    { 
     map = new Map(); 
     textures(); 
     createArray(texture, textures, 32, 1, 8); 
     createArray(water, waterAnimated, 32, 64, 1); 
     getFont(); 
     buildMapTextures(textures, mapTextures); 
    } 

    public static void counter() 
    { 
     imageCounter++; 
     if (imageCounter >= 500) 
      imageCounter = 0; 
     //System.out.println(imageCounter/8); 
    } 

    private void buildMapTextures(BufferedImage[] textures, BufferedImage[] mapTextures) 
    { 

     for (int i = 0; i <= 7; i++) 
     { 
      mapTextures[i] = resize(textures[i], 3, 3); 
     } 
     mapTextures[8] = resize(waterAnimated[2], 3, 3); 
    } 

    private BufferedImage resize(BufferedImage image, int newW, int newH) 
    { 
     BufferedImage thumbnail = Scalr.resize(image, Scalr.Method.ULTRA_QUALITY, Scalr.Mode.FIT_EXACT, newW, newH, Scalr.OP_ANTIALIAS); 
     return thumbnail; 
    } 

    public static BufferedImage waterAnimation() 
    { 
     return waterAnimated[imageCounter/8]; 
    } 

    private void textures() 
    { 
     try 
     { 
      texture = ImageIO.read(new File("src/resources/textures.png")); 
     } catch (IOException e) 
     { 
     } 

     try 
     { 
      water = ImageIO.read(new File("src/resources/water.png")); 
     } catch (IOException e) 
     { 
     } 

     try 
     { 
      icon = ImageIO.read(new File("src/resources/icon.png")); 
     } catch (IOException e) 
     { 
     } 

    } 

    static BufferedImage player() 
    { 
     BufferedImage player = null; 
     try 
     { 
      player = ImageIO.read(new File("src/resources/player.png")); 
     } catch (IOException e) 
     { 
     } 
     return player; 
    } 

    static void createArray(BufferedImage image, BufferedImage[] images, int size, int rows, int cols) 
    { 
     BufferedImage temp = image; 

     for (int i = 0; i < rows; i++) 
     { 
      for (int j = 0; j < cols; j++) 
      { 
       images[(i * cols) + j] = temp.getSubimage(j * size, i * size, size, size); // line 111 
      } 
     } 
    } 

    public static void readLevel(String filename, int[][] level, int part) 
    { 
     try 
     { 
      File f = new File("src/resources/levels/" + part + "/" + filename + ".txt"); 
      FileReader fr = new FileReader(f); 
      BufferedReader in = new BufferedReader(fr); 
      StringBuilder sb = new StringBuilder(); 
      byte b = 0; 
      while ((b = (byte) in.read()) != -1) 
      { 
       sb.append("" + ((char) b)); 
      } 
      String str = sb.toString(); 
      String[] lines = str.split("(\n|\r)+"); 
      for (int i = 0; i < lines.length; i++) 
      { 
       for (int j = 0; j < lines[i].length(); j++) 
       { 
        level[i][j] = Integer.parseInt("" + lines[i].charAt(j)); 
       } 
      } 
      in.close(); 
     } catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 

    private static void getFont() 
    { 
     try 
     { 
      f = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream("src/resources/Jet Set.ttf")); 
      fs = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream("src/resources/Jet Set.ttf")); 
     } catch (Exception e) 
     { 
      System.out.println(e); 
     } 
     f = f.deriveFont(22f); 
     fs = fs.deriveFont(13f); 
    } 

} 

播放器代碼

package scrolls; 

import java.awt.Graphics2D; 
import java.awt.GraphicsConfiguration; 
import java.awt.GraphicsEnvironment; 
import java.awt.Transparency; 
import java.awt.event.KeyEvent; 
import java.awt.image.BufferedImage; 

public class Player 
{ 
    static int x, y, dx, dy; 
    BufferedImage[] sprites = new BufferedImage[8]; 
    int rotation = 0; 
    int imageCounter = 0; 
    public static boolean moving = false; 
    static int playerEnergy = 150000; 
    static int playerLvl = 1; 
    static int playerExp = 3; 
    static int expNeeded = (((playerLvl + 1) * playerLvl) * 2); 
    static int playerHealth = 100; 
    static int playerMana = 100; 
    static int mapRow = 6; 
    static int mapColumn = 8; 
    static int playerRow, playerColumn; 

    public Player() 
    { 
     y = 40; 
     x = 700; 
     Resources.createArray(Resources.player(), sprites, 66, 1, 8); 
    } 

    private void changeImage() 
    { 
     imageCounter++; 
     if (imageCounter >= 80) 
      imageCounter = 0; 
    } 

    public void move() 
    { 
     y = y + dy; 
     x = x + dx; 
     changeImage(); 
     playerPosition(); 
    } 

    static void mapPosition() 
    { 
     if (y < 0) 
      playerRow = 0; 
     else 
      playerRow = (y/32) + 1; 
     if (x < 0) 
      playerColumn = 0; 
     else 
      playerColumn = (x/32) + 1; 
    } 

    private void playerPosition() 
    { 
     if (x >= 817 - 59) 
     { 
      x = -24; 
      mapColumn++; 
     } 

     if (x <= -25) 
     { 
      x = 817 - 59; 
      mapColumn--; 
     } 

     if (y <= -25) 
     { 
      y = 599 - 152 - 41; 
      mapRow--; 
     } 
     if (y >= 599 - 152 - 40) 
     { 
      y = -24; 
      mapRow++; 
     } 
    } 

    public static int playerExp() 
    { 

     return playerExp; 
    } 

    public static int getNextExp() 
    { 
     return expNeeded; 
    } 

    public static int playerLvl() 
    { 
     if (playerExp >= expNeeded) 
     { 
      playerLvl++; 
     } 
     return playerLvl; 
    } 

    public static int playerHealth() 
    { 
     return playerHealth; 
    } 

    public static int playerMana() 
    { 
     return playerMana; 
    } 

    public static int playerEnergy() 
    { 
     if ((dx != 0) || (dy != 0)) 
      playerEnergy--; 
     if ((dx != 0) && (dy != 0)) 
      playerEnergy--; 

     return playerEnergy; 
    } 

    public int getX() 
    { 
     return x; 
    } 

    public int getY() 
    { 
     return y; 
    } 

    public static BufferedImage rotate(BufferedImage image, double angle) 
    { 
     double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle)); 
     int w = image.getWidth(), h = image.getHeight(); 
     int neww = (int) Math.floor(w * cos + h * sin), newh = (int) Math.floor(h * cos + w * sin); 

     GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration(); 
     BufferedImage result = gc.createCompatibleImage(neww, newh, Transparency.TRANSLUCENT); 
     Graphics2D g = result.createGraphics(); 

     g.translate((neww - w)/2, (newh - h)/2); 
     g.rotate(angle, w/2, h/2); 
     g.drawRenderedImage(image, null); 
     g.dispose(); 

     return result; 
    } 

    public BufferedImage getPlayerImage() 
    { 

     roatePlayer(); 
     int image = animatePlayer(); 

     double angle = Math.toRadians(rotation); 

     if (dy != 0 || dx != 0) 
     { 
      return rotate(sprites[image], angle); 
     } 
     return rotate(sprites[0], angle); 

    } 

    private int animatePlayer() 
    { 
     return imageCounter/10; 
    } 

    private void roatePlayer() 
    { 
     if (dy > 0) 
      rotation = 0; 
     if (dy < 0) 
      rotation = 180; 
     if (dx > 0) 
      rotation = -90; 
     if (dx < 0) 
      rotation = 90; 

     if (dy > 0 && dx > 0) 
      rotation = -45; 
     if (dy > 0 && dx < 0) 
      rotation = 45; 
     if (dy < 0 && dx < 0) 
      rotation = 135; 
     if (dy < 0 && dx > 0) 
      rotation = -135; 
    } 

    public void keyPressed(KeyEvent e) 
    { 
     int key = e.getKeyCode(); 

     if (key == KeyEvent.VK_A) 
     { 
      dx = -1; 
      rotation = -90; 
     } 

     if (key == KeyEvent.VK_S) 
     { 
      dy = 1; 
      rotation = 0; 
     } 

     if (key == KeyEvent.VK_D) 
     { 
      dx = 1; 
      rotation = 90; 
     } 

     if (key == KeyEvent.VK_W) 
     { 
      dy = -1; 
      rotation = 180; 
     } 

    } 

    public void keyReleased(KeyEvent e) 
    { 
     int key = e.getKeyCode(); 

     if (key == KeyEvent.VK_A) 
      dx = 0; 

     if (key == KeyEvent.VK_S) 
      dy = 0; 

     if (key == KeyEvent.VK_D) 
      dx = 0; 

     if (key == KeyEvent.VK_W) 
      dy = 0; 
    } 

} 
+1

你得明明顯示的代碼。 –

+2

您的代碼有一個錯誤,您可以通過調試來發現它。在這個時代,晶球已經停止工作,所以如果你沒有向我們展示你的代碼,我們不能幫你。 – Renan

+0

@Ranan:我會說水晶球有些朦朧,但並沒有完全停止工作。更多的信息肯定會有幫助,雖然... –

回答

7

我強烈懷疑,你加載一些資源(聲音,圖像)或者通過假設它們的存在爲文件,或者您正在使用適當的getResource/getResourceAsStream調用,但是您的資源不存在於jar文件中。如果沒有看到任何代碼或jar文件中的內容,我們就無法確定,但是您應該檢查您加載資源的位置,以及爲什麼希望找到資源。

哦,你也可能有一個套管問題 - 當它從Windows文件系統加載資源時,即使文件名爲foo.png,要求FOO.PNG也會工作;同樣是而不是從jar文件加載資源時爲true。

當然,您應該查看Resources.java第111行和Player.java第31行,以幫助確定發生了什麼問題(例如哪些資源出現故障)。

編輯:好的,現在我們已經得到了代碼,它是正如我第一次建議的。這Resource.player()的代碼行:

player = ImageIO.read(new File("src/resources/player.png")); 

...加載player.png期待它是本地文件系統中的文件。你想要的東西,如:

player = ImageIO.read(Resource.class.getResource("/src/resources/player.png")); 

很奇怪有一個src文件夾中的jar文件,順便說一句。如果你已經實際上剛剛在reources目錄中的圖片,你會想:

player = ImageIO.read(Resource.class.getResource("/resources/player.png")); 
+0

我用Winrar檢查了jar文件,所有的文件都在那裏。這是線似乎是導致問題,日食編譯罰款,並沒有任何錯誤 圖像[(i * cols)+ j] = temp.getSubimage(j *大小,i *大小,大小,大小); 我發佈了以上完整代碼 – user2457344

+0

@ user2457344:什麼是'temp'?它是如何初始化的?您仍然給我們*無論任何代碼*,也沒有任何指示你正在加載什麼文件。我強烈懷疑這是我列出的三個問題之一,但我可以給你*沒有更多的幫助*沒有更多的信息。 –

+0

我發佈了以上完整代碼 – user2457344