2016-03-30 5 views
0

私の目標:カメラは地形の位置を取得しようとしているため、その位置に地形が追加されます。カメラの理由は私が地形を置いている場所を見つけて、間違った場所に置かないようにすることですが、カメラの位置を更新してから、何かを入力するときに地形の位置を更新する必要があります。Javaエラー:NumberFormatException

問題:私はエディタGUIを使ってresiveTexを変更しているので、カメラ移動が地形の場所で「ライブ」を追加しているのを見ることができます。しかし、私は新しい価値を入力すると壊れているように見えます。何が起こっているのかを私に教えてください。私はそれを見てみましたが、ちょっと混乱しました!

エラー:

Exception in thread "main" java.lang.NumberFormatException: For input string: "" 
at java.lang.NumberFormatException.forInputString(Unknown Source) 
at java.lang.Integer.parseInt(Unknown Source) 
at java.lang.Integer.parseInt(Unknown Source) 
at entities.Camera.TerLocToFloat(Camera.java:39) 
at entities.Camera.move(Camera.java:47) 
at Run.MainLoop.main(MainLoop.java:146) 

Window.java Camera.java

String numberAsString = Window.getTexupdate(); 
int number = Integer.parseInt(numberAsString); 

public class Window { 

    public static boolean NewTerrainCamPos = false; 

    public static String textVal; 
    public static String textVal2; 
    public static String resiveTex = "1"; 
    public static String resiveTex2; 

    public static final int Width = 1000; 
    public static final int Height = 720; 
    public static final int FPS_CAP = 120; 

    private static long lastFrameTime; 
    private static float delta; 

    public static void createDisplay(){ 

     ContextAttribs attribs = new ContextAttribs(3,2).withForwardCompatible(true).withProfileCore(true); 

     try { 
      Canvas openglSurface = new Canvas(); 
       JFrame frame = new JFrame(); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 

       //............................. 
       JMenuBar menuBar = new JMenuBar(); 
       JMenu terrain = new JMenu("Terrain"); 
       menuBar.add(terrain); 
       menuBar.add(terrain); 
       JMenuItem exit = new JMenuItem("Exit"); 
       JMenuItem newTerrain = new JMenuItem("add Terrain"); 
       JMenuItem editTerrain = new JMenuItem("Edit Terrain"); 

       newTerrain.addActionListener(new ActionListener(){ 
        public void actionPerformed(ActionEvent e) { 
         NewTerrainCamPos = true; 
         JFrame frame2 = new JFrame(); 
         frame2.setVisible(true); 
         frame2.setSize(300, 300); 
         //............................... 
         GridLayout experimentLayout = new GridLayout(3,2); 
         frame2.setLayout(experimentLayout); 
         //..................................... 
         JLabel xCord = new JLabel("XCoords: "); 
         JLabel zCord = new JLabel("ZCoords: "); 
         JTextField text = new JTextField(); 
         JTextField text2 = new JTextField(); 

         text.addKeyListener(new KeyListener(){ 

          @Override 
          public void keyTyped(KeyEvent arg0) { 
           resiveTex = text.getText(); 
          } 

          @Override 
          public void keyPressed(KeyEvent e) { 

          } 

          @Override 
          public void keyReleased(KeyEvent e) { 

          } 

         }); 

         resiveTex2 = text2.getText(); 

         JButton createTerrain = new JButton("CreateTerrain"); 

         createTerrain.addActionListener(new ActionListener(){ 
          TIDF terrainFileID; 
          public void actionPerformed(ActionEvent a){ 
           NewTerrainCamPos = false; 
           textVal = text.getText(); 
           textVal2 = text2.getText(); 
           TIDF.terrainIDFile(); 
          } 
         }); 

         frame2.add(xCord); 
         frame2.add(text); 
         frame2.add(zCord); 
         frame2.add(text2); 
         frame2.add(createTerrain); 
        } 
       }); 

       editTerrain.addActionListener(new ActionListener(){ 
        public void actionPerformed(ActionEvent e) { 
         JFrame frame3 = new JFrame(); 
         frame3.setVisible(true); 
         frame3.setSize(300, 300); 
         //...................................... 
         GridLayout experimentLayout = new GridLayout(3,2); 
         frame3.setLayout(experimentLayout); 
         //...................................... 
         JButton select = new JButton("Select"); 
         String terrainLocList[] = 
          { 
           "Item 1", 
           "Item 2", 
           "Item 3", 
           "Item 4" 
          }; 

         JList list = new JList(terrainLocList); 
         list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); 
         list.setVisibleRowCount(-1); 
         frame3.add(list); 
         frame3.add(select); 
        } 
       }); 

       terrain.add(newTerrain); 
       terrain.add(editTerrain); 
       terrain.add(exit); 
       frame.setJMenuBar(menuBar); 
       //......................................... 

       frame.setSize(1100, 1000); 
       frame.add(openglSurface); 
       frame.setVisible(true); 
       openglSurface.setSize(1000, 720); 
       Display.setParent(openglSurface); 

      Display.setDisplayMode(new DisplayMode(Width, Height)); 
      Display.create(new PixelFormat(), attribs); 
      frame.setTitle("Game editor 0.1"); 
     } catch (LWJGLException e) { 
      e.printStackTrace(); 
     } 

