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

R.Jsの提案はNPEを排除しますが、OrderApplicationが作成時にOrder.initInstance()を呼び出すため、コードも機能するはずです。そのため、SectionsActivityが開始するとシングルトンがインスタンス化される必要があります。もちろん、mainfest.xmlでOrderApplicationを宣言していない場合を除きます。これは、NPEの根本的な原因である可能性があります。

<application
    android:name="OrderApplication"
于 2013-02-21T04:50:09.780 に答える
0

getInsance()内でinitInstance()を呼び出します。

ただし、シングルトンの使用を再検討することをお勧めします。これが良い習慣であるかどうかについてはいくつかの議論があります。あなたのアプリケーション全体を見ずにコメントすることはしません。

于 2013-02-21T04:14:27.983 に答える