Andrew Jake Villegas Andrew Jake Villegas - 6 months ago 622
Java Question

Null Pointer Exception - Issues with Butterknife - Android Application Development

Hi Guys. I am having another newbie problem. As you can see in my error message. At my MainActivity.java, line 125 is presenting a Null Pointer Exception

05-03 22:19:17.559 22615-22615/com.example.andrewjakevillegas.stormy E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.andrewjakevillegas.stormy, PID: 22615
java.lang.
NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.andrewjakevillegas.stormy.MainActivity.updateDisplay(MainActivity.java:125)
at com.example.andrewjakevillegas.stormy.MainActivity.access$200(MainActivity.java:38)
at com.example.andrewjakevillegas.stormy.MainActivity$1$1.run(MainActivity.java:100)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)


Here is my MainActivity.java

package com.example.andrewjakevillegas.stormy;

import...

public class MainActivity extends ActionBarActivity {

public static final String TAG = MainActivity.class.getSimpleName();

private CurrentWeather mCurrentWeather;

@BindView(R.id.timeLabel)
TextView mTimeLabel;
@BindView(R.id.temperatureLabel)
TextView mTemperatureLabel;
@BindView(R.id.humidityLabel)
TextView mHumidityValue;
@BindView(R.id.precipLabel)
TextView mPrecipValue;
@BindView(R.id.summaryLabel)
TextView mSummaryLabel;
@BindView(R.id.iconImageView)
ImageView mIconImageView;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient mClient;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
ButterKnife.bind( this );



if (isNetworkAvailable()) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url( forecastURL )
.build();

Call call = client.newCall( request );
call.enqueue( new Callback() {
@Override
public void onFailure(Call call, IOException e) {

}

@Override
public void onResponse(Call call, Response response) throws IOException {

try {
String jsonData = response.body().string();
Log.v( TAG, jsonData );
if (response.isSuccessful()) {
mCurrentWeather = getCurrentDetails( jsonData );
runOnUiThread( new Runnable() {
@Override
public void run() {
updateDisplay();
}
} );
} else {
alertUserAboutError();
}
} catch (IOException e) {
Log.e( TAG, "Exception caught: ", e );
} catch (JSONException e) {
Log.e( TAG, "Exception caught: ", e );
}
}
} );
} else {
Toast.makeText( this, getString( R.string.network_unavailable_message ),
Toast.LENGTH_LONG ).show();
}
Log.d( TAG, "Main UI code is running!" );

// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
mClient = new GoogleApiClient.Builder( this ).addApi( AppIndex.API ).build();
}

private void updateDisplay() {
mTemperatureLabel.setText( mCurrentWeather.getTemperature() + "" );
}

private CurrentWeather getCurrentDetails(String jsonData) throws JSONException {
JSONObject forecast = new JSONObject( jsonData );
String timezone = forecast.getString( "timezone" );
Log.i( TAG, "From JSON: " + timezone );

JSONObject currently = forecast.getJSONObject( "currently" );
CurrentWeather currentWeather = new CurrentWeather();
currentWeather.setHumidity( currently.getDouble( "humidity" ) );
currentWeather.setTime( currently.getLong( "time" ) );
currentWeather.setIcon( currently.getString( "icon" ) );
currentWeather.setPrecipChance( currently.getDouble( "precipProbability" ) );
currentWeather.setSummary( currently.getString( "summary" ) );
currentWeather.setTemperature( currently.getDouble( "temperature" ) );

return new CurrentWeather();
}

private boolean isNetworkAvailable() {
ConnectivityManager manager = (ConnectivityManager)
getSystemService( Context.CONNECTIVITY_SERVICE );
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
boolean isAvailable = false;
if (networkInfo != null && networkInfo.isConnected()) {
isAvailable = true;
}

return isAvailable;
}

private void alertUserAboutError() {
AlertDialogFragment dialog = new AlertDialogFragment();
dialog.show( getFragmentManager(), "error_dialog" );
}

@Override
public void onStart() {
super.onStart();

// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
mClient.connect();
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse( "http://host/path" ),
// TODO: Make sure this auto-generated app URL is correct.
Uri.parse( "android-app://com.example.andrewjakevillegas.stormy/http/host/path" )
);
AppIndex.AppIndexApi.start( mClient, viewAction );
}

@Override
public void onStop() {
super.onStop();

// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse( "http://host/path" ),
// TODO: Make sure this auto-generated app URL is correct.
Uri.parse( "android-app://com.example.andrewjakevillegas.stormy/http/host/path" )
);
AppIndex.AppIndexApi.end( mClient, viewAction );
mClient.disconnect();
}
}


This particular line of code is giving me the headache.This is line 125 from my error log.

private void updateDisplay() {
mTemperatureLabel.setText( mCurrentWeather.getTemperature() + "" );
}


I have checked my layout. I got the right id and type of TextView.

