2013-06-24 26 views
-1

我有以下java代碼連接cassandra。它給出java.lang.NumberFormatExceptionjava.lang.NumberFormatException

有什麼想法嗎?它給

package com.retail; 

import org.apache.cassandra.thrift.*; 
import org.apache.thrift.protocol.*; 
import org.apache.thrift.transport.*; 

import java.lang.*; 

public class ShowKeyspaces { 

public static void main(String args[]) throws Exception { 

    String host = System.getenv("localhost"); 
    int port = Integer.parseInt(System.getenv("9160")); 

    TSocket socket = new TSocket(host,port); 
    TTransport transport = new TFramedTransport(socket); 

    TProtocol proto = new TBinaryProtocol(transport); 
    transport.open(); 

    Cassandra.Client client = new Cassandra.Client(proto); 

    System.out.println("Version: "+client.describe_version()); 
    System.out.println("cluster name : "+client.describe_cluster_name()); 

    transport.close(); 
    } 

} 

錯誤是:

**Exception in thread "main" java.lang.NumberFormatException: null 
at java.lang.Integer.parseInt(Unknown Source) 
at java.lang.Integer.parseInt(Unknown Source) 
at com.retail.ShowKeyspaces.main(ShowKeyspaces.java:14)** 

任何想法,我應該怎麼去這個???

謝謝!

+0

爲什麼'INT端口=的Integer.parseInt(System.getenv( 「9160」));',而不僅僅是'INT端口= 9160;'? –

+0

,因爲System.getenv只接受字符串參數,而在存儲它的端口時它應該是一個整數。 –

+2

它也是爲什麼你得到一個'java.lang.NumberFormatException',把它包裝在try catch中來驗證。以這種方式使用'system.getenv'確實沒有意義,也許你想'System.getenv(「port」)'? –

回答

2

你得到一個NumberFormatException,因爲這行:

int port = Integer.parseInt(System.getenv("9160")); 

我想你沒有設置的系統屬性和System.getenv("9160")將返回一個空字符串,又名「」。因此,無論set 9160 as an environment變量或者乾脆:

int port = 9160; 
+0

行「System.getenv(」localhost「);」包含一個非常相似的錯誤。我敢打賭,「本地主機」應該是一些環境變量的值而不是變量名。 – mschenk74

+0

@ mschenk74我同意你的觀點,與'System.getenv(「9160」)'相同。 –

相關問題