In a weather application, it throws a NullPointerException error on line: mWeatherLayout.setVisibility (View.INVISIBLE);

Mistake

09-07 23:59:35.061 14038-14038/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.alex.wta, PID: 14038 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.alex.wta/com.example.alex.wta.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setVisibility(int)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2702) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767) at android.app.ActivityThread.access$900(ActivityThread.java:177) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5951) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setVisibility(int)' on a null object reference at com.example.alex.wta.MainActivity.onCreate(MainActivity.java:59) at android.app.Activity.performCreate(Activity.java:6289) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2655) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767) at android.app.ActivityThread.access$900(ActivityThread.java:177) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5951) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) 

MainActivity

 public class MainActivity extends AppCompatActivity implements IView { @BindView(R.id.current_temp) TextView mCurrentTemp; @BindView(R.id.max_temp) TextView mMaxTemp; @BindView(R.id.min_temp) TextView mMinTemp; @BindView(R.id.icon) ImageView weatherIcon; @BindView(R.id.pressure) TextView mPressure; @BindView(R.id.humidity) TextView mHumidity; @BindView(R.id.search) EditText mSearchText; @BindView(R.id.city) TextView mCityName; @BindView(R.id.weather) LinearLayout mWeatherLayout; @BindView(R.id.detail) TextView mDetail; private Presenter presenter; private ProgressDialog mProgressDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); mWeatherLayout.setVisibility(View.INVISIBLE); DataManager dataManager = new DataManager(); presenter = new Presenter(dataManager, this); } @Override public void showWeatherData(WeatherResponse response) { mCurrentTemp.setText(String.valueOf(response.getWeatherData().getTemp()) + "℃"); mMaxTemp.setText(String.valueOf(response.getWeatherData().getTemp_max()) + "℃"); mMinTemp.setText(String.valueOf(response.getWeatherData().getTemp_min()) + "℃"); mPressure.setText(String.valueOf(response.getWeatherData().getPressure()) + " hPa"); mHumidity.setText(String.valueOf(response.getWeatherData().getHumidity()) + " %"); Object weatherDetail = response.getWeatherList().get(0); LinkedTreeMap linkedTreeMap = (LinkedTreeMap) weatherDetail; String iconUrl = String.valueOf(linkedTreeMap.get("icon")); mDetail.setText(String.valueOf(linkedTreeMap.get("main"))); Picasso.with(this) .load("http://openweathermap.org/img/w/" + iconUrl + ".png").into(weatherIcon); } @Override public void showLoadingDialog() { if (mProgressDialog == null) { mProgressDialog = new ProgressDialog(this); mProgressDialog.setTitle("Weather App"); mProgressDialog.setMessage("Loading..."); } mProgressDialog.show(); } @Override public void hideLoadingDialog() { mProgressDialog.hide(); mWeatherLayout.setVisibility(View.VISIBLE); } @Override public void errorLoadingData() { mWeatherLayout.setVisibility(View.INVISIBLE); Toast.makeText(this, "Error loading data, please try again.", Toast.LENGTH_SHORT).show(); } @OnClick(R.id.btn_search) public void onClickSearch() { String city = mSearchText.getText().toString(); mSearchText.setText(""); clearFocusAndHideInput(); mCityName.setText(city.toUpperCase()); presenter.getWeatherForCity(city); } private void clearFocusAndHideInput() { mSearchText.clearFocus(); mSearchText.setFocusable(false); mSearchText.setFocusableInTouchMode(false); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0); } @OnClick(R.id.search) public void onEditTextClick(View v) { mSearchText.setFocusableInTouchMode(true); mSearchText.setFocusable(true); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(v, InputMethod.SHOW_EXPLICIT); } @Override protected void onDestroy() { mProgressDialog.dismiss(); presenter.unsubscribe(); super.onDestroy(); } } 

Layout

  <LinearLayout android:id="@+id/weather" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:visibility="visible"> 
  • in xml correctly indicated id? - Andriy Martsinkevych
  • Yes, that's right - Cypher
  • strange, but I previously also had a problem with version 8.4.0. ButterKnife.setDebug (true) - will show what could be the problem. What version are you using? try this: compile 'com.jakewharton: butterknife: 8.0.1' annotationProcessor 'com.jakewharton: butterknife-compiler: 8.0.1' - Andriy Martsinkevych
  • Try using GONE instead of INVISIBLE. - iFr0z
  • 2
    Comment out the line with the error and see if the same error will fall out on other fields initiated by ButterNife - Roman

1 answer 1

in onCreate
in the mWeatherLayout.setVisiblity(View.INVISIBLE) line mWeatherLayout.setVisiblity(View.INVISIBLE)
mWeatherLayout not initialized

  • one
    The author of the question for initialization uses the ButterKnife library - Dmitry Ikryanov