Here is my activity_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: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=".MainActivity"
android:id="@+id/relativeLayout"
android:background="#fffc970b">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/temperatureLabel"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textColor="@android:color/white"
android:textSize="150dp"
android:text="100"/>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/degreeImageView"
android:layout_alignTop="@+id/temperatureLabel"
android:layout_toRightOf="@+id/temperatureLabel"
android:layout_toEndOf="@+id/temperatureLabel"
android:layout_marginTop="50dp"
android:src="@drawable/degree"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="At 5:00 PM it will be"
android:id="@+id/timeLabel"
android:layout_above="@+id/temperatureLabel"
android:layout_centerHorizontal="true"
android:textColor="#95ffffff"
android:textSize="18sp"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alcatraz Island, CA"
android:id="@+id/locationLabel"
android:layout_above="@+id/timeLabel"
android:layout_centerHorizontal="true"
android:layout_marginBottom="60dp"
android:textColor="@android:color/white"
android:textSize="24sp"/>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iconImageView"
android:layout_alignBottom="@+id/locationLabel"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:src="@drawable/cloudy_night"/>

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/temperatureLabel"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:weightSum="100"
android:id="@+id/linearLayout">

<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="50">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="HUMIDITY"
android:id="@+id/humidityLabel"
android:textColor="#95ffffff"
android:gravity="center_horizontal"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="0.88"
android:id="@+id/humidityValue"
android:textColor="@android:color/white"
android:textSize="24sp"
android:gravity="center_horizontal"/>
</LinearLayout>

<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="50">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RAIN/SNOW?"
android:id="@+id/precipLabel"
android:textColor="#95ffffff"
android:gravity="center_horizontal"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="100"
android:id="@+id/precipValue"
android:textColor="@android:color/white"
android:textSize="24sp"
android:gravity="center_horizontal"/>
</LinearLayout>
</LinearLayout>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stormy with a chance of meatballs"
android:id="@+id/summaryLabel"
android:layout_below="@+id/linearLayout"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:textColor="@android:color/white"
android:textSize="18dp"
android:gravity="center_horizontal"/>




My Current Weather Java Class should get and set the right values.

public int getTemperature() {
return (int)Math.round(mTemperature);
}

public void setTemperature(double temperature) {
mTemperature = temperature;
}


Here is a copy of my CurrentWeather.java just in case you need to refer to it.

package com.example.andrewjakevillegas.stormy;

public class CurrentWeather {
private String mIcon;
private long mTime;
private double mTemperature;
private double mHumidity;
private double mPrecipChance;
private String mSummary;

public String getIcon() {
return mIcon;
}

public void setIcon(String icon) {
mIcon = icon;
}

public int getIconId(){
int iconId = R.drawable.clear_day;

if (mIcon.equals("clear-day")){
iconId = R.drawable.clear_day;
}

else if (mIcon.equals("clear-night")){
iconId = R.drawable.clear_night;
}

else if (mIcon.equals("rain")) {
iconId = R.drawable.rain;
}
else if (mIcon.equals("snow")) {
iconId = R.drawable.snow;
}
else if (mIcon.equals("sleet")) {
iconId = R.drawable.sleet;
}
else if (mIcon.equals("wind")) {
iconId = R.drawable.wind;
}
else if (mIcon.equals("fog")) {
iconId = R.drawable.fog;
}
else if (mIcon.equals("cloudy")) {
iconId = R.drawable.cloudy;
}
else if (mIcon.equals("partly-cloudy-day")) {
iconId = R.drawable.partly_cloudy;
}
else if (mIcon.equals("partly-cloudy-night")) {
iconId = R.drawable.cloudy_night;
}


return iconId;
}

public long getTime() {
return mTime;
}

public void setTime(long time) {
mTime = time;
}

public int getTemperature() {
return (int)Math.round(mTemperature);
}

public void setTemperature(double temperature) {
mTemperature = temperature;
}

public double getHumidity() {
return mHumidity;
}

public void setHumidity(double humidity) {
mHumidity = humidity;
}

public double getPrecipChance() {
double precipPercentage = mPrecipChance * 100;
return (int)Math.round(precipPercentage);
}

public void setPrecipChance(double precipChance) {
mPrecipChance = precipChance;
}

public String getSummary() {
return mSummary;
}

public void setSummary(String summary) {
mSummary = summary;
}


}


Here are the image results when I ran the debugger.

enter image description here

The butterknife is not working from what it looks like. Do you have any suggestions how I can solve this? Thank you.

Answer

I Found the answer to my problem.

You have to download - copy and paste the whole butterknife-compiler (code below)to your build gradle. I was just copying the dependencies and adding it to my gradle dependencies. Which is giving me the problem.

buildscript {
  repositories {
    mavenCentral()
   }
  dependencies {
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
  }
}

apply plugin: 'com.neenbedankt.android-apt'

dependencies {
  compile 'com.jakewharton:butterknife:8.0.1'
  apt 'com.jakewharton:butterknife-compiler:8.0.1'
}
Comments