     GL11.glViewport(0, 0, Width, Height); 
     lastFrameTime = getCurrentTime(); 
    } 

    public static boolean Returnboolean(){ 
     return NewTerrainCamPos; 
    } 

    public static String getTex1() { 
     return textVal; 
    } 

    public static String getTex2(){ 
     return textVal2; 
    } 

    public static String getTexupdate(){ 
     return resiveTex; 
    } 

    public static String getTexupdate2(){ 
     return resiveTex2; 
    } 

    public static void updateDisplay(){ 
     Display.sync(FPS_CAP); 
     Display.update(); 
     long currentFrameTime = getCurrentTime(); 
     delta = (currentFrameTime - lastFrameTime)/1000f; 
     lastFrameTime = currentFrameTime; 
    } 

    public static float getFrameTimeSeconds(){ 
     return delta; 
    } 

    public static void closeDisplay(){ 
     Display.destroy(); 
    } 

    private static long getCurrentTime(){ 
     return Sys.getTime()*1000/Sys.getTimerResolution(); 
    } 

} 

public class Camera { 

    private static final float TERRAIN_HEIGHT = 0; 
    private boolean FPS = false; 
    private boolean isInAir = false; 

    private float distanceFromPlayer = 40; 
    private float angleAroundPlayer = 0; 

    private Vector3f position = new Vector3f(0,0,0); 
    private float pitch = 20; 
    private float yaw; 
    private float roll; 

    private static final float RUN_SPEED = 20; 
    private static final float GRAVITY = -50; 
    private static final float JUMP_POWER = 30; 
    private float upwardsSpeed = 0; 

    private Player player; 

    public Camera(Player player){ 
     this.player = player; 
    } 

    public void TerLocToFloat(){ 
     String numberAsString = Window.getTexupdate(); 
     int number = Integer.parseInt(numberAsString); 
     position.x = number; 
     position.z = 0; 
    } 

    public void move(){ 
     if(Window.Returnboolean() == true){ 
      AddTerrainCamPos(); 
      TerLocToFloat(); 
     } else{ 
      viewState(); 
     } 
    }  
} 
+2

にはdefaultValueを設定することにより、整数を解析するために、以下の方法を使用してみてくださいお手伝いをしてください。 –

+0

確かにそれを追加して、偶然にも私に知らせてくれたことを忘れてしまいました – furProgrammer

+2

JTextFieldでKeyListenerを使用することを含め、バットから見ることができるコードの問題がたくさんあります。まさにこのコードが何をしようとしているかについてもっと詳しく説明してください。 –

答えて

0

ライン39 numberAsStringがNFE

を引き起こしていることを、空の文字列があります

は常に完全例外またはエラーメッセージを投稿し、関与している線(S)を示す。、例外またはエラーについて尋ねる場合0

public static int parseInteger(String string, int defaultValue) { 
    try { 
    return Integer.parseInt(string); 
    } 
    catch (NumberFormatException e) { 
    return defaultValue; 
    } 
}