0

I'm rather new to android programming, and am running into my old friend the NullPointerException...

I've been on it for quite a while now, but can't figure out what is wrong.

basicly gives me a NullPointerException when I try to call any .setText() methods, maybe someone sees what I'm doing wrong here...(though i tried to follow examples as close as possible)

public class lessonView extends Activity {

TextView adressOfLecture;
TextView lecturer;
TextView lesson;

Lecture lecture;

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

    Bundle data = getIntent().getExtras();
    lecture = (Lecture) data.getParcelable("student");

    adressOfLecture = (TextView)findViewById(R.id.lectureViewAdressLabel);
    lecturer = (TextView)findViewById(R.id.lectureViewLecturerLabel);
    lesson = (TextView)findViewById(R.id.lectureViewTitle);

    updateLabels();

}


@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_lesson_view, 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);
}

private void updateLabels(){
    adressOfLecture.setText(lecture.getRoom());
    lecturer.setText(lecture.getTutor());
    lesson.setText(lecture.getName());
}

}

also, here's my xml file:

<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.coronarip7.app.stupla.lessonView">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="(ort)"
    android:id="@+id/lectureViewAdressLabel"
    android:layout_centerVertical="true"
    android:layout_toStartOf="@+id/lectureViewTitle"
    android:layout_marginRight="86dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="dozent"
    android:id="@+id/lectureViewLecturerLabel"
    android:layout_toEndOf="@+id/lectureViewTitle"
    android:layout_alignTop="@+id/lectureViewAdressLabel"
    android:layout_alignParentEnd="true"
    android:layout_marginRight="27dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/lectureViewTitle"
    android:gravity="center_horizontal"
    android:text="test"
    android:textSize="40dp"
    android:textStyle="bold"
    android:padding="10dp"
    android:layout_row="0"
    android:layout_column="0"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

and the logcat I'm getting:

04-25 01:23:51.058  12236-12236/com.coronarip7.app.stupla E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.coronarip7.app.stupla, PID: 12236
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.coronarip7.app.stupla/com.coronarip7.app.stupla.lessonView}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
        at android.app.ActivityThread.access$800(ActivityThread.java:145)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5081)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.coronarip7.app.stupla.lessonView.updateLabels(lessonView.java:59)
        at com.coronarip7.app.stupla.lessonView.onCreate(lessonView.java:31)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
            at android.app.ActivityThread.access$800(ActivityThread.java:145)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5081)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            at dalvik.system.NativeStart.main(Native Method)

seriously, I am out of ideas on how to fix it...

Rami
  • 7,879
  • 12
  • 36
  • 66
  • Could you provide your 'updateLabels' method? That's what the logcat is pointing to but since you haven't provided it, nobody can really help you. – Mark O'Sullivan Apr 24 '15 at 23:48
  • I think `lecture` is `null`, but I can't be certain. – Paul Boddington Apr 24 '15 at 23:49
  • What is on line 59 of lessonView.java? – Emmanuel Apr 24 '15 at 23:50
  • @pbabcdefp it seems like lecture has the value of "dozent" – Mark O'Sullivan Apr 24 '15 at 23:50
  • @Emmanuel I'm guessing it's the updateLabels() method. That's why I've asked him to post it. – Mark O'Sullivan Apr 24 '15 at 23:51
  • It is a line inside of `updateLabels()` I just want to know which one. – Emmanuel Apr 24 '15 at 23:52
  • must be `null` `lecture` – AndroidEx Apr 24 '15 at 23:53
  • 4
    Probably `lecture` is null, add `if(lecture!=null){...}` in your `updateLabels()` and see if you still have the NPE – Rami Apr 24 '15 at 23:54
  • @MOS182 he has added the method, at the the bottom of the code he has posted. Well, it is showing for me – Sanj Apr 24 '15 at 23:54
  • I think it has to be `lecture`. The `TextView` fields were all assigned in `onCreate`. – Paul Boddington Apr 24 '15 at 23:55
  • Not sure of android:layout_alignTop="@+id/lectureViewAdressLabel", it should be android:layout_alignTop="@id/lectureViewAdressLabel". You maybe reseting the value for R.id.lectureViewAdressLabel. [See this](http://stackoverflow.com/questions/5025910/difference-between-id-and-id-in-android) – Sanj Apr 24 '15 at 23:58
  • I agree with @Rami. You've defined lecturer and lesson, so I believe line 59 is `adressOfLecture.setText(lecture.getRoom());` which then points to line 31 which must be `lecture = (Lecture) data.getParcelable("student");` which is causing the issue because lecture isn't receiving a value. – Mark O'Sullivan Apr 25 '15 at 00:00
  • 1
    That's irrelevant sanj. The problem is he is not receiving lecture. He needs to look at the code where the intent is fired I think. – Sam Redway Apr 25 '15 at 00:01

3 Answers3

0

First check if you properly put the extra in the intent. Then I would use the following test in your onCreate method:

Intent intent = getIntent();
if (intent.hasExtra("student")){
    lecture = (Lecture) intent.getParcelableExtra("student");
}

Then test if lecture is null like Rami wrote earlier.

0

Ok guys, thanks for the suggestions, but I'll go the global route. I'll create a static version of my array (from which i wanted to pass an object via the Intent), and just pass an int[] array with the coordiantes and call it in the new view.

But thanks for the quick answers anyways!

(I'm new to stackoverflow, is there a way to mark a question as "solved", or "not-needed-to-be-solved-anymore"?)

0

Do not use static objects in your code like arrays, objects, they have globally available, you should create intent and add your data into your intent like and call your activity

Intent intent = new Intent ();
intent.putExtra ("student",value);

start activity with intent And in your lessonViewActivity check for intent like

Intent intent = getIntent();
if (intent.hasExtra("student")){
    lecture = (Lecture) intent.getParcelableExtra("student"); 
if (lecture !=null){
updateLabels ();
}
}
Haris Qurashi
  • 2,104
  • 1
  • 13
  • 28
  • well, thats what i did, and that didnt work. and why not go static? because this view should display different content depending on what object would be passed to it. – Ben Groß Apr 25 '15 at 08:36