-1

I have been struggling with the same crash for 4 hours already and I still cannot figure out the cause of it. Can any kind soul enlighten me ? I am getting this error

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference

The moment it start to load my textview lbl it just crash and give me this error

Below is my code :

    public class BookDoc extends Fragment {
    View view;
     @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            // Inflate the layout for this fragment
         if (view != null) {
                ViewGroup parent = (ViewGroup) view.getParent();
                if (parent != null) {
                    parent.removeView(view);
                }
            }
            try {
                view = inflater.inflate(R.layout.activity_main, container, false);
            } catch (InflateException e) {

            }
         StrictMode.ThreadPolicy policy = new StrictMode.
                 ThreadPolicy.Builder().permitAll().build();
                 StrictMode.setThreadPolicy(policy);

        Typeface type = Typeface.createFromAsset(getActivity().getAssets(),"fonts/verdana.ttf"); 
        TextView lbl = (TextView) view.findViewById(R.id.textViewa);
        return view
        }

And this is my 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:background="#f5f6f5"
    
    >
    
    <ScrollView
        android:id="@+id/sv123"
       android:layout_width="match_parent"
    android:layout_height="match_parent"  >
    
        <RelativeLayout
             android:layout_width="match_parent"
    android:layout_height="match_parent" 
            >
    
    <LinearLayout
        android:id="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1.0"
        android:orientation="vertical"
        android:background="#cc7781" 
        android:visibility="gone"
        >
       <TextView
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        android:id="@+id/tba"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:gravity="center"
        android:text="View Existing Booking"
        android:textSize="20dp" />
       
         <TextView

        android:id="@+id/tbb"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:gravity="center"
        android:text="You have already made a booking."
        android:textSize="15dp" />
                <TextView
android:layout_marginBottom="20dp"
        android:id="@+id/tbc"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:gravity="center"
        android:text="Would you like to view it?"
        android:textSize="15dp" />
    </LinearLayout>
    <LinearLayout
        android:layout_below="@+id/top"
        android:id="@+id/top2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1.0"
        android:orientation="vertical" >

        <fragment
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.MapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        
    </LinearLayout>


                   <RelativeLayout
                       android:layout_below="@+id/top2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="#f5f6f5"
     >
          <TextView
        
        android:id="@+id/textViewa"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
android:textColor="#7D7D7D"
        android:text="SELECT A NEARBY CLINIC" />
          <View
              android:layout_marginTop="5dp"
               android:id="@+id/textView2"
                   android:layout_below="@+id/textViewa"
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:background="@android:color/darker_gray"/>
<ListView
    android:layout_marginTop="5dp"
    android:layout_below="@+id/textView2"
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
        
             />
    <TextView
         android:layout_marginTop="10dp"
        android:id="@+id/textViewff"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/listView1"
        android:textColor="#e94256"
        android:textSize="20dp"
        android:text="View All Clinics" />
 </RelativeLayout>
 </RelativeLayout>
 </ScrollView>
</RelativeLayout>
user3145668
  • 1
  • 1
  • 5
  • Did you ever run into an InflateException? Then you should change your layout instead of just not crashing and continuing as if everything was OK. (Empty catch blocks are a problem not a solution) – Bö macht Blau Jun 05 '18 at 19:42
  • Side note- do not turn off the strict mode thread policy. Doing that just about assures your code quality is going to be crap and your threading is going to cause massive UI lag. Touching StrictMode is a sign that you don't actually know what you're doing, and you're copy pasting really old, really clueless tutorials from idiots. – Gabe Sechan Jun 05 '18 at 19:50
  • If your code is ok, you must verify the file manifiest.xml and here you should be add yours activity's and permits neccesary for your app. Example of activity: . Example of permits: – Marcos Riveros Jun 05 '18 at 20:00

1 Answers1

1

Your view is not initialized and it is still null. Before your "if statement", initialize the view as below:

view = (View) inflater.inflate(R.layout.your_layout_file, container, false);

everything else is fine

Davi
  • 1,031
  • 12
  • 21
  • "Before your "if statement" ..." - so you want OP to initialize the View and next the freshly inflated View which is not yet added to the parent ViewGroup should be removed from this ViewGroup? – Bö macht Blau Jun 05 '18 at 19:50
  • @0X0nosugar I am just solving the error, not the logic. He is checking if parent ViewGroup is not null, so it will not remove the view that doesn't have a parent – Davi Jun 05 '18 at 19:59
  • Thank you @Dawit! I was trying to port an old project from eclipse.. Is there a difference as it was working when it compiled on eclipse a few years ago – user3145668 Jun 05 '18 at 20:08
  • @0X0nosugar , is my logic wrong ? What is the recommended solution as I am totally new to android – user3145668 Jun 05 '18 at 20:09
  • @user3145668 it should work normally, it might as to upgrade gradle and build files. But again, it will work normally – Davi Jun 05 '18 at 20:11
  • @Dawit - ok, I mixed up *parent* and *container*. But if inflating the View works, OP would not have run into an NPE in the first place. At the beginning, *view* is null, so the next thing to be executed will be the try-catch. There, something seems to go wrong because later on the NPE occurs. If I am right, then your answer makes sure the NPE is avoided by making the "something" happen earlier. That is not precisely a constructive solution. – Bö macht Blau Jun 06 '18 at 05:37
  • If this is only about avoiding a NPE you should flag the question as duplicate of [What is a NullPointerException...](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) instead of writing an answer. – Bö macht Blau Jun 06 '18 at 05:37