Java错误:NumberFormatException

时间:2016-03-30 03:05:15

标签: java numberformatexception

我的目标:相机正在尝试获取地形的位置并按照它进行直播,以便我可以看到它的位置,所以一旦我点击按钮,它就会在该位置添加地形。相机的原因是要找到我放置地形的位置,这样我就不会把它放在错误的位置,但我需要更新相机的位置,然后在输入内容时更新地形位置。

我的问题:好的问题是我有一个编辑gui来改变resiveTex所以当我这样做时我可以看到相机移动到地形的位置我添加"直播#34 ;。但是如果我输入一个新的值似乎打破了plsss通过告诉我发生了什么来帮助我我尝试查找它但它有点令人困惑!

错误:

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

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();
         } 
     }    
}

1 个答案:

答案 0 :(得分:0)

Camera.java中的第39行

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

numberAsString为空字符串,导致NFE

尝试使用以下方法通过将defaultValue设置为0

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