我有一个去年5月左右的旧项目,其工作原理与预期的相同,但是当我将代码复制/粘贴到新项目中时,它将引发异常。有问题的代码是一个简单的TCP / IP客户端。
当粘贴到新项目中时,它将引发NPE异常:
com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: Thread-5
Process: com.example.myapplication, PID: 32308
java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.DataOutputStream.writeBytes(java.lang.String)' on a null object reference
at com.example.myapplication.MainActivity$sendMessage.run(MainActivity.java:212)
at java.lang.Thread.run(Thread.java:761)
我试图寻找有关Socket NPE的答案,但是由于在旧项目中运行时此代码已经(并且正在运行),所以我找不到与之相关的任何信息。
我使用了Studios调试器,正如人们期望的那样,显示了“ socket = null”。这是我的套接字在带有“ catch”的“ try”块中的初始化:
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
socket = new Socket(serverAddr, 502);
os = new DataOutputStream(socket.getOutputStream());
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
完整代码:
public class MainActivity extends AppCompatActivity
{
public EditText serverIp,smessage;
public TextView chat,VALUE0,VALUE1,VALUE2,VALUE3;
public Button connectPhones,disconnectPhones,send;
public String serverIpAddress = "",msg = "",msg1 = "",str = "";
public String substrVALUE,substrMSG = "";
public Handler handler = new Handler();
public Socket socket;
public DataOutputStream os;
public BufferedReader in;
public int i,ID,TYPE;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
serverIp = (EditText) findViewById(R.id.server_ip);
smessage = (EditText) findViewById(R.id.smessage);
chat = (TextView) findViewById(R.id.chat);
VALUE0 = (TextView) findViewById(R.id.VALUE0);
VALUE1 = (TextView) findViewById(R.id.VALUE1);
VALUE2 = (TextView) findViewById(R.id.VALUE2);
VALUE3 = (TextView) findViewById(R.id.VALUE3);
send = (Button) findViewById(R.id.send_button);
connectPhones = (Button) findViewById(R.id.connectPhones);
disconnectPhones = (Button) findViewById(R.id.disconnectPhones);
connectPhones.setEnabled(true);
disconnectPhones.setEnabled(false);
send.setEnabled(false);
connectPhones.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
connectPhones.setEnabled(false);
disconnectPhones.setEnabled(true);
send.setEnabled(true);
serverIpAddress = serverIp.getText().toString();
str = "";
if (!serverIpAddress.equals(""))
{
Thread clientThread = new Thread(new ClientThread());
clientThread.start();
}
}
});
send.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Thread sendThread = new Thread(new sendMessage());
sendThread.start();
}
});
disconnectPhones.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
connectPhones.setEnabled(true);
disconnectPhones.setEnabled(false);
send.setEnabled(false);
try
{
socket.close();
handler.post(new Runnable() {
@Override
public void run() {
chat.setText("");
str = "";
}
});
}
catch (IOException e)
{
}
str = "";
msg = "";
}
});
}
public class ClientThread implements Runnable
{
public void run()
{
try
{
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
socket = new Socket(serverAddr, 502);
os = new DataOutputStream(socket.getOutputStream());
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while(true) {
String line = null;
while ((line = in.readLine()) != null)
{
msg1 = line;
substrMSG=msg1.toString();
//if ( substrMSG.substring(0,1).matches("S"))
//{
i = 0;
while (i < msg1.length())
{
String cutting and printing to textView
}
Thread.sleep(100);
}
}
catch (Exception e)
{
}
}
}
class sendMessage implements Runnable
{
@Override
public void run()
{
try
{
str = smessage.getText().toString();
str = str + "\n";
msg = msg + "C: " + str;
handler.post(new Runnable()
{
@Override
public void run()
{
chat.setText(msg);
}
});
os.writeBytes(str);
os.flush();
}
catch(IOException e)
{
}
}
}
}
我必须进行哪些更改才能使其正常工作?
答案 0 :(得分:1)
正如EpicPandaForce所说,我忘记设置正确的权限。