Why Getapplicationcontext() in Constructor of Activity Throws Null Pointer Exception

Why getApplicationContext() in constructor of Activity throws null pointer exception?

Wait for the end of onCreate to call this method.

public class MainActivity extends BaseActivity { 

public onCreate(Bundle savedInstanceState) {
super(savedInstanceState);
getApplicationContext(); //activity has a context now
}
}

Why do I get NPE when I invoke android.content.Context.getApplicationContext() within Application's constructor?

How do the above application snippet is something different than the below?

Student has no superclass (other than Object, the default). You are referencing fields that you defined yourself on Student.

Application has superclasses, as you can see in the documentation. So, let's add a superclass to your Student:

public class Base {
String something;

public void onCreate() {
something = "a value";
}
}

public class Student extends Base {
int age;
String name;
String badgeid;
int value;

public Student(int age, String name) {
this.age = age;
this.name = name;
this.badgeid = this.age + this.name;
this.value = something.length;
}
}

Here, you will crash with a NullPointerException, because something is null at the time that you try calling something.length. The superclass does not set something to a value until onCreate() is called. You are referencing something too early.

Similarly, you cannot call methods exposed by Context until the appropriate time. In the case of Application, Activity, and Service, that is after your onCreate() calls super.onCreate().

NullPointerException for getApplicationContext() in Delegate Class

This is very simple.

You can get context instance after called OnCreate

private AppUpdateHelper appUpdateHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
appUpdateHelper = new AppUpdateHelper(getApplicationContext());
appUpdateHelper.handleAppUpdate();
}

Java.lang.NullPointerException at getapplicationContext() Android

Do not use an activity context inside its constructor, it will now work. Please put all the code from the constructor into a method and call it inside the `onCreate method like this:

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
// TODO Auto-generated method stub
setContentView(R.layout.fragment_cliente);

getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);

SecurePreferences mSessao = new SecurePreferences(FragmentClientes.this, "sessao");
mSessao.put("menuAtual", "Clientes");

Integer idViagem = getIntent().getIntExtra(TAG, -1);

init(idViagem);

}

public void init(Integer mId) {

clientes = new ArrayList<ClienteModel>();

try {

mRepositorio = new Repositorio(FragmentClientes.this);

List lista = mRepositorio.getClientesViagem(mId);

clientes = lista;

ad = new ClientViewAdapter(FragmentClientes.this, this, clientes);

lv.setVerticalFadingEdgeEnabled(true);
lv.setVerticalScrollBarEnabled(true);

lv.setAdapter(ad);

} catch (Exception e) {
e.printStackTrace();
}

}

Also, you will need to find another way of passing the integer parameter.

For this, use Intent.putExtra(TAG, idViagem) on the intent which starts the activity and then retrieve the value in the constructor with getIntent().getIntExtra(TAG, -1)

For example if you're starting FragmentClientes activity from another activity:

Intent intent = new Intent(this, FragmentClientes.class);
intent.putExtra("idViagem", int_value_of_id_that_you_passed_through_constructor);
startActivity(intent);

Null pointer, Context in activity intent for bottom navigation

You can't instantiate Activities directly. You can't directly pass components between them. You don't even need an Activity for this.

Remove the Activity extension:

public class BottomNavigationBuilder {
//...
}

Then just use your context reference for anything that needs a Context (new Intent(context, PizzaActivity.class), context.startActivity(), etc).

You also don't need to reassign bottomNavigation after passing it to the builder. It's the same instance, so any changes made to bottomNavigation inside the Builder are also made to bottomNavigation outside.

Null Pointer exception starting IntentService

If you're going to override onCreate() in your IntentService, then make sure you call super.onCreate() in it. That seems to quite likely be your problem.

Why is there NullPointerException on Context that is already declared?

Somewhere, you are calling new CrimeLab(null) or are otherwise passing null into the CrimeLab constructor.

In your now-updated question, you are creating a new CrimeLab in:

public static CrimeLab get(Context context) {
if (sCrimeLab == null) {
sCrimeLab = new CrimeLab(context);
}return sCrimeLab;
}

Hence, the Context being passed into get() is null. So, find where you are calling CrimeLab.get() and fix the parameter so that it is not null.

null pointer in openFileOutput

You're receiving a null pointer exception because you're setting the Context variable con to null and then referencing it with con.openFileOutput.

Where are you using this code, in an activity?

If this code is in your Activity, just remove the Context variable and call openFileOutput. You can do this because Activity derives from Context. If the code is in another class you should pass a context into the class and use it.



Related Topics



Leave a reply



Submit