本文介绍了java.lang.NumberFormatException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下Java代码与cassandra连接。它给出了java.lang.NumberFormatException
I have following java code to connect with cassandra. it is giving 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)**
任何想法我应该怎么做?
Any idea how I should go about this???
谢谢!
推荐答案
由于以下原因,您得到了 NumberFormatException
:
You are getting a NumberFormatException
because of this line:
int port = Integer.parseInt(System.getenv("9160"));
我认为您尚未将 9160 设置为系统属性,并且 System.getenv( 9160)
返回一个空字符串,即。因此,可以将 9160 设置为环境变量或简单地:
I think you haven't set 9160 as a system property and System.getenv("9160")
is returning an empty string, aka "". So either set 9160 as an environment variable or simply:
int port = 9160;
这篇关于java.lang.NumberFormatException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!