简体   繁体   中英

Java Error: NumberFormatException

My Goal: The camera is trying to get the location of the terrain and follow it live so I can see where its being placed so once I click the button it will add the terrain in that location. The reason for the camera is to find where I'm placing the terrain so I don't place it in the wrong spot but I need to update the camera position then with where the terrain location is when I type something.

My Problem: Ok so the issue is that I have a editor gui to change the resiveTex so when I do I can see the camera move with the place of the terrain I'm adding "live". But it seems to break if I enter a new value plsss help me by telling me whats going on I tried looking it up but its a bit confusing!

Error:

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

Line 39 in Camera.java

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

Where numberAsString is empty string that causing the NFE

Try using the following method to parse the integer by setting defaultValue to 0

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM