ERROR E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.henvelt_joseph, PID: 4817 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.henvelt_joseph/com.example.henvelt_joseph.MainActivity}: 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:3449) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7656) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 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 com.example.henvelt_joseph.MainActivity.onCreate(MainActivity.java:52) at android.app.Activity.performCreate(Activity.java:8000) at android.app.Activity.performCreate(Activity.java:7984) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
public class MainActivity extends AppCompatActivity {
//Create the buttons
Button MSButton, DeptButton, SpecButton, GGButton, GCButton, ISButton, GDButton,WFUBUtton,CUButton, CartButton;
//Create a Dummy String for sections of the app that are under construction
private final String DUMMYMESSAGE = "This Section of the App is under construction";
/**
* onCreate will create the buttons when the app is first started or when the homescreen is visted
* @param savedInstanceState
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Have the buttons find their IDs
MSButton = findViewById(R.id.MobileShopping);
DeptButton = findViewById(R.id.Departments);
SpecButton = findViewById(R.id.Specials);
GGButton = findViewById(R.id.GiftGuide);
GCButton = findViewById(R.id.GiftCards);
ISButton = findViewById(R.id.InventorySystem);
GDButton = findViewById(R.id.GetDirections);
WFUBUtton = findViewById(R.id.WorkForUs);
CUButton = findViewById(R.id.ContactUs);
CartButton = findViewById(R.id.CartMain);
//Go to the specials section to display items that are on sale and coupons
//In this section the user can add items to their carts
SpecButton.setOnClickListener(new View.OnClickListener() {
/**
* onClick will bring the user to the special section of the app
* @param view
*/
@Override
public void onClick(View view) {
Intent Specials = new Intent(MainActivity.this, Specials.class);
startActivity(Specials);
}
});
//Display a dummy MESSAGE for the section of the app that is not support.
MSButton.setOnClickListener(new View.OnClickListener() {
/**
* Will display a message informing the user that this section of the app is under construction
* @param view
*/
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), DUMMYMESSAGE,Toast.LENGTH_LONG).show();
}
});
//Display a dummy setOnClickListenerMESSAGE for the section of the app that is not support.
DeptButton.setOnClickListener(new View.OnClickListener() {
/**
* Will display a message informing the user that this section of the app is under construction
* @param view
*/
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), DUMMYMESSAGE,Toast.LENGTH_LONG).show();
}
});
//Display a dummy MESSAGE for the section of the app that is not support.
GGButton.setOnClickListener(new View.OnClickListener() {
/**
* Will display a message informing the user that this section of the app is under construction
* @param view
*/
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), DUMMYMESSAGE,Toast.LENGTH_LONG).show();
}
});
//Display a dummy MESSAGE for the section of the app that is not support.
GCButton.setOnClickListener(new View.OnClickListener() {
/**
* Will display a message informing the user that this section of the app is under construction
* @param view
*/
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), DUMMYMESSAGE,Toast.LENGTH_LONG).show();
}
});
//Display a dummy MESSAGE for the section of the app that is not support.
ISButton.setOnClickListener(new View.OnClickListener() {
/**
* Will display a message informing the user that this section of the app is under construction
* @param view
*/
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), DUMMYMESSAGE,Toast.LENGTH_LONG).show();
}
});
//Display a dummy MESSAGE for the section of the app that is not support.
GDButton.setOnClickListener(new View.OnClickListener() {
/**
* Will display a message informing the user that this section of the app is under construction
* @param view
*/
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), DUMMYMESSAGE,Toast.LENGTH_LONG).show();
}
});
//Display a dummy MESSAGE for the section of the app that is not support.
WFUBUtton.setOnClickListener(new View.OnClickListener() {
/**
* Will display a message informing the user that this section of the app is under construction
* @param view
*/
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), DUMMYMESSAGE,Toast.LENGTH_LONG).show();
}
});
//Display a dummy MESSAGE for the section of the app that is not support.
CUButton.setOnClickListener(new View.OnClickListener() {
/**
* Will display a message informing the user that this section of the app is under construction
* @param view
*/
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), DUMMYMESSAGE,Toast.LENGTH_LONG).show();
}
});
//Allow the user to go to the cart from the home screen
CartButton.setOnClickListener(new View.OnClickListener() {
/**
* Will bring the user to the shopping cart
* @param view
*/
@Override
public void onClick(View view) {
Intent Specials = new Intent(MainActivity.this, ShoppingCart.class);
startActivity(Specials);
}
});
}
public void setContentView(int activity_main) {
}
}
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/home">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/MobileShopping"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="29dp"
android:text="@string/Mobile_Shopping" />
<Button
android:id="@+id/Departments"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="29dp"
android:text="@string/Departments" />
<Button
android:id="@+id/Specials"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="29dp"
android:text="@string/Specials" />
<Button
android:id="@+id/GiftGuide"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="29dp"
android:text="@string/Gift_Guide" />
<Button
android:id="@+id/GiftCards"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="29dp"
android:text="@string/Gift_Cards" />
<Button
android:id="@+id/InventorySystem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="29dp"
android:text="@string/Inventory_System" />
<Button
android:id="@+id/GetDirections"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="29dp"
android:text="@string/Get_Directions" />
<Button
android:id="@+id/WorkForUs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="29dp"
android:text="@string/Work_For_Us" />
<Button
android:id="@+id/ContactUs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="29dp"
android:text="@string/ContactUs" />
<Button
android:id="@+id/CartMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="29dp"
android:text="@string/Cart" />
</LinearLayout>`enter code here`
</ScrollView>
<TextView
android:id="@+id/home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:text="@string/title_home"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Home" />