• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Urgent!!! A NullPointerException !!! I can't solve it.

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have complied 3 files (SerialBean.java;SerialBuffer.java;ReaderSerial.java) as my package successfully, it is named serial. The package extends javax.comm.*; I wanna do some serial work.
Then I maked an application :SerialExample1
----------------------------SerialExmaple1.java-----------------------------

It is good enough to deliver the String "Hi,Chrono".
Then I maked another windows application :SerialExample2



It is a bad code!!! I compiled them seccessfully but It can't work .The java system gave
some wrong messages
-----------------------------------------------------------------------------------
Exception occurred during event dispatching:java.lang.NullPointerExceptionat serial.SerialBean.WritePort(SerialBean.java:115)at Frame1.button1_actionPerformed(Frame1.java:66)at Frame1_button1_actionAdapter.actionPerformed(Frame1.java:82)at java.awt.Button.processActionEvent(Button.java:329)at java.awt.Button.processEvent(Button.java:302)at java.awt.Component.dispatchEventImpl(Component.java:2593)at java.awt.Component.dispatchEvent(Component.java:2497)at java.awt.EventQueue.dispatchEvent(EventQueue.java:339)at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:131)at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:98)at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)at java.awt.EventDispatchThread.run(EventDispatchThread.java:85)-----------------------------------------------------------------------------------
I tried to debug it in Frame1.java:66 and SerialBean.java:115 but I can not find any error.
How to deal with it?
Here are the code about package serial


Help me !!!
[ September 02, 2002: Message edited by: Mapraputa Is ]
 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's pretty hard to follow, but my guess without actually running it is that the out variable is null. Probably because your Initialize() method is probably failing. You have "eaten" all useful the exceptions that it might throw and instead are returning an int - which you never bother to check.
Might I also recommend using the [CODE] tags when posting.
A style guide would do a world of good, too. There are lots out there, but this one is close at hand.
 
Chrono Wren
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much,I will do as your follow,but I really wanna sovle it quickly but I almost losed my head
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The error message:
Exception occurred during event dispatching:java.lang.NullPointerException at serial.SerialBean.WritePort(SerialBean.java:115)
indicates the NullPointerException occurs at line 115 in SerialBean.java, i think you should post that code too !!
 
Chrono Wren
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have posted all the code .please check them clearly.
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
However, it's pretty hard for us to guess where line 115 is. This would be easy for you to indicate.
- Peter
 
Chrono Wren
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SerialBean:115 is
 
Jacky Chow
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the problem is that you use both old AWT and swing components, you should use JButton to replace Button, and use JTextField to replace TextField.
 
author
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make sure that the String Msg parameter passed to the method is not null before you call length() on it in the for loop.
for( int i = 0; Msg != null && Msg.length() < i; i++ )
This might be it. Also check to make sure that the outputstream is not null as well.
Sean
 
Chrono Wren
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the advices.
I have tried all but failed.
 
Jacky Chow
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i found that you never check the return value of SerialBean.Initialize(), if the method return -1, NullPointerException may occur.
The line "out = serialPort.getOutputStream();" may return null, so you should check the "out" object after this line, so NullPointerException occur when you use it at line 155.
[ September 06, 2002: Message edited by: Jacky Chow ]
 
Chrono Wren
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya ,I know I have printed the "out" in the method
WritePort()
I got the Msg
then I got the int initialize()=-1 ;
but why ???
I cant understand ?
Any one can explain it clearly??
 
Dave Landers
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Initialize method has about 4 places where it catches an exception and returns a -1.
One of these is the problem. This is the reason for exceptions in the first place - they do tell valuable information about when things go wrong. Your code has killed this valuable information in favor of the meaningless -1.
Remove all the blocks that look like this:

and leave only the good "do something" part.
Then change your code to actually deal with those exceptions.
Or if you won't do that, at least do printStackTrace() on the exceptions you have caught.
Then you can tell us what's wrong
 
reply
    Bookmark Topic Watch Topic
  • New Topic