0

I've seen various methods to split a string. I've tryed with both from this post.

I'm trying to read and split the next string: {2b 00 00}

I see that the most common case is to split a message separated by ":", but in this case, my message is separated by spaces.

Trying both ways, with the regular split() function or with the StringTokenizer I'm getting a "nullpointerexception" that I supose that is cause because of the space:

private String splitReceivedString(String s) {
    String[] separated = s.split(" ");
    return separated[1];
}

How could I get the values of this kind of string?

ADDED CODE WITH THE POSSIBLE PROBLEM

After checking some of your answers, I do realize that the problem comes from the bluetooth inputstream. I'm getting null values from it. So, here is the code that I'm using to receive the messages:

The code is almost the same than the bluetoothChat example. But it is modified to adapt to my program, so may I have something wrong.

I've got an MCU wich returns me this String {2b 00 00} when I send to it another String. I think that this is done in the connectedThread:

public class ConnectedThread extends Thread {

   public void run() {
    byte[] buffer = new byte[1024];  // buffer store for the stream
    int bytes; // bytes returned from read()

    /**Keep listening to the InputStream until an exception occurs*/
    while (true) {
        try {
            /**Read from the InputStream*/
            bytes = GlobalVar.mmInStream.read(buffer);

            /**Send the obtained bytes to the UI activity*/
            GlobalVar.mHandler.obtainMessage(GlobalVar.MESSAGE_READ, bytes, -1, buffer).sendToTarget();

So, this is sending me the string to the handler function in the main activity:

    public final Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case GlobalVar.MESSAGE_STATE_CHANGE:
                //The code here is irelevant
            case  GlobalVar.MESSAGE_WRITE:
                byte[] writeBuf = (byte[]) msg.obj;
                /**construct a string from the buffer*/
                String writeMessage = new String(writeBuf);
                GlobalVar.mCommunicationArrayAdapter.add(writeMessage);
                break;
            case  GlobalVar.MESSAGE_READ:
                byte[] readBuf = (byte[]) msg.obj;
                /**construct a string from the valid bytes in the buffer*/
                String readMessage = new String(readBuf);
                GlobalVar.mCommunicationArrayAdapter.add(readMessage);
                GlobalVar.readString = readMessage;                    
                break;

Then, the variable GlobalVar.readString is the one that I'm getting in the split function:

    private String splitReceivedString (String s) {

        String[] separated = s.split(" ");
        return separated[1];
    }

    receive1 = splitReceivedString (GlobalVar.readString);

So, the thing is that it isn't reading right the received string, and I don't know how to fix it.

Community
  • 1
  • 1
masmic
  • 3,526
  • 11
  • 52
  • 105

4 Answers4

1

If you get a NullPointerException this cannot be due to the string " " you pass to the split function - that is definitely not null. It seems that the String s that you get as a parameter of your splitReceivedString method is null for any reason.

chiccodoro
  • 14,407
  • 19
  • 87
  • 130
  • Well, yes, it could be. I'm developing a bluetooth app based on the bluetoothChat exmample. The string that I'm inserting there, is a string that I receive via bluetooth, so I get it from the variable `readString` from the handler. I tought that it was right 'cause when making a toast to this variable, I see in the app the string `2b 00 00`. So, what I could do? – masmic Aug 07 '13 at 15:02
  • its because splitting the string using .split(" ") wont return anything since " " is not correct regualr expression syntax which is required for the .split method. Thus the null pointer. – KBusc Aug 07 '13 at 15:08
  • @KBusc: split never returns `null`. See here, too: http://stackoverflow.com/a/6902125/55787 – chiccodoro Aug 08 '13 at 07:15
  • @chiccodoro, Added code in the main question explaining which is the problem. – masmic Aug 08 '13 at 08:34
0

Insert some output (System.out.println or other) to debug

  • what is your s

  • how many elements does separated have

  • what is separated[0] (if you only have one element)

Try to use

s.split("\\s");

to split at any whitespace.

koljaTM
  • 10,064
  • 2
  • 40
  • 42
0

I tested your code in a console application. I changed the double quotes to single quotes and there was a spelling mistake oin the method "Split"

    static void Main(string[] args)
    {
        splitReceivedString("2b 00 00"); //1
    }

    public static string splitReceivedString(String s)
    {
        string[] separated = s.Split(' '); //2
        return separated[1];
    }
beaumondo
  • 4,862
  • 7
  • 29
  • 42
0

You want to use a regular expression to split the string. Try like so

String splitString = "2b 00 00";
String delim = "[ ]";
//here we split the string wherever we see a character matching
//the inside of our square brackets. In this case a space
String[] splitResults = splitString.split(delim);

I have tested this and it works giving the following results

splitResults[0] = "2b"
splitResults[1] = "00"  
splitResults[2] = "00"
KBusc
  • 663
  • 8
  • 24
  • nothing neither this way. I think that I'm gettin this because of the `readString ` variable from the bluetooth's imputstream. – masmic Aug 07 '13 at 15:11
  • Added code in the main question explaining which is the problem. – masmic Aug 08 '13 at 08:36