aspose file tools*
The moose likes Android and the fly likes Null pointer exception probably due to synchronisation issues; problem in starting activity Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "Null pointer exception probably due to synchronisation issues; problem in starting activity" Watch "Null pointer exception probably due to synchronisation issues; problem in starting activity" New topic
Author

Null pointer exception probably due to synchronisation issues; problem in starting activity

Madhu Nandan
Ranch Hand

Joined: Dec 16, 2010
Posts: 52

Hello,

I have a small app which is the client running on Android emulator. Server (small Java app in Eclipse) keeps sending strings. Client will receive these strings and display an image if a string in the correct format and right contents is received.

I send two strings-one with wrong content and the other with correct content. Both strings are received. For the string with wrong content, the appropriate message is shown. But when the string with correct content is receive, an image is supposed to be shown. But this doesn't happen. I get a warning "Activity pause timeout for HistoryRecord" and then error.

Code and LogCat below.

Please help me with this.

Thank you for time spent and efforts put on this.

Cheers,
Madhu

Main activity




Interface




AsyncTask




[Thumbnail for LogCat.jpg]

Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6



As per your error log, the exception is occurring in the above line, i.e. NumberFormatException. As per the log its probably trying to parse a space character. Do a log trace to find out what value are you getting in actVal.


Swastik
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

There is NullPointerException caused at line 40 of TCPListener.java

what is at line 40?


Life is easy because we write the source code.....
Madhu Nandan
Ranch Hand

Joined: Dec 16, 2010
Posts: 52

Swastik Dey wrote:

As per your error log, the exception is occurring in the above line, i.e. NumberFormatException. As per the log its probably trying to parse a space character. Do a log trace to find out what value are you getting in actVal.


Thank you for the response.

I think what you mentioned is not a problem because I display the message name and value - msgValue = Integer.parseInt(actVal), and I get it correctly. It is there in the LogCat as well. C: shiftDirection (msgName), 1.0 (msgValue).

I am trying to receive messages in a loop (100 times), appending them to an ArrayList and passing the contents of ArrayList one by one. As a start, I send two messages in this order: 1) <MSG><N>shiftDirection<!N><V>0<!V><!MSG> and
2) <MSG><N>shiftDirection<!N><V>1<!V><!MSG>

The first message should not do anything and should just display a message-"Just show an image" in LogCat. When the second message is received, an actual image should be shown. Earlier, if I sent just one message (2nd), I was able to show an image in the emulator. Now this does not happen. I get activity pause timeout and the following error as shown in LogCat.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

Madhu

The value you have mentioned is 1.0, not 1 i.e not an int. So you can't use Integer.parseInt on it. Either use Double.parseDouble or Float.parseFloat.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

Just look at the error log you have mentioned it displays
java.lang.NumberFormatException

I still doubt in some condition it's getting a space character as your error log says that.
Madhu Nandan
Ranch Hand

Joined: Dec 16, 2010
Posts: 52

Swastik Dey wrote:Just look at the error log you have mentioned it displays
java.lang.NumberFormatException

I still doubt in some condition it's getting a space character as your error log says that.


I tried Float.parseFloat. Still it does not work. In the LogCat, I can only find java.lang.NullPointerException. I could not find java.lang.NumberFormatException.

I guess there is some problem in one of the loops where I try to read the messages from the server or where I try to pass the contents of ArrayList one by one to the main activity.
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

what is at line 40?
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

Sorry, I was referring to the older log.
Madhu Nandan
Ranch Hand

Joined: Dec 16, 2010
Posts: 52

Pramod P Deore wrote:what is at line 40?


From line 40 starts this parser:



Code continued. LogCat points to this line as well.



Code continued.




I guess the parser is throwing the null pointer exception. It was working fine earlier.
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

print the value of tempStr, at the start of callCompleted(String tempStr)
{
System.out.println ("tempStr is"+tempStr);
}
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

