我想寫一個程序,將使用telnet發送GPS座標。
我不斷收到以下異常:試圖發送GPS座標到android模擬器
Exception in thread "Timer-0" java.lang.NullPointerException
at org.apache.commons.net.telnet.Telnet._sendByte(Telnet.java:1060)
at org.apache.commons.net.telnet.TelnetOutputStream.write(TelnetOutputStream.java:87)
at org.apache.commons.net.io.ToNetASCIIOutputStream.write(ToNetASCIIOutputStream.java:77)
at org.apache.commons.net.io.ToNetASCIIOutputStream.write(ToNetASCIIOutputStream.java:111)
at java.io.PrintStream.write(PrintStream.java:430)
at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:202)
at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:272)
at sun.nio.cs.StreamEncoder.flushBuffer(StreamEncoder.java:85)
at java.io.OutputStreamWriter.flushBuffer(OutputStreamWriter.java:168)
at java.io.PrintStream.write(PrintStream.java:477)
at java.io.PrintStream.print(PrintStream.java:619)
at java.io.PrintStream.println(PrintStream.java:756)
at com.example.myandroid.gpsSender$1.run(gpsSender.java:34)
at java.util.TimerThread.mainLoop(Timer.java:512)
at java.util.TimerThread.run(Timer.java:462)
我不知道爲什麼我收到此。你能告訴我嗎?感謝 這裏是我的代碼:
package com.example.myandroid;
import org.apache.commons.net.telnet.TelnetClient;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.SocketException;
import java.util.Timer;
import java.util.TimerTask;
public class gpsSender {
private TelnetClient telnet = new TelnetClient();
public static void main(String[] args) throws Exception {
gpsSender client = new gpsSender();
client.start();
}
public String start() throws Exception {
// Connect to the specified server
telnet.connect("localhost", 5554);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
float longitude = 1;
float latitude = 1;
int count = 0;
PrintStream out = new PrintStream(telnet.getOutputStream());
public void run() {
out.println("geo fix " + String.valueOf(longitude) + " "
+ String.valueOf(latitude));
out.flush();
System.out.println("geo fix " + String.valueOf(longitude) + " "
+ String.valueOf(latitude));
longitude++;
latitude++;
count++;
if (count > 1000) {
cancel();
}
}
}, 0, 1000);
try {
telnet.disconnect();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ("Done");
}
public void write(String value) {
try {
} catch (Exception e) {
e.printStackTrace();
}
}
}
非常感謝你!!!!! – dewijones92