-2

I'm trying to build a basic table view with a few rows and columns populated with textviews to represent a lesson timetable. I have written some code which works out what lesson I would have using date and time, then change the colour of its textview. I tried to do this inside a separate class as I want it be executed in multiple ways. However when I run it I am getting a nullPointerException caused by findViewById.

Error:

FATAL EXCEPTION: main
11-23 19:43:51.870 7861-7861/com.example.harris.whathaveilearnt E/AndroidRuntime: Process: com.example.harris.whathaveilearnt, PID: 7861
11-23 19:43:51.870 7861-7861/com.example.harris.whathaveilearnt E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.harris.whathaveilearnt/com.example.harris.whathaveilearnt.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
11-23 19:43:51.870 7861-7861/com.example.harris.whathaveilearnt E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
11-23 19:43:51.870 7861-7861/com.example.harris.whathaveilearnt E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
11-23 19:43:51.870 7861-7861/com.example.harris.whathaveilearnt E/AndroidRuntime:     at android.app.ActivityThread.-wrap11(ActivityThread.java)
11-23 19:43:51.870 7861-7861/com.example.harris.whathaveilearnt E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
11-23 19:43:51.870 7861-7861/com.example.harris.whathaveilearnt E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
11-23 19:43:51.870 7861-7861/com.example.harris.whathaveilearnt E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148)
11-23 19:43:51.870 7861-7861/com.example.harris.whathaveilearnt E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417)
11-23 19:43:51.870 7861-7861/com.example.harris.whathaveilearnt E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
11-23 19:43:51.870 7861-7861/com.example.harris.whathaveilearnt E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
11-23 19:43:51.870 7861-7861/com.example.harris.whathaveilearnt E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-23 19:43:51.870 7861-7861/com.example.harris.whathaveilearnt E/AndroidRuntime:  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
11-23 19:43:51.870 7861-7861/com.example.harris.whathaveilearnt E/AndroidRuntime:     at android.app.Activity.findViewById(Activity.java:2090)
11-23 19:43:51.870 7861-7861/com.example.harris.whathaveilearnt E/AndroidRuntime:     at com.example.harris.whathaveilearnt.selectLesson.nextlesson(selectLesson.java:73)
11-23 19:43:51.870 7861-7861/com.example.harris.whathaveilearnt E/AndroidRuntime:     at com.example.harris.whathaveilearnt.MainActivity.onCreate(MainActivity.java:41)
11-23 19:43:51.870 7861-7861/com.example.harris.whathaveilearnt E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:6237)
11-23 19:43:51.870 7861-7861/com.example.harris.whathaveilearnt E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
11-23 19:43:51.870 7861-7861/com.example.harris.whathaveilearnt E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
11-23 19:43:51.870 7861-7861/com.example.harris.whathaveilearnt E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
11-23 19:43:51.870 7861-7861/com.example.harris.whathaveilearnt E/AndroidRuntime:     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
11-23 19:43:51.870 7861-7861/com.example.harris.whathaveilearnt E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
11-23 19:43:51.870 7861-7861/com.example.harris.whathaveilearnt E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
11-23 19:43:51.870 7861-7861/com.example.harris.whathaveilearnt E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148) 
11-23 19:43:51.870 7861-7861/com.example.harris.whathaveilearnt E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417) 
11-23 19:43:51.870 7861-7861/com.example.harris.whathaveilearnt E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)

Activity:

public class selectLesson extends MainActivity {

    Calendar c = Calendar.getInstance();
    int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
    int dateDays = c.get(Calendar.DAY_OF_MONTH);
    int dateMonth = c.get(Calendar.MONTH);
    int weekOfMonth = c.get(Calendar.WEEK_OF_MONTH);
    LocalTime currentTime = LocalTime.now();
    int nextLessonNumber = 0;
    String nextLessonString;
    int currentLesson;

    LocalTime lesson1Start = new LocalTime("09:15");
    LocalTime lesson1End = new LocalTime("10:15");
    LocalTime lesson2Start = new LocalTime("10:15");
    LocalTime lesson3Start = new LocalTime("11:40");
    LocalTime lesson4start = new LocalTime("13:45");
    LocalTime lesson5Start = new LocalTime("14:45");
    LocalTime endOfDay = new LocalTime ("15:50");

    private static final String TAG = "selectLesson";

