widget thread UDPsocket nullpointerexception

Put your problem here if it does not fit any of the other categories.

widget thread UDPsocket nullpointerexception

Postby pzoli » Mon Oct 19, 2009 7:39 am

I wrote a simple widget which uses the threads. In the thread I open a UDP socket server. OnUpdate method runs every 2 secounds and calls the getVar() method. The getVar method returns the received UDP datagram. While there is no high load on the phone everything is ok. When I start a other program and will high load then break my widget and get "java.lang.NullPointerException" message on logcat channel. What is wrong, why frozen? In onEnabled method is it allowd to start thrads?

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package secound.widget;
  3.  
  4.  
  5.  
  6. import java.lang.Thread.State;
  7.  
  8.  
  9.  
  10. import android.util.Log;
  11.  
  12. import android.widget.RemoteViews;
  13.  
  14. import android.content.ComponentName;
  15.  
  16. import android.appwidget.AppWidgetManager;
  17.  
  18. import android.appwidget.AppWidgetProvider;
  19.  
  20. import android.content.Context;
  21.  
  22.  
  23.  
  24. public class SecoundWidget extends AppWidgetProvider {
  25.  
  26.     private static volatile TCPServer cThread = null;
  27.  
  28.     private static volatile String classtext;
  29.  
  30.     private static String Tag = "logolout";
  31.  
  32.  
  33.  
  34.     @Override
  35.  
  36.     public void onEnabled(Context context){
  37.  
  38.         cThread = new TCPServer();
  39.  
  40.         cThread.start();
  41.  
  42.     }
  43.  
  44.  
  45.  
  46.      @Override
  47.  
  48.      public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){
  49.  
  50.  
  51.  
  52.         String message = "start";
  53.  
  54.         message=cThread.getVar();
  55.  
  56.  
  57.  
  58.         RemoteViews remoteViews;
  59.  
  60.         ComponentName thisWidget;
  61.  
  62.         remoteViews = new RemoteViews (context.getPackageName(),R.layout.main);
  63.  
  64.         thisWidget = new ComponentName(context,SecoundWidget.class);
  65.  
  66.  
  67.  
  68.         remoteViews.setTextViewText(R.id.widget_textview,message);
  69.  
  70.         appWidgetManager.updateAppWidget(thisWidget,remoteViews);        
  71.  
  72.         }
  73.  
  74. }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4



Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package secound.widget;
  2.  
  3.  
  4.  
  5. import java.net.DatagramPacket;
  6.  
  7. import java.net.DatagramSocket;
  8.  
  9. import android.util.Log;
  10.  
  11.  
  12.  
  13. public class TCPServer extends Thread {
  14.  
  15.         public static String Tag = "threadban";
  16.  
  17.         public volatile String userInput;
  18.  
  19.         public volatile String str;
  20.  
  21.  
  22.  
  23.         @Override
  24.  
  25.         public void run() {
  26.  
  27.                 try {
  28.  
  29.                         DatagramSocket ds = new DatagramSocket(9876);
  30.  
  31.                         byte buffer[] = new byte[6];
  32.  
  33.                         while (true) {
  34.  
  35.                                 DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
  36.  
  37.                                 ds.receive(dp);
  38.  
  39.                                 str = new String(dp.getData());
  40.  
  41.                                 Log.v(Tag,str);
  42.  
  43.                                 userInput = str;
  44.  
  45.                         }
  46.  
  47.  
  48.  
  49.                 } catch (Exception e) {
  50.  
  51.                         //String hiba = e.getMessage();
  52.  
  53.                 }
  54.  
  55.  
  56.  
  57.         }
  58.  
  59.  
  60.  
  61.         public String getVar() {
  62.  
  63.                 return userInput;
  64.  
  65.         }
  66.  
  67. }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
pzoli
Freshman
Freshman
 
Posts: 6
Joined: Mon Oct 19, 2009 6:42 am

Top

Postby qlimax » Tue Oct 20, 2009 5:09 pm

have you tryed to initialize the user input string in the thread class?


like this

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ...
  2.  
  3. public volatile String userInput="";
  4.  
  5. ...
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


maybe your widget calls getVar before the vm has called the run method, so the userInput string is null (when you start a thread, you tell the vm to call the run method - when there is cpu time...)

let me know....bye
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby pzoli » Tue Oct 20, 2009 5:48 pm

Yes, I tryed but nothing's changed. That initialize would help me if at the moment break the widget when I start the widget. I enable widget, run, run, run... and I start other program on the phone and then break my widget.

qlimax wrote:have you tryed to initialize the user input string in the thread class?


like this

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ...
  2. public volatile String userInput="";
  3. ...
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


maybe your widget calls getVar before the vm has called the run method, so the userInput string is null (when you start a thread, you tell the vm to call the run method - when there is cpu time...)

let me know....bye
pzoli
Freshman
Freshman
 
Posts: 6
Joined: Mon Oct 19, 2009 6:42 am

Postby qlimax » Tue Oct 20, 2009 6:14 pm

can you post the logcat error?
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby pzoli » Tue Oct 20, 2009 6:58 pm

Of couse!

qlimax wrote:can you post the logcat error?
Attachments
logcat.txt
nullpointerexception
(15.61 KiB) Downloaded 539 times
pzoli
Freshman
Freshman
 
Posts: 6
Joined: Mon Oct 19, 2009 6:42 am

Postby qlimax » Tue Oct 20, 2009 7:30 pm

from the logcat i don't uderstand if the thread is null (whel you call getVar) or the string returning from the getVar method is null....

you can check it with some ,not so elegant, if(...==null) Log.v(...)

or try catch block...
------------
I don't really understand why are you using the volatile keyword. I've never used this and I honestly don't understand the mean of the keyword...
have you tried to remove all this keywords?
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Top

Postby pzoli » Wed Oct 21, 2009 4:15 pm

The mistake is found!
If to use up the free memori then kill widget threads. (step by step for all widget). So died my UPDServer thread and can not to run "getVar()" method.

solving:
Change getVar to putVar method and the network thread (UDPServer) call the SecoundWidget.putVar() method.

qlimax wrote:from the logcat i don't uderstand if the thread is null (whel you call getVar) or the string returning from the getVar method is null....

you can check it with some ,not so elegant, if(...==null) Log.v(...)

or try catch block...
------------
I don't really understand why are you using the volatile keyword. I've never used this and I honestly don't understand the mean of the keyword...
have you tried to remove all this keywords?
pzoli
Freshman
Freshman
 
Posts: 6
Joined: Mon Oct 19, 2009 6:42 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 28 guests