0

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)
4

3 回答 3

1

你还没有实例化你的Order. 您需要在尝试访问其方法之前执行此操作。

在你的课堂上尝试这样的事情Order:-

//RETURN INSTANCE
public static Order getInstance(){
    initInstance(); // Instantiate and then return.
    return instance;
}

建议:-

在你的SectionsActivity,既然你已经声明public static Order order;了,为什么不这样使用它: -

order = Order.getInstance();
order.setTableNumber(table);
于 2013-02-21T04:12:50.233 回答
0

在 getInsance() 中调用 initInstance()。

虽然,您可能需要重新考虑使用单例。关于这是否是好的做法存在一些争论,如果没有看到您的整个应用程序,我不会发表评论。

于 2013-02-21T04:14:27.983 回答
0

R.Js 的建议确实消除了 NPE,但您的代码也应该可以工作,因为 OrderApplication 在创建时调用 Order.initInstance(),因此一旦 SectionsActivity 启动,就应该实例化单例。当然,除非您没有在 mainfest.xml 中声明 OrderApplication,这可能是 NPE 的根本原因:

<application
    android:name="OrderApplication"
于 2013-02-21T04:50:09.780 回答