1
package com.example.praduman.humlog;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Parcelable;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import com.parse.Parse;
import java.io.Serializable;


public class HomeActivity extends ActionBarActivity {

    private HumLogController humLogController;
    private Intent logInActivityIntent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        humLogController = new HumLogController();   

        logInActivityIntent = new Intent(this , LogInActivity.class);
        logInActivityIntent.putExtra("controllerObject", humLogController);
        startActivity(logInActivityIntent);
}
}

I am passing instance of humLogController with the intent to the LogInActivity, when I call the method createNewUser() with the object passed, the application crashes. Everything else works fine.

package com.example.praduman.humlog;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.Toast;


public class LogInActivity extends ActionBarActivity {

    private Button signUpButton;
    private Button logInButton;
    private Intent signUpChoiceIntent;
    private HumLogController humLogController;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log_in);
        humLogController = (HumLogController)getIntent().getSerializableExtra("controllerObject");
        humLogController.createNewUser();
        setIntentAndButton();
    }

    private void setIntentAndButton(){
        signUpChoiceIntent = new Intent (this , SignUpChoiceActivity.class);
        signUpChoiceIntent.putExtra("controllerObject", humLogController);
        logInButton = (Button) findViewById(R.id.logInSignInButton);
        signUpButton= (Button) findViewById(R.id.logInSignUpButton);
        setActionListeners();
    }

    private void setActionListeners(){
        logInButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(LogInActivity.this, "hope this will work" , Toast.LENGTH_LONG).show();
            }
        });

        signUpButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(signUpChoiceIntent);
            }
        });
    }

}

This is the controller class which implements Serializable, other than createNewUser(), if I call one of the variable like username or password by making them public. It works but when I call that method it crashes. And yes, there is no problem with humLogModel, I tested that.

package com.example.praduman.humlog;

import java.io.Serializable;

/**
 * Created by Praduman on 23/07/2015.
 */
public class HumLogController implements Serializable{
    private transient HumLogModel humLogModel;
    private transient String username;
    private transient String password;
    public transient String userType;

    public HumLogController() {
        this.humLogModel = new HumLogModel();
    }
    public void createNewUser(){
        humLogModel.createNewUser("TestUser4", "1234");
    }
}

The log cat is ...

07-26 15:07:23.785    8885-8885/? D/dalvikvm﹕ Late-enabling CheckJNI
07-26 15:07:23.805    8885-8891/? D/dalvikvm﹕ Debugger has detached; object registry had 1 entries
07-26 15:07:23.905    8885-8888/? D/dalvikvm﹕ GC_CONCURRENT freed 201K, 3% free 8981K/9212K, paused 2ms+0ms, total 7ms
07-26 15:07:23.909    8885-8900/? I/dalvikvm﹕ Could not find method android.database.Cursor.getNotificationUri, referenced from method com.parse.ParseSQLiteCursor.getNotificationUri
07-26 15:07:23.909    8885-8900/? W/dalvikvm﹕ VFY: unable to resolve interface method 489: Landroid/database/Cursor;.getNotificationUri ()Landroid/net/Uri;
07-26 15:07:23.909    8885-8900/? D/dalvikvm﹕ VFY: replacing opcode 0x72 at 0x0002
07-26 15:07:23.913    8885-8885/? I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
07-26 15:07:23.913    8885-8885/? W/dalvikvm﹕ VFY: unable to resolve virtual method 441: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
07-26 15:07:23.913    8885-8885/? D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
07-26 15:07:23.921    8885-8885/? I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
07-26 15:07:23.921    8885-8885/? W/dalvikvm﹕ VFY: unable to resolve virtual method 463: Landroid/content/res/TypedArray;.getType (I)I
07-26 15:07:23.921    8885-8885/? D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
07-26 15:07:23.925    8885-8885/? I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawable
07-26 15:07:23.925    8885-8885/? W/dalvikvm﹕ VFY: unable to resolve virtual method 404: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
07-26 15:07:23.929    8885-8885/? D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
07-26 15:07:23.929    8885-8885/? I/dalvikvm﹕ Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawableForDensity
07-26 15:07:23.929    8885-8885/? W/dalvikvm﹕ VFY: unable to resolve virtual method 406: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
07-26 15:07:23.929    8885-8885/? D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
07-26 15:07:23.997    8885-8888/? D/dalvikvm﹕ GC_CONCURRENT freed 268K, 4% free 9101K/9396K, paused 1ms+2ms, total 8ms
07-26 15:07:24.057    8885-8885/? D/AndroidRuntime﹕ Shutting down VM
07-26 15:07:24.057    8885-8885/? W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4c3e648)
07-26 15:07:24.057    8885-8885/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.praduman.humlog/com.example.praduman.humlog.LogInActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example.praduman.humlog.HumLogController.createNewUser(HumLogController.java:18)
            at com.example.praduman.humlog.LogInActivity.onCreate(LogInActivity.java:23)
            at android.app.Activity.performCreate(Activity.java:5133)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

1 Answer 1

1

You marked humLogModel as transient, so it will not be serialized and will be null in the copy made in your new activity.

Note that your constructor will not be used for the de-serialized copy:

The deserialization process does not use the object's constructor - the object is instantiated without a constructor and initialized using the serialized instance data.

Hence, since you are not serializing humLogModel, and your constructor does not get a chance to populate humLogModel, humLogModel is null when you go to use it.

2
  • Can you tell me how to deserialize the humLogModel , because I will need to use it with in the HumLogControlle class. Or I have to serialize the Model class. Jul 26, 2015 at 19:44
  • Thank you Thank you thank you Its working , thank you Jul 26, 2015 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.