---
title: "Null pointer Exception occured in onCreate() when acessing view in Android"  
description: "Null pointer Exception occured in onCreate() when acessing view in Android"  
author: "Anonymous User"  
published: 2015-10-15  
updated: 2015-10-15  
canonical: https://www.mindstick.com/forum/33516/null-pointer-exception-occured-in-oncreate-when-acessing-view-in-android  
category: "android"  
tags: ["exception handling", "android", "android activity", "android layout", "exception", "nullpointerexception", "android fragment"]  
reading_time: 2 minutes  

---

# Null pointer Exception occured in onCreate() when acessing view in Android

I've created a new [activity](https://www.mindstick.com/forum/12894/how-to-force-recreation-of-activity-fragment-on-restore) using a wizard. I get [NullPointerException](https://www.mindstick.com/forum/159415/an-android-app-crashes-with-a-nullpointerexception) when attempting to call a [method](https://www.mindstick.com/forum/166/webservice-method) on [Views](https://www.mindstick.com/forum/12838/compile-views-in-asp-dot-net-mvc) obtained with findViewById() in my activity onCreate().\
Activity onCreate():\

```
@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    View something = findViewById(R.id.something);    something.setOnClickListener(new View.OnClickListener() { ... }); // NPE HERE    if (savedInstanceState == null) {        getSupportFragmentManager().beginTransaction()                .add(R.id.container, new PlaceholderFragment()).commit();    }}
```

\
[Layout](https://www.mindstick.com/articles/12853/android-layout-and-its-type) [XML](https://www.mindstick.com/blog/203/working-with-xml-data-in-sql-server) (fragment_main.xml):\

```
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="packagename.MainActivity$PlaceholderFragment" >    <View        android:layout_width="100dp"        android:layout_height="100dp"        android:id="@+id/something" /></RelativeLayout>
```

## Replies

### Reply by Anonymous User

The view is in the fragment layout (fragment_main.xml) and not in the activity layout (activity_main.xml). onCreate() is too early in the lifecycle to find it in the activity view hierarchy, and a null is returned. Invoking a method on null causes the NPE.\
The preferred solution is to move the code to the fragment onCreateView(), calling findViewById() on the inflated fragment layout rootView:\

```
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,    Bundle savedInstanceState) {  View rootView = inflater.inflate(R.layout.fragment_main, container,      false);  View something = rootView.findViewById(R.id.something); // not activity findViewById()  something.setOnClickListener(new View.OnClickListener() { ... });  return rootView;}
```

As a side note, the fragment layout will eventually be a part of the activity view hierarchy and discoverable with activity findViewById() but only after the fragment transaction has been run. Pending fragment transactions get executed in super.onStart() after onCreate()


---

Original Source: https://www.mindstick.com/forum/33516/null-pointer-exception-occured-in-oncreate-when-acessing-view-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
