-1

Trying to send an Intent from a fragment to an Activity but get stuck in there so.. Any clue would be great guys :)

In my fragment, I have this :

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.customer_detail, container, false);


        if (mItem != null) {
            ((TextView) rootView.findViewById(R.id.customer_hidden_id)).setText(String.valueOf(mItem.getCustomerId()));
            ((TextView) rootView.findViewById(R.id.customer_lastname)).setText(mItem.getLastName());
            ((TextView) rootView.findViewById(R.id.customer_firstname)).setText(mItem.getFirstName());
            ((TextView) rootView.findViewById(R.id.customer_address)).setText(mItem.getAddress());
            ((TextView) rootView.findViewById(R.id.customer_postcode)).setText(mItem.getPostCode());
            ((TextView) rootView.findViewById(R.id.customer_city)).setText(mItem.getCity());
            ((TextView) rootView.findViewById(R.id.customer_phone)).setText(mItem.getPhone());
            ((TextView) rootView.findViewById(R.id.customer_mail)).setText(mItem.getEmail());
            ((TextView) rootView.findViewById(R.id.customer_license)).setText(mItem.getLicenseNumber());

            Intent intent;
            intent = new Intent(getActivity(), CustomerDetailActivity.class );
            intent.putExtra("customer", mItem);
            getActivity().startActivity(intent);

        }

        return rootView;
    }

and in the activity I try to get the Intent :

public class CustomerDetailActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customer_detail);
        Toolbar toolbar = findViewById(R.id.detail_toolbar);
        setSupportActionBar(toolbar);

        Intent intent = getIntent();
        Customer currentCustomer = intent.getParcelableExtra("customer");
        Log.i("CUST", currentCustomer.toString());
}

But i get a nullPointerException so the Intent is not passed. If you guys know a way...

The LogCat error :

06-07 14:36:53.299 3224-3224/com.eni.goj.lokacar E/AndroidRuntime: FATAL EXCEPTION: main
                                                                   Process: com.eni.goj.lokacar, PID: 3224
                                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.eni.goj.lokacar/com.eni.goj.lokacar.Activities.CustomerDetailActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.eni.goj.lokacar.BO.Customer.toString()' on a null object reference
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
                                                                       at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                       at android.os.Looper.loop(Looper.java:164)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:6494)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
                                                                    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.eni.goj.lokacar.BO.Customer.toString()' on a null object reference
                                                                       at com.eni.goj.lokacar.Activities.CustomerDetailActivity.onCreate(CustomerDetailActivity.java:34)
                                                                       at android.app.Activity.performCreate(Activity.java:7009)
                                                                       at android.app.Activity.performCreate(Activity.java:7000)
                                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
                                                                       at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
                                                                       at android.os.Handler.dispatchMessage(Handler.java:106) 
                                                                       at android.os.Looper.loop(Looper.java:164) 
                                                                       at android.app.ActivityThread.main(ActivityThread.java:6494) 
                                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                                       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
j0w
  • 505
  • 2
  • 12
  • 32
  • use bundle you cant pass parseable object in intent – Milan Pansuriya Jun 07 '18 at 14:20
  • 1
    You don't even start your activity from your code... – Samuel Eminet Jun 07 '18 at 14:22
  • can you show the part of your code where you start the activity – Sree Jun 07 '18 at 14:22
  • What line causes the NullPointerException? – Code-Apprentice Jun 07 '18 at 14:33
  • In a fragment, you must do `getActivity().startActivity(intent);`. – Code-Apprentice Jun 07 '18 at 14:34
  • Sorry guys, erased the line by cleaning the code before posting my thread. just edited the post. ^^' – j0w Jun 07 '18 at 14:34
  • @Code-Apprentice it's the Log.i() line in the CustomerDetailActivity. Tried your way, still get the null pointer :D – j0w Jun 07 '18 at 14:39
  • So `currentCustomer` is `null` which means that `mItem` is `null`. I suggest that you use the debugger to help you figure out why. – Code-Apprentice Jun 07 '18 at 14:40
  • Or perhaps one of the members of your `Customer` class is `null` and the NPE is actually caused by some line in `Customer.toString()`. Why are you starting a new activity in the fragment's `onCreateView()` method? You can do the exact same thing by starting the activity directly without the fragment. – Code-Apprentice Jun 07 '18 at 14:44
  • After looking at your stack trace, the code you have shown cannot possibly cause the NPE because you have `if (mItem != null)` and then pass `mItem` in the Intent's bundle. The cause of the problem is somewhere else. You need to use the Android Studio debugger to track down the cause of the problem. If you need further help, please create a [mcve] that actually causes the error you are asking about. – Code-Apprentice Jun 07 '18 at 14:46
  • yeah, thought so too, but mItem is not null : I/CUST: Customer{customerId=3, lastName='BAILULE', firstName='JULIE', address='BLAAAA', city='BLA', postCode='12345', phone='0607080910', email='', licenseNumber='WWW123456XXX'} – j0w Jun 07 '18 at 14:49
  • Do you start this `CustomerDetailActivity` anywhere else? – Code-Apprentice Jun 07 '18 at 14:52
  • Have you implemented Parcelable interface in your model class – kundan kamal Jun 07 '18 at 14:55

2 Answers2

0
Intent intent;
intent = new Intent(getActivity(), CustomerDetailActivity.class);
intent.putExtra("customer", mItem);
getActivity().startActivity(intent);
Ajean
  • 5,528
  • 14
  • 46
  • 69
kundan kamal
  • 674
  • 7
  • 16
0

Your intent is being passed but the code that handles it is throwing the exception.

Customer currentCustomer = intent.getParcelableExtra("customer");
Log.i("CUST", currentCustomer.toString());

currentCustomer is null therefore trying to access its toString method thrown an exception.

What is a NullPointerException, and how do I fix it?

Kuffs
  • 35,581
  • 10
  • 79
  • 92