Hi I am using javax.comm api's serial port events in my application, i am using these events only. CD,CTS, and DSR. so whenever i fire an event i had to call a method from my base class. my problem is that whenever i call the method i am getting a nullpointer exception, the exception goes like this.
java.lang.NullPointerException
at myComport.serialEvent(TypicalPlayerApplet.java:1951)
at com.sun.comm.Win32SerialPort.sendCTSeventWin32SerialPort.java:559)
at com.sun.comm.NotificationThread.run Win32SerialPort.java:843)
Where TypicalPlayerApplet is my application and myComport is my method which implements serialporteventlistener.
I did not find much information on the web, about this. So i am posting this topic in this forum. Can anybody help me with this. My serialEvent code is like this..
public void serialEvent(SerialPortEvent ev) { if(this.port==null) { System.out.println(port.getName()+ "got serial event on a closed port"); return; } if(port.isCD()) // Carrier Detect { System.out.println("Carrier detect called-need to invoke the forward method"); owner.forward(); } else if(port.isCTS()) // Clear To Send { System.out.println("Clear To Send called-need to invoke the reverse method"); owner.reverse(); } else if(port.isDSR()) // Data Set Ready { System.out.println("Data Set Ready called-need to invoke the play method"); owner.play(); } }
in the above code port is my SerialPort object and owner is the application base class. the methods forward reverse and play are the methods written in side the base class. These work on a java media player.
java.lang.NullPointerException will be generated when some object in the method myComport.serialEvent is null. The error message cites line 1951 in java file TypicalPlayerApplet.java
You reference an object named owner several times: owner.forward(); owner.reverse(); owner.play(); This object may have been decleared somewhere in you program but never ininitialized (instantiated).
You could add a line of debug code in this method to test if it is null and print out a message.
if (owner == null) System.out.prinltn("owner is null");
recompile and run and see if the line gets executed at run time.
or you could add some dummy lines of debug code at several places in the method, recompile and see which lines get executed before the nullpointerexception.
System.out.println("Got here 111");
System.out.println("Got here 222");
System.out.println("Got here 333");
etc. then delete them when you figure out the problem.
This is the poor man's way of debugging.
My best guess is that owner is not initialized properly.