    public void nextlesson() {

        if (currentTime.isBefore(lesson1Start)) {
            //p1 next
            nextLessonNumber = 1;
            Log.d(TAG, "It worked");
            currentLesson = 0;
        } else if (currentTime.isAfter(lesson1Start) && currentTime.isBefore(lesson1End)) {
            //p2 next
            nextLessonNumber = 2;
            Log.d(TAG, "It worked");
            currentLesson = 1;
        } else if (currentTime.isAfter(lesson2Start) && currentTime.isBefore(lesson3Start)) {
            //p3 next
            nextLessonNumber = 3;
            Log.d(TAG, "It worked");
            currentLesson = 2;
        } else if (currentTime.isAfter(lesson3Start) && currentTime.isBefore(lesson4start)) {
            //p4 next
            nextLessonNumber = 4;
            Log.d(TAG, "It worked");
            currentLesson = 3;
        } else if (currentTime.isAfter(lesson4start) && currentTime.isBefore(lesson5Start)) {
            //p5 next
            nextLessonNumber = 5;
            Log.d(TAG, "It worked");
            currentLesson = 4;
        } else if (currentTime.isAfter(lesson5Start) && currentTime.isAfter(endOfDay)) {
            currentLesson = 5;
        } else {
            nextLessonNumber = 0;
            Log.d(TAG, "It didn't worked");
        }



        TextView MonP1 = (TextView) findViewById(R.id.monP1Text);
        TextView MonP2 = (TextView) findViewById(R.id.monP2Text);
        TextView MonP3 = (TextView) findViewById(R.id.monP3Text);
        TextView MonP4 = (TextView) findViewById(R.id.monP4Text);
        TextView MonP5 = (TextView) findViewById(R.id.monP5Text);

        TextView TueP1 = (TextView) findViewById(R.id.tueP1Text);
        TextView TueP2 = (TextView) findViewById(R.id.tueP2Text);
        TextView TueP3 = (TextView) findViewById(R.id.tueP3Text);
        TextView TueP4 = (TextView) findViewById(R.id.tueP4Text);
        TextView TueP5 = (TextView) findViewById(R.id.tueP5Text);

        TextView WedP1 = (TextView) findViewById(R.id.wedP1Text);
        TextView WedP2 = (TextView) findViewById(R.id.wedP2Text);
        TextView WedP3 = (TextView) findViewById(R.id.wedP3Text);
        TextView WedP4 = (TextView) findViewById(R.id.wedP4Text);
        TextView WedP5 = (TextView) findViewById(R.id.wedP5Text);

        TextView ThursP1 = (TextView) findViewById(R.id.thursP1Text);
        TextView ThursP2 = (TextView) findViewById(R.id.thursP2Text);
        TextView ThursP3 = (TextView) findViewById(R.id.thursP3Text);
        TextView ThursP4 = (TextView) findViewById(R.id.thursP4Text);
        TextView ThursP5 = (TextView) findViewById(R.id.thursP5Text);

        TextView FriP1 = (TextView) findViewById(R.id.friP1Text);
        TextView FriP2 = (TextView) findViewById(R.id.friP2Text);
        TextView FriP3 = (TextView) findViewById(R.id.friP3Text);
        TextView FriP4 = (TextView) findViewById(R.id.friP4Text);
        TextView FriP5 = (TextView) findViewById(R.id.friP5Text);
        if ((weekOfMonth == 1) || (weekOfMonth == 3)) {
            Log.d(TAG, "It made it this far");
            switch (dayOfWeek) {
                case 1: //Sunday DO NOTHING

                    break;
                case 2: //Monday WEEK A
                    if (currentLesson == 1) {
                        MonP1.setTextColor(this.getResources().getColor(R.color.colorAccent));

                    } else if (currentLesson == 2) {
                        MonP2.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    } else if (currentLesson == 3) {
                        MonP3.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    } else if (currentLesson == 4) {
                        MonP4.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    } else if (currentLesson == 5) {
                        MonP5.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    }
                    break;

                case 3: //Tuesday WEEK A
                    if (currentLesson == 1) {
                        TueP1.setTextColor(this.getResources().getColor(R.color.colorAccent));

                    } else if (currentLesson == 2) {
                        TueP2.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    } else if (currentLesson == 3) {
                        TueP3.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    } else if (currentLesson == 4) {
                        TueP4.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    } else if (currentLesson == 5) {
                        TueP5.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    }
                    break;

                case 4: //Wednesday WEEK A
                    if (currentLesson == 1) {
                        WedP1.setTextColor(this.getResources().getColor(R.color.colorAccent));

                    } else if (currentLesson == 2) {
                        WedP2.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    } else if (currentLesson == 3) {
                        WedP3.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    } else if (currentLesson == 4) {
                        WedP4.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    } else if (currentLesson == 5) {
                        WedP5.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    }
                    break;

                case 5: //Thursday WEEK A
                    if (currentLesson == 1) {
                        ThursP1.setTextColor(this.getResources().getColor(R.color.colorAccent));

                    } else if (currentLesson == 2) {
                        ThursP2.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    } else if (currentLesson == 3) {
                        ThursP3.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    } else if (currentLesson == 4) {
                        ThursP4.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    } else if (currentLesson == 5) {
                        ThursP5.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    }
                    break;


                case 6: //Friday WEEK A
                    if (currentLesson == 1) {
                        FriP1.setTextColor(this.getResources().getColor(R.color.colorAccent));

                    } else if (currentLesson == 2) {
                        FriP2.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    } else if (currentLesson == 3) {
                        FriP3.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    } else if (currentLesson == 4) {
                        FriP4.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    } else if (currentLesson == 5) {
                        FriP5.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    }
                    break;
                case 7: //Saturday DO NOTHING

                    break;

            }
        } else {
            switch (dayOfWeek) {
                case 1: //Sunday DO NOTHING

                    break;
                case 2: //Monday WEEK A
                    if (currentLesson == 1) {
                        MonP1.setTextColor(this.getResources().getColor(R.color.colorAccent));

                    } else if (currentLesson == 2) {
                        MonP2.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    } else if (currentLesson == 3) {
                        MonP3.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    } else if (currentLesson == 4) {
                        MonP4.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    } else if (currentLesson == 5) {
                        MonP5.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    }
                    break;

                case 3: //Tuesday WEEK A
                    if (currentLesson == 1) {
                        TueP1.setTextColor(this.getResources().getColor(R.color.colorAccent));

                    } else if (currentLesson == 2) {
                        TueP2.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    } else if (currentLesson == 3) {
                        TueP3.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    } else if (currentLesson == 4) {
                        TueP4.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    } else if (currentLesson == 5) {
                        TueP5.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    }
                    break;

                case 4: //Wednesday WEEK A
                    if (currentLesson == 1) {
                        WedP1.setTextColor(this.getResources().getColor(R.color.colorAccent));

                    } else if (currentLesson == 2) {
                        WedP2.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    } else if (currentLesson == 3) {
                        WedP3.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    } else if (currentLesson == 4) {
                        WedP4.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    } else if (currentLesson == 5) {
                        WedP5.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    }
                    break;

                case 5: //Thursday WEEK A
                    if (currentLesson == 1) {
                        ThursP1.setTextColor(this.getResources().getColor(R.color.colorAccent));

                    } else if (currentLesson == 2) {
                        ThursP2.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    } else if (currentLesson == 3) {
                        ThursP3.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    } else if (currentLesson == 4) {
                        ThursP4.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    } else if (currentLesson == 5) {
                        ThursP5.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    }
                    break;


                case 6: //Friday WEEK A
                    if (currentLesson == 1) {
                        FriP1.setTextColor(this.getResources().getColor(R.color.colorAccent));

                    } else if (currentLesson == 2) {
                        FriP2.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    } else if (currentLesson == 3) {
                        FriP3.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    } else if (currentLesson == 4) {
                        FriP4.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    } else if (currentLesson == 5) {
                        FriP5.setTextColor(this.getResources().getColor(R.color.colorAccent));
                    }
                    break;
                case 7: //Saturday DO NOTHING

                    break;
            }
        }


    }





}

