0
このクラスTime
:別のクラスから変数を出力しますか?
public class Time {
int secondPassed = 0;
Timer mytime = new Timer()
TimerTask mytask = new TimerTask() {
public void run() {
secondPassed++;
if(secondPassed == 3) {
secondPassed = 0;
Udp callUdp = new Udp();
String trnsferRP = callUdp.trnsfrRPckt();
System.out.println(trnsferRP + "exp");
System.out.println("Its 10");
}
else {
System.out.println("Second : " + secondPassed);
}
}
};
public void start(){
mytime.scheduleAtFixedRate(mytask, 1000,1000);
}
public static void main(String[] args) throws Exception {
Time starter = new Time();
starter.start();
}
}
が、これはクラスUDP
です:
public class Udp {
public DatagramPacket receivePacket;
public String result;
public void socketp() throws Exception {
DatagramSocket clientSocket = new DatagramSocket(10076);
InetAddress IPAddress = InetAddress.getByName("192.168.100.10");
byte[] sendData = new byte[9];
byte[] receiveData = new byte[9];
String sentence = "VDV2bE1";
sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 10076);
clientSocket.send(sendPacket);
this.receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
clientSocket.close();
this.result = new String(receivePacket.getData());
return;
}
public String trnsfrRPckt(){
return this.result;
}
}
ので、私は、私は、受信したパケットを印刷したいんだということで、class Time
へclass Udp
から変数を呼び出すようにしようとしています。現在のところ私の結果は
second : 1
second : 2
nullexp
its 10
nullが出てきたのですか?パケットデータ+「EXP」でなければなりません
ありがとう、私はその前に試してキャッチする方法を知っていません。私はそのエラーに言いました。あなたは助けてくれてありがとうございます –
あなたのtryとcatchブロックは問題ありません。あなたのプログラムも大丈夫です。 tryとcatchブロックは例外処理に使用されます。 trnsfrRPcktがnullを返すので、socketpの前にtrnsfrRPckt()を呼び出すと例外がスローされません。あなたは単に人間のエラーを持っています.. :) – printfmyname
うん、それはそれのように働くので、それは..申し訳ありませんプログラミングの新しい、エラーを赦してください –