0

I want to use the value of 2 editTexts from one activity in another. Here is my code so far. I am getting:

java.lang.NullPointerException.

The Code:

public class AddJob extends AppCompatActivity{
    // vars
    private BottomNavigationView bottomNavigationView;
    private EditText editTextLat, editTextLng;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_job);

        TextView textView = findViewById(R.id.activityTitleAddJob);
        textView.setText("Add a Job");

        editTextLat = findViewById(R.id.editTextLat);
        editTextLng = findViewById(R.id.editTextLng);
    }

    public int getLatitude() {
        return new Integer(editTextLat.getText().toString());
    }

    public int getLongitude() {
        return new Integer(editTextLng.getText().toString());
    }
}

The Stack Trace:

enter image description here

Here is the code snippet from the map class:

AddJob aj = new AddJob();
int lat = aj.getLatitude();
int lng = aj.getLongitude();
Toast.makeText(aj, lat + " " + lng, Toast.LENGTH_LONG).show();
Raj
  • 2,797
  • 2
  • 9
  • 25
  • Can we have the stacktrace – Crammeur Jul 22 '18 at 20:30
  • provide stack trace, we are not wizards – lord_hokage Jul 22 '18 at 20:35
  • I added the stack trace –  Jul 22 '18 at 20:57
  • Is because you call getLatitude() and editTextLat is null. – Crammeur Jul 22 '18 at 21:06
  • Please don't post screenshots of code, XML, or logcat output. Please post all text as text. Furthermore, the trace suggests that you have another `Activity` named `map`. You'll want to post the relevant code from that, as well. You appear to be directly instantiating `AddJob` yourself, which you should never do. This would explain the `NullPointerException`. – Mike M. Jul 22 '18 at 21:06
  • How would I make editTextLat not null. I tried setting the text in onCreate(). –  Jul 22 '18 at 21:09
  • [Also don't post links to code or stack traces. I've inserted the image into the post because It is important to keep all the info in the post to help folks who run into the same problem, but please supply text next time as @MikeM requested] – Elletlar Jul 22 '18 at 21:12

5 Answers5

2

Please read about the Activity lifecycle. You should never create an Activity directly with

new MyActivity()

This will not launch any of the lifecycle events (onCreate, etc...) or bind it to a context, set a view hierarchy on it, or do any of the regular Activity things you may be expecting. Your program is returning null because onCreate is never called on the activity, and if you were to simply try to call it yourself it would likely crash.

If you want data from one activity to be available in another activity, an easy way to achieve this is to save the data in SharedPreferences in the AddJob activity (whenever the values are updated) and access it in MapActivity from the SharedPreferences. You can also pass the data from one Activity to the next by adding data to the Intent when you launch it.

One advantage to using SharedPreferences here is that the user's choices will be saved from one app session to the next, and if you have multiple things that can launch the MapActivity they don't have to keep passing that data to it.

Sachin
  • 3,815
  • 2
  • 13
  • 26
Tyler V
  • 3,246
  • 2
  • 13
  • 34
1

hello would it not be better using an Intent

Intent i = new Intent(MainActivity.this, NEXTActivity.class);
            i.putExtra("latitude", lat);
            i.putExtra("longitude", lng);
            startActivity(i);

NEXTActivity

 Bundle extras = getIntent().getExtras();
    double latitude = extras.getDouble("latitude");
    double longitude = extras.getDouble("longitude");
NoobAndroid
  • 38
  • 1
  • 6
  • I added your code into my activities, but I got a java.lang.NullPointerException. It happened over here: double latitude = getIntent().getExtras().getDouble("latitude"); –  Jul 22 '18 at 22:29
  • have you made variables for lat and lng `public String lat; public String lng; ` and also got the information from the edit text for each one. – NoobAndroid Jul 22 '18 at 22:50
1

You shouldn't create activity objects, you can start activity with Intent and pass data through it. Check out the relevant answer in the link below:

https://stackoverflow.com/a/2091482/10116426

Ahmet Zorer
  • 102
  • 8
0

It seems that you are instantiating the AddJob class. This may raise problems on the back-stack of android, which may create lifecycle management problems. So, as mentioned by @NoobAndroid, it is better to use the recommended way not to run into unexpected errors.

Aman
  • 1,335
  • 8
  • 13
0
use this code .

package com.example.cloudanalogy.introscreen;

import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private BottomNavigationView bottomNavigationView;
    private EditText editTextLat, editTextLng;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
                TextView textView = findViewById(R.id.activityTitleAddJob);
                textView.setText("Add a Job");
                editTextLat = findViewById(R.id.editTextLat);
                editTextLng = findViewById(R.id.editTextLng);
        }

            public int getLatitude() {
                return Integer.parseInt(editTextLat.getText().toString());
            }

            public int getLongitude() {
                return Integer.parseInt(editTextLng.getText().toString());
            }

    public void onclick(View view) {

        int lat = getLatitude();
        int lng = getLongitude();
        Toast.makeText(this, ""+lat+" and "+lng, Toast.LENGTH_SHORT).show();

    }
}