From your code what looks to me the server is returning some name and value. So instead of returning the message like that, why not make it simpler

<msg name='a' value='1.0'/>
or may be
<msg>
<name>a</name>
<value>1.0</value>
</msg>

and now use some standard parsers like Jdom or Sax.
Madhu Nandan
Ranch Hand

Joined: Dec 16, 2010
Posts: 52

Pramod P Deore wrote:print the value of tempStr, at the start of callCompleted(String tempStr)
{
System.out.println ("tempStr is"+tempStr);
}


I did that. I get the correct contents. source or tempStr has <MSG><N>shiftDirection<!N><V>1<!V><!MSG>

The LogCat shows error in this line:

Matcher matcher = pattern.matcher(source);

Just an outside question: Is there a way to display the contents of LogCat directly in this forum (or maybe other help forums like StackOverflow) without taking a print screen and attaching the image?
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

Just an outside question: Is there a way to display the contents of LogCat directly in this forum (or maybe other help forums like StackOverflow) without taking a print screen and attaching the image?

Copy it from logcat and print it like code you have printed above.
Madhu Nandan
Ranch Hand

Joined: Dec 16, 2010
Posts: 52

Swastik Dey wrote:From your code what looks to me the server is returning some name and value. So instead of returning the message like that, why not make it simpler

<msg name='a' value='1.0'/>
or may be
<msg>
<name>a</name>
<value>1.0</value>
</msg>

and now use some standard parsers like Jdom or Sax.


The parser receives messages in the correct format: <MSG><N>shiftDirection<!N><V>1<!V><!MSG>

The output of the parser is also fine. Message name: shiftDirection, message value: 1. So won't this simple parser suffice? Or kindly tell me if you see any further problems with this.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

I am not saying what you are getting is wrong. But it's not following a valid xml structure. If you use a proper xml structure the parsing becomes much easier using the standard java parsers.
Madhu Nandan
Ranch Hand

Joined: Dec 16, 2010
Posts: 52

Pramod P Deore wrote:
Just an outside question: Is there a way to display the contents of LogCat directly in this forum (or maybe other help forums like StackOverflow) without taking a print screen and attaching the image?

Copy it from logcat and print it like code you have printed above.


The last LogCat looks like this. I send 2 messages. I see that both are received and parsed. When the 2nd message is received, it should show an image. It is trying to start an activity (to show an image) but the activity pause timeout is happening; another message, actually null, is received and I get the error.

Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

What code do you have in line number 54 in TCPListen.java?
Madhu Nandan
Ranch Hand

Joined: Dec 16, 2010
Posts: 52

Swastik Dey wrote:What code do you have in line number 54 in TCPListen.java?


From line 40 starts parser. Line 54: Pattern pattern = Pattern.compile(find1);

Parser code below. But I just found this. I send 3 messages-1st in wrong format, 2nd with wrong value and 3rd in correct format and correct value. First time, all 3 messages were sent and received; information about 1st and 2nd was shown correctly. And for the 3rd message, image should have been shown. But it does not happen and I get the error. When I try the same thing again, only first 2 messages are received and the 3rd message is not received. This happens randomly. Sometimes all 3 messages are received and sometimes only 2.

public void callCompleted(String tempStr){
String source = tempStr;

//mTitle.setText(source);
if(source.matches("<MSG><N>.*<!N><V>.*<!V><!MSG>")) {
Log.d("TCP", "C: In the parser" + " " + source);
//mTitle.setText(source1);
//tempStr = "";
//String source = "<MSG><N>shiftDirection<!N><V>1<!V><!MSG>";

// Match characters lying before message name
String find1 = "<MSG><N>";
// Match characters lying before message value
String find2 = ".*<!N><V>";
// Mask the characters by replacing them with blank space
String replace = "";

// Mask the characters lying before message name
Pattern pattern = Pattern.compile(find1); //line 54
Matcher matcher = pattern.matcher(source);
String output = matcher.replaceAll(replace);

// Split the message and extract message name
String name1[] = output.split("<!N>");
String msgName = name1[0];

//Log.i(TAG, "Message name is" + msgName);

//Mask the characters lying before message value
Pattern pattern1 = Pattern.compile(find2);
Matcher matcher1 = pattern1.matcher(output);
String output1 = matcher1.replaceAll(replace);

//Split the remaining message and extract message value
String val1[] = output1.split("<!V>");
String actVal = val1[0];

//Log.d("TCP", msgName);
//Pass the message name and value for further action
float msgValue = Float.parseFloat(actVal);
actionOnData(msgName, msgValue);
} else Log.d("TCP", "C:Wrong format message" + " " + source);
}


