1. Download our Official Android App: Forums for Android!

Apps URGENT Help NEEDED WITH App CODE!! : java.lang.NullPointerException

Discussion in 'Android Development' started by kumankit, Apr 26, 2012.

  1. kumankit

    kumankit Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    1
    Joined:
    Apr 26, 2012

    Apr 26, 2012
    1
    0
    5
    Hello everyone, I'm creating an app for chat client. This is my first app and i'm fairly new to java programming too. I am getting force close for this code. the logcat and the code are given below. kindly help me out. I relly cant figure this out myself.







    package com.drive;


    import android.app.Activity;
    import android.app.AlertDialog;
    import android.app.Dialog;
    import android.app.NotificationManager;
    import android.content.BroadcastReceiver;
    import android.content.ComponentName;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.content.ServiceConnection;
    import android.os.*;
    import android.os.Handler;
    import android.os.IBinder;
    import android.view.KeyEvent;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.View.OnKeyListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;

    import com.drive.interfaces.IAppManager;
    import com.drive.services.IMService;
    import com.drive.tools.FriendController;
    import com.drive.types.FriendInfo;

    public class Messaging extends Activity {

    private static final int MESSAGE_CANNOT_BE_SENT = 0;
    private EditText messageText;
    private EditText messageHistoryText;
    private Button sendMessageButton;
    private IAppManager imService;
    private FriendInfo friend = new FriendInfo();

    private ServiceConnection mConnection = new ServiceConnection() {

    public void onServiceConnected(ComponentName className, IBinder service) {
    imService = ((IMService.IMBinder)service).getService();
    }
    public void onServiceDisconnected(ComponentName className) {
    imService = null;
    Toast.makeText(com.drive.Messaging.this, R.string.local_service_stopped,
    Toast.LENGTH_SHORT).show();
    }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.messaging_screen); //messaging_screen);

    messageHistoryText = (EditText) findViewById(R.id.messageHistory);

    messageText = (EditText) findViewById(R.id.message);

    messageText.requestFocus();

    sendMessageButton = (Button) findViewById(R.id.sendMessageButton);

    Bundle extras = this.getIntent().getExtras();

    friend.userName = extras.getString(FriendInfo.USERNAME);
    friend.ip = extras.getString(FriendInfo.IP);
    friend.port = extras.getString(FriendInfo.PORT);
    String msg = extras.getString(FriendInfo.MESSAGE);


    setTitle("Messaging with " + friend.userName);


    // EditText friendUserName = (EditText) findViewById(R.id.friendUserName);
    // friendUserName.setText(friend.userName);

    if (msg != null)
    {
    this.appendToMessageHistory(friend.userName , msg);
    ((NotificationManager)getSystemService(NOTIFICATION_SERVICE)).cancel((friend.userName+msg).hashCode());
    }

    sendMessageButton.setOnClickListener(new OnClickListener(){
    CharSequence message;
    Handler handler = new Handler();
    public void onClick(View arg0) {
    message = messageText.getText();
    if (message.length()>0)
    {
    appendToMessageHistory(imService.getUsername(), message.toString());

    messageText.setText("");
    Thread thread = new Thread(){
    public void run() {
    if (!imService.sendMessage(friend.userName, message.toString()))
    {

    handler.post(new Runnable(){

    public void run() {
    showDialog(MESSAGE_CANNOT_BE_SENT);
    }

    });
    }
    }
    };
    thread.start();

    }

    }});

    messageText.setOnKeyListener(new OnKeyListener(){
    public boolean onKey(View v, int keyCode, KeyEvent event)
    {
    if (keyCode == 66){
    sendMessageButton.performClick();
    return true;
    }
    return false;
    }


    });

    }

    @Override
    protected Dialog onCreateDialog(int id) {
    int message = -1;
    switch (id)
    {
    case MESSAGE_CANNOT_BE_SENT:
    message = R.string.message_cannot_be_sent;
    break;
    }

    if (message == -1)
    {
    return null;
    }
    else
    {
    return new AlertDialog.Builder(Messaging.this)
    .setMessage(message)
    .setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
    /* User clicked OK so do some stuff */
    }
    })
    .create();
    }
    }

    @Override
    protected void onPause() {
    super.onPause();
    unregisterReceiver(messageReceiver);
    unbindService(mConnection);

    FriendController.setActiveFriend(null);

    }

    @Override
    protected void onResume()
    {
    super.onResume();
    bindService(new Intent(Messaging.this, IMService.class), mConnection , Context.BIND_AUTO_CREATE);

    IntentFilter i = new IntentFilter();
    i.addAction(IMService.TAKE_MESSAGE);

    registerReceiver(messageReceiver, i);

    FriendController.setActiveFriend(friend.userName);



    }


    public class MessageReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent)
    {
    Bundle extra = intent.getExtras();
    String username = extra.getString(FriendInfo.USERNAME);
    String message = extra.getString(FriendInfo.MESSAGE);

    if (username != null && message != null)
    {
    if (friend.userName.equals(username)) {
    appendToMessageHistory(username, message);
    }
    else {
    if (message.length() > 15) {
    message = message.substring(0, 15);
    }
    Toast.makeText(Messaging.this, username + " says '"+
    message + "'",
    Toast.LENGTH_SHORT).show();
    }
    }
    }

    };
    private MessageReceiver messageReceiver = new MessageReceiver();

    private void appendToMessageHistory(String username, String message) {
    if (username != null && message != null) {
    messageHistoryText.append(username + ":\n");
    messageHistoryText.append(message + "\n");
    }
    }





    }





    04-27 02:06:40.870: E/AndroidRuntime(222): Uncaught handler: thread main exiting due to uncaught exception
    04-27 02:06:40.901: E/AndroidRuntime(222): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.drive/com.drive.Messaging}: java.lang.NullPointerException
    04-27 02:06:40.901: E/AndroidRuntime(222): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
    04-27 02:06:40.901: E/AndroidRuntime(222): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
    04-27 02:06:40.901: E/AndroidRuntime(222): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
    04-27 02:06:40.901: E/AndroidRuntime(222): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
    04-27 02:06:40.901: E/AndroidRuntime(222): at android.os.Handler.dispatchMessage(Handler.java:99)
    04-27 02:06:40.901: E/AndroidRuntime(222): at android.os.Looper.loop(Looper.java:123)
    04-27 02:06:40.901: E/AndroidRuntime(222): at android.app.ActivityThread.main(ActivityThread.java:4363)
    04-27 02:06:40.901: E/AndroidRuntime(222): at java.lang.reflect.Method.invokeNative(Native Method)
    04-27 02:06:40.901: E/AndroidRuntime(222): at java.lang.reflect.Method.invoke(Method.java:521)
    04-27 02:06:40.901: E/AndroidRuntime(222): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
    04-27 02:06:40.901: E/AndroidRuntime(222): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
    04-27 02:06:40.901: E/AndroidRuntime(222): at dalvik.system.NativeStart.main(Native Method)
    04-27 02:06:40.901: E/AndroidRuntime(222): Caused by: java.lang.NullPointerException
    04-27 02:06:40.901: E/AndroidRuntime(222): at com.drive.Messaging.onCreate(Messaging.java:70)
    04-27 02:06:40.901: E/AndroidRuntime(222): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    04-27 02:06:40.901: E/AndroidRuntime(222): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
    04-27 02:06:40.901: E/AndroidRuntime(222): ... 11 more
    04-27 02:06:40.930: E/dalvikvm(222): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
     

    Advertisement

  2. Unforgiven

    Unforgiven OK Google
    Moderator
    Rank:
     #1
    Points:
    4,238
    Posts:
    36,168
    Joined:
    Jun 23, 2010

    Jun 23, 2010
    36,168
    42,007
    4,238
    Male
    Douglas, MA
    I've moved this to our applications development forum so you can get some better eyeballs on your question.:)
     
    kumankit likes this.
  3. Rukbat

    Rukbat Android Expert
    Rank:
    None
    Points:
    843
    Posts:
    10,392
    Joined:
    Jan 16, 2012

    Jan 16, 2012
    10,392
    2,894
    843
    Male
    Being retired and writing Tasker tasks.
    Fayetteville, NC, USA
    NullPointerException usually means that you created a variable, failed to initialize it to any value, then used itas a pointer. You can't do that.
     
  4. LolaKima

    LolaKima Lurker
    Rank:
    None
    Points:
    5
    Posts:
    1
    Joined:
    Jun 19, 2012

    Jun 19, 2012
    1
    0
    5
    Please if you have found a solution for this problem please share it since I have the same problem and I can not fix it.
    thank you in advance
     
  5. uwe

    uwe Member
    Rank:
    None
    Points:
    38
    Posts:
    45
    Joined:
    Feb 22, 2010

    Feb 22, 2010
    45
    19
    38
    Switzerland
    check if extras is null in onCreate
     

Share This Page

Loading...