0
java를 통해 내 컴퓨터에 arduino를 연결하려고하는데, arduino가 stdo에게 보내는 것을 기록하는 온라인 주 코드를 발견했습니다. 나는 파일에 그것을 넣어하는 방법을 방황 한 스레드의 모든없이 좋은 것을 사용하는 동안 나는 "Scrittore"클래스를 생성하지만, 주요 코드로 사용하는 동안 그것은 나에게 오류 제공합니다스레드 포인터를 사용하는 동안 널 포인터 예외가 발생했습니다.
**Exception in thread "Thread-1" java.lang.NullPointerException
at ricezionearduinoseriale.Scrittore.scrivi(Scrittore.java:32)
at ricezionearduinoseriale.SerialTest$SerialReader.run(SerialTest.java:67)
at java.lang.Thread.run(Thread.java:724)**
수있는 사람을 버그 또는 내가 무엇을 잘못하고있는 중이을 찾는 나를 도와?
public class SerialTest{
public SerialTest()
{
super();
}
void connect (String portName) throws Exception
{
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
if (portIdentifier.isCurrentlyOwned())
{
System.out.println("Error: Port is currently in use");
}
else
{
CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
if (commPort instanceof SerialPort)
{
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
InputStream in = serialPort.getInputStream();
OutputStream out = serialPort.getOutputStream();
(new Thread(new SerialReader(in))).start();
(new Thread(new SerialWriter(out))).start();
}
else
{
System.out.println("Error: Only serial ports are handled by this example.");
}
}
}
/** */
public static class SerialReader implements Runnable
{
InputStream in;
Scrittore scrittura=new Scrittore();
public SerialReader (InputStream in)
{
this.in = in;
}
public void run()
{
byte[] buffer = new byte[1024];
int len = -1;
try
{
scrittura.open("output.txt");
//System.out.println(new String(buffer,0,len));
while ((len = this.in.read(buffer)) > -1)
{
String out=new String(buffer,0,len);
System.out.print(out);
scrittura.scrivi(out);
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
/** */
public static class SerialWriter implements Runnable
{
OutputStream out;
public SerialWriter (OutputStream out)
{
this.out = out;
}
public void run()
{
try
{
int c = 0;
while ((c = System.in.read()) > -1)
{
this.out.write(c);
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
public static void main (String[] args)
{
try
{
(new SerialTest()).connect("/dev/ttyACM0");
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class Scrittore {
FileWriter fw=null;
BufferedWriter bw=null;
public void open(String name){
try{
File file = new File(name);
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
System.out.println("Aperto");
}catch(IOException e){
e.printStackTrace();
}
}
public void scrivi(/*String content*/) {//Skipped input while debugging
String content="ciao";
try{
// System.out.println("Done");
bw.write(content);
}
catch(IOException e){
e.printStackTrace();
}
}
public void chiudi(){
try{
bw.close();
fw.close();
}catch(IOException e){
e.printStackTrace();
}
System.out.println("Chiuso");
}
}
(난 그냥 그에 아주 멍청한 놈 나는 아직도 자바와 함께 일하기 시작했다)
나는 강력하게 예외가 이전에 발생되고 있음을 의심 -하지만 당신은 예외를 많이 잡을 그냥 시스템에 덤프대로 대부분을 무시하고 있습니다. 마치 그들이 결코 일어나지 않았던 것처럼 계속됩니다. 그것은 거의 당신이 원하는 일이 아닙니다. –
그 점을 염두에 두겠다.하지만 나는 그 부분을 쓰지 않았다. 에러는 bw.write (content)를 호출하는 동안 scrivi() 메소드에있다. 코드에서 벗어나면 에러가 제거된다. – Franzo
글쎄, 'bw'는 null입니다. 이것은 null가 아닌 값을'bw '에 할당하는 메소드가 실패했기 때문에 쉽게 발생할 수 있습니다. –