LogCat:

Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

Sorry to bother you, but I will still stick to my point that if you maintain a valid xml structure parsing becomes much easier. Just look at the following example below. The change what I made is instead of !, I am using a front slash.

Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

One more point, what's the use of using a loop in postExecute. Why not change the method signature of the listener interface and pass the entire ArrayList, in this case it will decrease the number of method calls as well.
Madhu Nandan
Ranch Hand

Joined: Dec 16, 2010
Posts: 52

Swastik Dey wrote:One more point, what's the use of using a loop in postExecute. Why not change the method signature of the listener interface and pass the entire ArrayList, in this case it will decrease the number of method calls as well.


Thank you for this suggestion. I did this and now the code runs faster. Now I get the same error but faster

And the strange behaviour of receiving only 2 messages sometimes and sometimes all 3 messages continues.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

Probably you can make one more change as well. I am not getting the use of the loop in doInBackground(),even if the server returns one message loop continues till 100. You may follow the following process.
1. doInBackground message gets one message from server
2. return the message
3. onPostExecute is fired with the String response(not ArrayList)
4. invoke callCompleted with this String.
Madhu Nandan
Ranch Hand

Joined: Dec 16, 2010
Posts: 52

Swastik Dey wrote:Probably you can make one more change as well. I am not getting the use of the loop in doInBackground(),even if the server returns one message loop continues till 100. You may follow the following process.
1. doInBackground message gets one message from server
2. return the message
3. onPostExecute is fired with the String response(not ArrayList)
4. invoke callCompleted with this String.


If it is a single string, this works. I have tried it earlier as well. The real situation is that this client has to keep receiving messages and based on individual messages and their corresponding values, I have to do different things like show an image or some message etc. That is why I thought using ArrayList could be better so that I can keep adding the messages as they come and pass the ArrayList to callCompleted. Do you think this way is good? If you have any other way in mind, it would be helpful if you can suggest it.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

Ok, so if the client is supposed to receive n number of messages, even that for loop can't be a proper solution. I assume that you are not going to receive all (n) messages at a time, rather at a specific time interval. If this is the case why not use Timertask to schedule this activity. Start a time task, which runs at a specific time interval and executes the asynctask. Some message arrives you show the user the proper notification.
Madhu Nandan
Ranch Hand

Joined: Dec 16, 2010
Posts: 52

Swastik Dey wrote:Ok, so if the client is supposed to receive n number of messages, even that for loop can't be a proper solution. I assume that you are not going to receive all (n) messages at a time, rather at a specific time interval. If this is the case why not use Timertask to schedule this activity. Start a time task, which runs at a specific time interval and executes the asynctask. Some message arrives you show the user the proper notification.


I can receive any message out of the n messages at any time. There is no specific time for receiving the messages. The messages are CAN messages being sent by the car PC (an application in the car PC). These can be received anytime while the car is being driven. So I have to keep the client running continuously and whenever there is a message, do some action based on the message.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

Ok, even if the message comes at any time, its at least going to take one second if not more. So what's wrong in scheduling the task, which checks for a new message after every one second, and of course the client app is running.
Madhu Nandan
Ranch Hand

Joined: Dec 16, 2010
Posts: 52

