• Post Reply Bookmark Topic Watch Topic
  • New Topic

Convert String to Array or Bytes  RSS feed

 
Patrick Evans
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

First project here and it has been a steep learning curve. I have some code in the TwoWaySerialComm class that will write to a Com port. In my other class EBIAlarm i have my GUI. The aim of my app is the send strings out of the Com port by pressing Jbutton1-3 I can open the Com port but I don't know how to send the string. Any pointers?


Convert String to Array of Bytes.



 
S Majumder
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Patrick ,
Can you explain where you are getting the problem

Satya
 
Patrick Evans
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Satya,

The problem lies in jButton1

I cant work out how to have jButton1 send the string called 'alarm1'

I tried

byte[] c = string.getBytes();
byte[] c = string.getBytes(Charset.forName("UTF-8"));

TwoWaySerialComm.SerialWriter.this.out.write(c);

And various other iterations with no joy
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly do you mean with "no joy"? Do you get a compiler error, or an error at runtime? Or does the program do something else than what you expect? If so, then what exactly did you expect and what does the program actually do - and how does it differ from what you expect?

If you get any error messages, then please copy and paste the exact error message - error messages contain important information that tell you what's wrong.
 
Patrick Evans
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well the error changes depending on what i write. Currently in my Jbutton1ActionPerformed I have written TwoWaySerialComm.SerialWriter.this.out.write(c); The errors produced are:

Multiple markers at this line
- c cannot be resolved to a variable
- No enclosing instance of the type TwoWaySerialComm.SerialWriter is accessible
in scope

Thanks.
 
Patrick Evans
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error happens when I press the jButton1 I tried to get the error to print to jTextFiels 2 but i shall ask about that in another question.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Patrick Evans wrote:- c cannot be resolved to a variable

This means that you are trying to use a variable named c, but that variable is not defined at the point you are trying to use it.

Patrick Evans wrote:- No enclosing instance of the type TwoWaySerialComm.SerialWriter is accessible

This probably is because your nested class SerialWriter is not static.

Note that your nested class SerialReader is static (line 55), but SerialWriter is not (line 85).
 
Patrick Evans
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.

I remember changing that whilst trying to get it working. I have changed it back to: public static class SerialWriter implements Runnable but i get the same error.

With the local variable, Eclipse suggests adding byte[] c; but then the error remains for that also?


 
Patrick Evans
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alternatively, I tried

But then the error is Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:
Unhandled exception type IOException
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How were you able to compile that last bit of code? you should have gotten a compiler error. The write method might throw IOException, and since it's a checked exception you are required to handle it with a try-catch block.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can only get that sort of code to compile on Eclipse. That is what we have an FAQ about.
 
Patrick Evans
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think i am making progress. The below suggestion threw an error about using a string where it should be an integer. I have since changed the code to:



I now get a null pointer. Can anyone shed some light on to why?

Thanks

Stable Library
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version = RXTX-2.1-7
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at EBIAlarm$1.jButton1ActionPerformed(EBIAlarm.java:109)
at EBIAlarm$1.actionPerformed(EBIAlarm.java:102)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!