waffles waffles - 21 days ago 4
Java Question

NullPointerException when accessing constructor

I feel like either I'm missing a vital point in OOP, or my constructor is wrong. I keep getting a null pointer exception when creating my object.

The logcat is saying that its coming from the first line of the '// default constructor'.

Here is the code for my class:

package com.chriswahlfeldt.homeworkapp;

import android.app.Activity;
import android.view.View;
import android.widget.EditText;

public class MyHomework extends Activity {

private EditText title, description;
private View homeworkView, activityView;

// default constructor
public MyHomework() {

homeworkView = getLayoutInflater().inflate(R.layout.add_homework, null);
activityView = getLayoutInflater().inflate(R.layout.activity_my, null);

title = (EditText) homeworkView.findViewById(R.id.classTitleET);
title.setText("");

description = (EditText) homeworkView.findViewById(R.id.descriptionET);
title.setText("");

}

public View getContentView_activity_my() { return activityView; }

public View getContentView_add_homework() { return homeworkView; }

public String getTitleTxt() { return title.getText().toString(); }

public String getDescriptionTxt() { return description.getText().toString(); }

public void setTitleTxt(String thatString) { title.setText(thatString); }

public void setDescriptionTxt(String thatString) { title.setText(thatString); }
}


And here is where it is used:

package com.chriswahlfeldt.homeworkapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MyActivity extends Activity
{

private MyHomework hW = new MyHomework();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(hW.getContentView_activity_my());

final Button addHWBtn = (Button) findViewById(R.id.HWBtn);

addHWBtn.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
setContentView(hW.getContentView_add_homework());
}
});
}
}


.xml file: activity_my.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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=".MyActivity"
android:id="@+id/mainRelLayout" >

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/HWBtn"
android:textSize="25sp"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="+Homework"/>

</RelativeLayout>


.xml file: add_homework.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:alpha=".8">

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/classTitleET"
android:layout_gravity="top"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:capitalize="sentences"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="10dp"
android:hint="Class Title"
android:textSize="20sp" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="8"
android:id="@+id/descriptionET"
android:layout_below="@+id/classTitleET"
android:layout_alignLeft="@+id/classTitleET"
android:layout_alignStart="@+id/classTitleET"
android:layout_alignRight="@+id/classTitleET"
android:layout_alignEnd="@+id/classTitleET"
android:hint="Homework Description"
android:textSize="20sp"
android:height="120dp"
android:gravity="top" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/createBtn"
android:hint="Create"
android:textSize="20sp"
android:layout_below="@+id/descriptionET"
android:layout_alignLeft="@+id/descriptionET"
android:layout_alignStart="@+id/descriptionET"
android:layout_alignRight="@+id/descriptionET"
android:layout_alignEnd="@+id/descriptionET" />

</RelativeLayout>


Thx!

nPn nPn
Answer

yout can't call getLayoutInflater() directly , you need to call it on an instance of the LayoutInflater class. try this

LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
homeworkView = inflater.inflate(R.layout.add_homework, null);

you can replace context with "this" if you are calling this inan activity

Comments