Aloweiwi Aloweiwi - 18 days ago 5
Java Question

nothing changed but XML file, and getting the following : Attempt to invoke virtual method SetonClickListner on a null object reference

The application I am working on was working perfectly; and I only changed the xml file design, and when I changed it I keep getting the following problem:

11-19 22:24:13.278 21043-21043/de.nocnoc.clean.cleanlight I/Timeline: Timeline: Activity_launch_request id:de.nocnoc.clean.cleanlight time:12506541
11-19 22:24:13.385 21043-21043/de.nocnoc.clean.cleanlight D/AndroidRuntime: Shutting down VM
11-19 22:24:13.386 21043-21043/de.nocnoc.clean.cleanlight E/AndroidRuntime: FATAL EXCEPTION: main
Process: de.nocnoc.clean.cleanlight, PID: 21043
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.nocnoc.clean.cleanlight/de.nocnoc.clean.flashlight.MaincourseActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2464)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:234)
at android.app.ActivityThread.main(ActivityThread.java:5526)
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)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at de.nocnoc.clean.flashlight.MaincourseActivity.onCreate(MaincourseActivity.java:163)
at android.app.Activity.performCreate(Activity.java:6285)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524)
at android.app.ActivityThread.access$900(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:234)
at android.app.ActivityThread.main(ActivityThread.java:5526)
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)


This is my activity_maincourse.xml:

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="de.nocnoc.clean.flashlight.DrinksActivity"
android:background="@android:color/background_light">

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scrollView">

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">



<ImageView
android:layout_width="80dp"
android:layout_height="200dp"
android:src="@drawable/lemon_juice"
android:id="@+id/item1_image"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="@android:dimen/app_icon_size"
android:layout_height="wrap_content"
android:background="#ffff8800"
android:textColor="@android:color/background_dark"
android:text="-"
android:id="@+id/item1minusbtn"
android:layout_marginStart="25dp"
android:layout_marginBottom="48dp"
android:layout_above="@+id/item2_image"
android:layout_toEndOf="@+id/item1_image" />

<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#ffffbb33"
android:text="0"
android:id="@+id/item10btn"
android:layout_alignBottom="@+id/item1minusbtn"
android:layout_toEndOf="@+id/item1minusbtn"
android:layout_marginStart="28dp"
android:background="@android:color/background_light"
android:autoText="true"
android:textColor="@android:color/background_dark" />
<Button
style="?android:attr/buttonStyleSmall"
android:background="#ffff8800"
android:textColor="@android:color/background_dark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:id="@+id/item1plusbtn"
android:layout_alignBottom="@+id/item10btn"
android:layout_toEndOf="@+id/item10btn"
android:layout_marginStart="32dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lemon Juice"
android:textSize="20dp"
android:id="@+id/item1text"
android:layout_marginBottom="35dp"
android:layout_above="@+id/item1minusbtn"
android:layout_alignStart="@+id/item1minusbtn"
android:background="@android:color/background_light"
android:textColor="@android:color/background_dark" />


note: i am sure there is no problem in the Java file since I changed nothing there, and I added nothing to the buttons,images,.. all the same but different design.

please, HELP!

Answer

As you have not uploaded the Java file, but there are some possibilities due to which these error occurs.

  1. Check for the view on which you have implemented the click listener exist and the id of that view is correct.
  2. Check weather that view id also exist on the xml file.
  3. Check weather you have correctly typecast it with the proper view,

Basically these two are the most basic solution which causes the null pointer exception. Hope this helps.