Exception in thread "main" java.lang.NullPointerException at Client.main(Client.java:10)
求教为啥是这样的错误怎么改importjava.io.*;importjava.net.*;publicclassClient{publicstaticvoidmain(...
求教为啥是这样的错误 怎么改
import java.io.*;import java.net.*;
public class Client {
public static void main(String [] args) throws Exception {
Socket socket = null;
BufferedReader keyBoard = new BufferedReader(new InputStreamReader(System.in));
DataOutputStream toserver = new DataOutputStream(socket.getOutputStream());
BufferedReader fromserver= new BufferedReader(new InputStreamReader(socket.getInputStream()));
String ip="127.0.0.1";
String data = keyBoard.readLine();
String serverData=fromserver.readLine();
int port=80;
try {
socket=new Socket(ip,port);
toserver.writeBytes(data+'\n');
System.out.println("服务器反馈:"+serverData);
} catch (Exception e) {
// TODO: handle exception
}
socket.close(); 展开
import java.io.*;import java.net.*;
public class Client {
public static void main(String [] args) throws Exception {
Socket socket = null;
BufferedReader keyBoard = new BufferedReader(new InputStreamReader(System.in));
DataOutputStream toserver = new DataOutputStream(socket.getOutputStream());
BufferedReader fromserver= new BufferedReader(new InputStreamReader(socket.getInputStream()));
String ip="127.0.0.1";
String data = keyBoard.readLine();
String serverData=fromserver.readLine();
int port=80;
try {
socket=new Socket(ip,port);
toserver.writeBytes(data+'\n');
System.out.println("服务器反馈:"+serverData);
} catch (Exception e) {
// TODO: handle exception
}
socket.close(); 展开
9个回答
展开全部
意思是:第10行出现空指针错误,也就是创建的对象不存在。你可以在第10行检查,需要对创建对象的那一句进行检查。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2012-10-12
展开全部
空据指针异常了,把所有的值都用System.out.println()打出来看看哪一个不能为空但又是空的呗
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
没有获取到相应的参数吧,空指针异常。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.io.*;import java.net.*;
public class Client {
public static void main(String [] args) throws Exception {
Socket socket = null;
try {
String ip="127.0.0.1";
int port=80;
socket=new Socket(ip,port);
BufferedReader keyBoard = new BufferedReader(new InputStreamReader(System.in));
DataOutputStream toserver = new DataOutputStream(socket.getOutputStream());
BufferedReader fromserver= new BufferedReader(new InputStreamReader(socket.getInputStream()));
String data = keyBoard.readLine();
String serverData=fromserver.readLine();
toserver.writeBytes(data+'\n');
System.out.println("服务器反馈:"+serverData);
socket.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
public class Client {
public static void main(String [] args) throws Exception {
Socket socket = null;
try {
String ip="127.0.0.1";
int port=80;
socket=new Socket(ip,port);
BufferedReader keyBoard = new BufferedReader(new InputStreamReader(System.in));
DataOutputStream toserver = new DataOutputStream(socket.getOutputStream());
BufferedReader fromserver= new BufferedReader(new InputStreamReader(socket.getInputStream()));
String data = keyBoard.readLine();
String serverData=fromserver.readLine();
toserver.writeBytes(data+'\n');
System.out.println("服务器反馈:"+serverData);
socket.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
追问
e.getmessage()能得到啥
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
DataOutputStream toserver = new DataOutputStream(socket.getOutputStream());
你socket为空,这件调用它的getOutputStream()报空值异常。要先实例化。
你socket为空,这件调用它的getOutputStream()报空值异常。要先实例化。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询