I'm using jSerialComm library and Scanner class. when i try to run my program I'm getting following error:
Exception in thread "main" java.lang.NullPointerException
at java.io.Reader.(Unknown Source)
at java.io.InputStreamReader.(Unknown Source)
at java.util.Scanner.(Unknown Source)
at SerialComm.main(SerialComm.java:44)
which points to this line : Scanner scanner = new Scanner(port.getInputStream());
import java.util.Scanner;
import com.fazecast.jSerialComm.*;
public class SerialComm {
public static void main(String[] args) {
// TODO Auto-generated method stub
SerialPort serPort[] = SerialPort.getCommPorts();
int i = 0;
for(SerialPort port : serPort)
{
System.out.println(i++ + " " + port.getSystemPortName());
}
Scanner s = new Scanner(System.in);
int selected = s.nextInt();
SerialPort port = serPort[selected];
System.out.println(port.getBaudRate());
port.setBaudRate(115200);
System.out.println(port.getBaudRate());
port.setNumDataBits(8);
port.setNumStopBits(1);
port.setParity(SerialPort.NO_PARITY);
try
{
port.openPort();
System.out.println("Connection is opend");
}
catch (Exception ex)
{
System.out.println("Chack connection ther is a problem");
}
port.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 100, 0);
Scanner scanner = new Scanner(port.getInputStream());
while(scanner.hasNextLine())
{
try
{
String line = scanner.nextLine();
System.out.println(line);
}
catch(Exception ex)
{
System.out.println("halo");
}
}
}
}
I have tested your code it works fine with my Arduino, this the Arduino sketch
void setup(){
Serial.begin(115200);
}
void loop(){
for(int i = 0;i<1024;i++){
Serial.println(i);
delay(1);
}
for(int i = 1023;i>0;i--){
Serial.println(i);
delay(1);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.