title: Why can't I read an Integer in my Properties file?
body: <p>I'm trying to write a configs file in Java and I put my port number inside it for my HTTP Web Server to connect to and also the root path.</p>
  
  <p>Configs file:</p>
  
  <pre><code>root= some root
  port=8020
  </code></pre>
  
  <p>I am trying to access the properties like this:</p>
  
  <pre><code>FileInputStream file = new FileInputStream("config.txt");       
  //loading properties from properties file        
  config.load(file);
  
  int port = Integer.parseInt(config.getProperty("port"));   
  System.out.println("this is port " + port);
  </code></pre>
  
  <p>If I do it with a single parameter in the <code>getProperty</code> method, I get this error</p>
  
  <pre><code>"java.lang.NumberFormatException: null"
  </code></pre>
  
  <p>However, if I access it like this</p>
  
  <p><code>int port = Integer.parseInt(config.getProperty("port", "80"));</code> </p>
  
  <p>it works. </p>
  
  <p>Also, it works for <code>config.getProperty("root");</code> so I don't understand...</p>
  
  <p>Edit:</p>
  
  <pre><code>import java.net.*;
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.util.*;
  
  public class Server
  {
  
  
      public static void main(String[] args) throws Exception
      {
          boolean listening = true;
          ServerSocket server = null;     
          Properties config = new Properties();
          int port = 0;
          try
          {              
              //Reading properties file
              FileInputStream file = new FileInputStream("config.txt");       
              //loading properties from properties file        
              config.load(file);
  
              port = Integer.parseInt(config.getProperty("port"));   
              System.out.println("this is port " + port);
  
  
              System.out.println("Server binding to port " + port);
              server = new ServerSocket(port);
  
  
  
          }
          catch(FileNotFoundException e)
          {
              System.out.println("File not found: " + e);
          }
          catch(Exception e)
          {
              System.out.println("Error: " + e);
              System.exit(1);
          }
  
          System.out.println("Server successfully binded to port " + port);
  
          while(listening)
          {
              System.out.println("Attempting to connect to client");
              Socket client = server.accept();
              System.out.println("Successfully connected to client");
              new HTTPThread(client, config).start();
          }
  
          server.close();
      }
  
  }
  </code></pre>