Swastik Dey wrote:Ok, even if the message comes at any time, its at least going to take one second if not more. So what's wrong in scheduling the task, which checks for a new message after every one second, and of course the client app is running.


Do you mean to say run the client code (TCPListen) every one second? Or does it mean just run the TCPServiceHandler (AsyncTask) every one second? Sorry if I am wrong. I am quite new to these. It would be great if you could explain a bit more.

And in the current situation where I run background part in AsyncTask in a for loop, which is expensive or time consuming, will the problem of not showing the image when the correct message is received get solved if I go for TimerTask?
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

I am asking you to schedule at the client end. In stead of starting the asynctask from main activity start it from the scheduler. And process it just using a String no ArrayList.


Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

I forgot to mention one more point, in this case you don't need any loop in doInBackground.
Madhu Nandan
Ranch Hand

Joined: Dec 16, 2010
Posts: 52

Swastik Dey wrote:I am asking you to schedule at the client end. In stead of starting the asynctask from main activity start it from the scheduler. And process it just using a String no ArrayList.





To check after every one second, why have you given 1*60*1000? I though it should be just 1000 (ms).
_timer.scheduleAtFixedRate(mc, 0, 1*60*1000);

Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

Yes you are right, I considered to be one minutes. Sorry about that.
Madhu Nandan
Ranch Hand

Joined: Dec 16, 2010
Posts: 52

Swastik Dey wrote:Yes you are right, I considered to be one minutes. Sorry about that.


What is the TextView for?
_tvTest=(TextView)this.findViewById(R.id.TextView01);
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

I had used this just to test the server response.
Madhu Nandan
Ranch Hand

Joined: Dec 16, 2010
Posts: 52

Swastik Dey wrote:I had used this just to test the server response.


I tried this but got the following error.



Main activity:



TimerTask (MessageConsumer)



Listener


AsyncTask (TcpServiceHandler)

Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

You have called setContentView twice. Remove the first call.
Madhu Nandan
Ranch Hand

Joined: Dec 16, 2010
Posts: 52

Swastik Dey wrote:You have called setContentView twice. Remove the first call.


Oh, I had overlooked it. Now I corrected that. But there is another error in timer task-cannot create handler.

Madhu Nandan
Ranch Hand

Joined: Dec 16, 2010
Posts: 52

Madhu Nandan wrote:
Swastik Dey wrote:You have called setContentView twice. Remove the first call.


Oh, I had overlooked it. Now I corrected that. But there is another error in timer task-cannot create handler.



I changed main activity and timer task like below and this helped in overcoming the error.

Main activity: started the handler for TcpServiceHandler (AsyncTask) here without IP address.

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);

// Set up the window layout
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);

// Set up the custom title
mTitle = (TextView) findViewById(R.id.title_left_text);
mTitle.setText(R.string.app_name);
mTitle = (TextView) findViewById(R.id.title_right_text);

TcpServiceHandler handler = new TcpServiceHandler(this);
handler.execute("");

_timer=new Timer();
MessageConsumer mc = new MessageConsumer(this);
//check after every one second
_timer.scheduleAtFixedRate(mc, 0, 1000);
}


Timer task (MessageConsumer): added IP address to the handler for AsyncTask



But now this works only for one message. The client keeps running but receives only one message and crashes if there are further messages. The LogCat points to the starting of parser in main activity. Now I should try the standard parser (SAX/DOM) as suggested by you.

Madhu Nandan
Ranch Hand

Joined: Dec 16, 2010
Posts: 52

Swastik Dey wrote:You have called setContentView twice. Remove the first call.


I tried with the standard parser.



The test server I am using is this:



In this case, the client keeps running, receives only the first message sent and continues to run but does not receive the second message sent by the server. I send the second message 5 seconds after the first message is sent. If you can see why see this is happening, please point it out to me.

Madhu
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

Make these two changes and see if you get any difference


 
jQuery in Action, 2nd edition
 
subject: Null pointer exception probably due to synchronisation issues; problem in starting activity