0

我有以下java代码来连接cassandra。它给出了 java.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)**

知道我应该怎么做吗???

谢谢!

4

1 回答 1

3

NumberFormatException因为这条线,你得到了一个:

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

我认为您尚未将9160设置为系统属性,System.getenv("9160")而是返回一个空字符串,即“”。因此,要么9160设置为环境变量,要么简单地:

int port = 9160;
于 2013-06-24T09:23:11.267 回答