I am working on a project for college and I am using the Singleton Design Pattern to globally access a variable from any activity within my application.
I am getting this error when I run my application:
java.lang.NullPointerException at com.example.waitronproto9.SectionsActivity$2.onClick(SectionsActivity.java:80)
This is the line of code it relates to:
Order.getInstance().setTableNumber(table);
This is my entire SectionsActivity
:
public class SectionsActivity extends Activity{
LayoutInflater inflater;
ActionBar ab;
Button homeBtn;
Button viewBtn;
Button langBtn;
ImageView callWaiterBtn;
public static Order order;
public static EditText tableNum;
public static EditText coverNum;
int table;
int covers;
private OrderApplication app;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_sections);
Dialog d = createDialog();
d.show();
callWaiterBtn = (ImageView)findViewById(R.id.callWaiter);
callWaiterBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "A Waiter is on their way!",
Toast.LENGTH_SHORT).show();
}
});
}
public Dialog createDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(SectionsActivity.this);
View v = getLayoutInflater().inflate(R.layout.order_dialog, null);
builder.setView(v);
tableNum = (EditText)v.findViewById(R.id.numberEntry);
coverNum = (EditText)v.findViewById(R.id.coversEntry);
builder.setMessage("Order Information");
builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// Create order object in here
table = Integer.parseInt(tableNum.getText().toString());
covers = Integer.parseInt(coverNum.getText().toString());
Order.getInstance().setTableNumber(table);
Order.getInstance().setCoverNumber(covers);
Toast.makeText(SectionsActivity.this, "Order Created",
Toast.LENGTH_SHORT).show();
}
});
return builder.create();
}
}
This is the Order
class:
public class Order {
int tableNumber;
int coverNumber;
double amount;
List<Dish> orderItems;
private static Order instance;
// Constructors hidden because it is singleton...
private Order(int tableNumber, int coverNumber, double amount,
List<Dish> orderItems) {
super();
this.tableNumber = tableNumber;
this.coverNumber = coverNumber;
this.amount = amount;
this.orderItems = orderItems;
}
private Order() {
super();
orderItems = new ArrayList<Dish>();
}
// SET THE INSTANCE
public static void initInstance() {
if (instance == null) {
instance = new Order();
}
}
// RETURN INSTANCE
public static Order getInstance(){
return instance;
}
// Getters and Setters
public int getTableNumber() {
return tableNumber;
}
public void setTableNumber(int tableNumber) {
this.tableNumber = tableNumber;
}
public int getCoverNumber() {
return coverNumber;
}
public void setCoverNumber(int coverNumber) {
this.coverNumber = coverNumber;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public List<Dish> getOrderItems() {
return orderItems;
}
// Add to order
public void addToOrder(Dish d){
this.orderItems.add(d);
}
public void removeFromOrder(int position){
this.orderItems.remove(position);
}
public void setOrderItems(List<Dish> orderItems) {
this.orderItems = orderItems;
}
}
This is the OrderApplication
class:
public class OrderApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
initSingletons();
}
protected void initSingletons(){
Order.initInstance();
}
}
Can anyone see where I am going wrong? Any advice is much appreciated!
STACK TRACE:
02-21 04:06:14.559: E/AndroidRuntime(29562): FATAL EXCEPTION: main
02-21 04:06:14.559: E/AndroidRuntime(29562): java.lang.NullPointerException
02-21 04:06:14.559: E/AndroidRuntime(29562): at com.example.waitronproto9.SectionsActivity$2.onClick(SectionsActivity.java:80)
02-21 04:06:14.559: E/AndroidRuntime(29562): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
02-21 04:06:14.559: E/AndroidRuntime(29562): at android.os.Handler.dispatchMessage(Handler.java:99)
02-21 04:06:14.559: E/AndroidRuntime(29562): at android.os.Looper.loop(Looper.java:137)
02-21 04:06:14.559: E/AndroidRuntime(29562): at android.app.ActivityThread.main(ActivityThread.java:5041)
02-21 04:06:14.559: E/AndroidRuntime(29562): at java.lang.reflect.Method.invokeNative(Native Method)
02-21 04:06:14.559: E/AndroidRuntime(29562): at java.lang.reflect.Method.invoke(Method.java:511)
02-21 04:06:14.559: E/AndroidRuntime(29562): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-21 04:06:14.559: E/AndroidRuntime(29562): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-21 04:06:14.559: E/AndroidRuntime(29562): at dalvik.system.NativeStart.main(Native Method)