Main Activity:

package com.example.harris.whathaveilearnt;

import android.os.Bundle;
import android.provider.CalendarContract;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.format.Time;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import org.joda.time.LocalTime;

import java.util.Calendar;


public class MainActivity extends AppCompatActivity {


    private static final String TAG = "MyActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        selectLesson selectLesson = new selectLesson();
        Calendar c = Calendar.getInstance();
        int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
        int dateDays = c.get(Calendar.DAY_OF_MONTH);
        int dateMonth = c.get(Calendar.MONTH);
        LocalTime currentTime = LocalTime.now();

        selectLesson.nextlesson();







        TextView NextLessonTextView = (TextView) findViewById(R.id.NextLessonText);
        TextView DayOfTheWeek = (TextView) findViewById(R.id.dayOfWeekText);
        String dayOfWeekText;

//        DayOfTheWeek.setText(String.valueOf(dayOfWeek) + String.valueOf(dateDays));
        switch(dayOfWeek){
            case 1: dayOfWeekText = "Sunday";
                DayOfTheWeek.setText(dayOfWeekText +" " +dateDays +" " + dateMonth);
                break;
            case 2: dayOfWeekText = "Monday";
                DayOfTheWeek.setText(dayOfWeekText +" " +dateDays +" " + dateMonth);
                break;

            case 3: dayOfWeekText = "Tuesday";
                DayOfTheWeek.setText(dayOfWeekText +" " +dateDays +" " + dateMonth);
                break;
            case 4: dayOfWeekText = "Wednesday";
                DayOfTheWeek.setText(dayOfWeekText +" " +dateDays +" " + dateMonth);
                break;
            case 5: dayOfWeekText = "Thurday";
                DayOfTheWeek.setText(dayOfWeekText +" " +dateDays +" " + dateMonth);
                break;
            case 6: dayOfWeekText = "Friday";
                DayOfTheWeek.setText(dayOfWeekText +" " +dateDays +" " + dateMonth);
                break;
            case 7: dayOfWeekText = "Saturday";
                DayOfTheWeek.setText(dayOfWeekText +" " +dateDays +" " + dateMonth);
                break;
        }








        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
  • Can you post your `MainActivity` code as well? And where is `nextlesson()` called from? It seems to me that you have not called `setContentView(R.layout.your_layout);` before you start calling `findViewById()`. – ehehhh Nov 23 '15 at 19:55
  • 1
    Perhaps you have not called `setContentView()` in onCreate()? – Daniel Nugent Nov 23 '15 at 20:00

1 Answers1

0

selectLesson selectLesson = new selectLesson(); is not a proper way to instantiate an Activity in Android. Read more about it here: http://developer.android.com/training/basics/firstapp/starting-activity.html

BUT, in your case you might actually want to move the logic from selectLesson class into MainActivity and just call the nextlesson() method from there.

ehehhh
  • 1,066
  • 3
  • 16
  • 27