2012-03-31 8 views
1

ich ein Java-Projekt haben (in NetBeans 7.1) und ich erhalte die folgende NullPointerException:Null-Zeiger-Ausnahme, wenn die serielle Schnittstelle den Zugriff auf

run: 
Port COM10 not found. 
The serial port you are trying to use is currently in usejava.lang.NullPointerException 
Exception in thread "main" java.lang.NullPointerException 
    at mateorssms.Communicator.ListenOnPort(Communicator.java:68) 
    at mateorssms.Communicator.<init>(Communicator.java:35) 
    at mateorssms.MateorsSMS.main(MateorsSMS.java:14) 
Java Result: 1 
BUILD SUCCESSFUL (total time: 1 second). 

Also meine MateorsSMS Klasse:

package mateorssms; 

import java.awt.Frame; 


public class MateorsSMS{  
public static void main(String[] args) { 
    //UserInterface UI = new UserInterface(); 
    //UI.setVisible(true); 
    new Communicator();   
    // TODO code application logic here 
} 
} 

und Communicator-Klasse (andere Klasse in einer anderen Datei) ist

package mateorssms; 

import java.io.IOException; 
import java.io.InputStream; 
import javax.comm.*; 
import java.util.Enumeration; 
import java.util.TooManyListenersException; 


public class Communicator implements Runnable, SerialPortEventListener{ 

static Enumeration portList; 
static CommPortIdentifier portId; 
boolean portFound = false;  
SerialPort serialPort; 
String defaultPort = "COM10"; 
Thread readThread; 
InputStream inputStream; 



public Communicator(){ 
    getPort(); 
    ListenOnPort(); 
} 




private void getPort(){ ////////////////////////////////////////////////////////////////////////////// 
    portFound = false; 
    portList = CommPortIdentifier.getPortIdentifiers(); 
    while(portList.hasMoreElements() && !(portFound)){ 
     portId = (CommPortIdentifier) portList.nextElement(); 
     if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL){ 
      if(portId.getName().equals(defaultPort)){ 
       System.out.println("Found port: " + defaultPort); 
       portFound = true; 
      } 
     } 
    } 
    if(!portFound){ 
     System.err.println("Port " + defaultPort + " not found."); //user feedback 
     //System.exit(1);    
    } 
} 

private void ListenOnPort(){ 
    try { 
     serialPort = (SerialPort) portId.open("MateorsSMSApp", 300); 
     System.out.println("yes"); 
    } catch (Exception e) { 
     System.out.println("The serial port you are trying to use is currently in use"+e.toString()); 
    } 

    try { 
     inputStream = serialPort.getInputStream(); 
     System.out.println(inputStream.toString()); 
    } catch (IOException e) { 
     System.err.println("Ex"); 
    } 
    try { 
     serialPort.addEventListener(this); 
    } catch (TooManyListenersException e) { 
     System.err.println("Ex"); 
    } 
    // activate the DATA_AVAILABLE notifier 
    serialPort.notifyOnDataAvailable(true); 
    try {   
     serialPort.setSerialPortParams(460800, 
       SerialPort.DATABITS_8, 
       SerialPort.STOPBITS_1, 
       SerialPort.PARITY_NONE); 
     serialPort.setDTR(true); 
     serialPort.setRTS(true); 
    } catch (UnsupportedCommOperationException e) { 
     System.err.println("Ex"); 
    } 

    readThread = new Thread(this); 
    readThread.start(); 
} 



@Override 
public void serialEvent(SerialPortEvent event) { 
    switch (event.getEventType()) { 
     case SerialPortEvent.BI: System.out.println("BI"); // Break interruptbreak; 
     case SerialPortEvent.OE: System.out.println("OE");// Overrun error break; 
     case SerialPortEvent.FE: System.out.println("FE");// Framing error break; 
     case SerialPortEvent.PE: System.out.println("PE");// Parity error break; 
     case SerialPortEvent.CD: System.out.println("CD");//Carrier detected break; 
     case SerialPortEvent.CTS: System.out.println("CTS");//Clear to send break; 
     case SerialPortEvent.DSR: System.out.println("DSR");// Data set ready break; 
     case SerialPortEvent.RI: System.out.println("RI");// Ring indicator break; 
     case SerialPortEvent.OUTPUT_BUFFER_EMPTY: System.out.println("OUTPUT_BUFFER_EMPTY"); //break; 
     //break; //Buffer empty 

     case SerialPortEvent.DATA_AVAILABLE: //Data Available to be read 

      System.out.println("DATA_AVAILABLE"); 
      byte[] readBuffer = new byte[20]; 

      try { 

       while (inputStream.available() > 0) { 
        int numBytes = inputStream.read(readBuffer); 
       } 

       String result = new String(readBuffer); 
       System.out.println(result); 
      } catch (IOException e) { 
       System.err.println("Ex"); 
      } 
      break; 
    } 
    //SerialPort port; 
    // TODO do something with ev 
} 


@Override 
public void run(){ 
    try { 
     Thread.sleep(3000); 
    } catch (InterruptedException ex){ 
     System.err.println("Ex"); 

    } 

}    
} 
+0

Sieht so aus, als ob der COM-Port, den Sie verwenden möchten, ausgelastet ist. Probieren Sie verschiedene serielle Schnittstellen aus, bis Sie feststellen, dass sie nicht verwendet werden. – asgs

Antwort

2

Es sieht aus wie diese Zeile:

serialPort = (SerialPort) portId.open("MateorsSMSApp", 300); 

löst eine Ausnahme aus, was wahrscheinlich bedeutet, dass serialPort nach dieser Zeile noch null ist, weil der Aufruf von open fehlgeschlagen.

dann auf der nächsten Try-Block Sie die folgende Zeile haben, die eine NPE werfen würde, wenn serialPort null ist:

inputStream = serialPort.getInputStream(); 

Es wäre Ihr Leben einfacher, wenn Sie nur einen Block try/catch hatte in der ListenOnPort Verfahren statt 3.

Hinweis: Methodennamen in Java im allgemeinen mit einem Kleinbuchstaben beginnen: ListenOnPort =>listenOnPort

0

Sie erhalten drei Ausnahmemeldungen:

Port COM10 not found. 

Diese Nachricht wird in getPort() erstellt. Wahrscheinlich portId ist immer noch Null nach der Rückkehr von dieser Methode.

The serial port you are trying to use is currently in usejava.lang.NullPointerException 

Diese Nachricht wird im ersten try-Block von ListenOnPort() erzeugt. Siehe das Ende "java.lang.NullPointerException" dieser Nachricht. Dies unterstützt die Behauptung, dass portId null ist.

Exception in thread "main" java.lang.NullPointerException 

nun von assylias bemerkt wie serialPort.getInputStream() der Aufruf fehlschlägt, weil serialPort null ist, auch.

Ich schlage vor, Sie verbessern die Fehlerberichte, z. Drucken Sie Nachrichten und stapeln Sie Spuren von Ausnahmen, die Sie mit e.printStackTrace() anstelle von fangen.

+0

stimmen mit der Fehlerberichterstattung überein, obwohl das Protokollieren in eine Datei oder Datenbank besser ist als das Drucken der Ausnahme stacktrace auf der 'err' oder' out' Konsole. – asgs

+0

Natürlich ist es. Mithilfe eines ordnungsgemäßen Protokollierungs-Frameworks können Sie die Protokollnachrichtenziele konfigurieren, ohne das Programm zu ändern. –

Verwandte Themen